Results 1 to 3 of 3
  1. #1
    nmvp is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default JDBC with Connector/J with MySQL

    Hi to all...
    I just want to connect to mysql database with connector/J ( JDBC)
    I have downloaded "mysql-connector-java-5.1.15.zip" .... Then i extracted the file from this folder to---->

    "C:\Program Files\Java\jdk1.6.0_13\jre\lib\ext\mysql-connector-java-5.1.15-bin.jar " and set environmental variables classpath to above url too...

    I can compile the code successfully ........
    But while running it gives errors.....
    I have given my code and running scenario too....

    Kindly help....

    ########################################

    import java.sql.*;
    import javax.sql.*;

    public class jdbc
    {
    public static void main(String[] args)
    {
    System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "jdbctutorial";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "sonali";

    try
    {
    Class.forName(driver).newInstance();
    conn = DriverManager.getConnection(url+dbName,userName,pa ssword);
    System.out.println("Connected to the database");
    conn.close();
    System.out.println("Disconnected from database");
    }
    catch (Exception e) {e.printStackTrace();}
    }
    }

    ################################################## ##
    On cmd-------->
    ##########################################
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\Administrator>cd C:\Program Files\Java\jdk1.6.0_13\bin


    C:\Program Files\Java\jdk1.6.0_13\bin>javac jdbc.java

    C:\Program Files\Java\jdk1.6.0_13\bin>java jdbc
    Exception in thread "main" java.lang.NoClassDefFoundError: jdbc
    Caused by: java.lang.ClassNotFoundException: jdbc
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 52)
    at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:320)
    Could not find the main class: jdbc. Program will exit.


    C:\Program Files\Java\jdk1.6.0_13\bin>



    ############################################

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    strage, i compiled your code with some changes to dbName, userName and password and i got

    MySQL Connect Example.
    Connected to the database
    Disconnected from database

    note, when you add the jar to your classpath, you must include the path and the full name of your jar. and it wouldn't hurt if you update your jdk to a newer version.
    Last edited by j2me64; 02-15-2011 at 04:33 PM.

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,745
    Rep Power
    19

    Default

    Why is your project in the JDK bin directory?
    That os a very bad place to choose to write your code...

Similar Threads

  1. Connector J with Eclipse and MySQL 5.0.x
    By int80 in forum Eclipse
    Replies: 11
    Last Post: 05-31-2010, 09:32 AM
  2. MySQL/JDBC Mysql query output
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-13-2009, 01:57 AM
  3. mysql connector to java
    By sweet angle in forum JDBC
    Replies: 4
    Last Post: 10-20-2008, 06:33 AM
  4. MySQL connector selection
    By Ms.Ranjan in forum JDBC
    Replies: 3
    Last Post: 06-23-2008, 05:27 PM
  5. help ... JDBC or PHP/MySQL
    By bluebarca in forum Advanced Java
    Replies: 1
    Last Post: 11-16-2007, 10:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •