Results 1 to 8 of 8
  1. #1
    mchammer11 is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default ClassNotFoundException when trying to connect to Mysql database

    Hi all,

    I am trying to get my program to connect to a mysql database, and I am getting a ClassNotFoundException: com.mysql.jdbc.Driver error. My code is below:
    Java Code:
    public void connect() {
        Connection conn = null;
    
        try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
          conn = DriverManager.getConnection("jdbc:mysql://localhost/cmp_wrkr",
                                                             "root", "myPassHere");
                    //TODO remove debug
    		System.out.println("Connected");
    
        } catch(Exception e) {
    	 	e.printStackTrace();
    
        } finally {
          try {
            if(conn != null)
              conn.close();
          } catch(SQLException e) {}
       }
    Thanks in advance for your help

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Add the mysql driver to the classpath.

  3. #3
    mchammer11 is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default

    Do you have an example of this? I can't seem to find a good one.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Much depends on how you are compiling and running the code. Are you doing this on the command line? In NetBeans? Eclipse? JCreator? Other?

    If the command line, have you looked at the standard Sun tutorials on the classpath?

  5. #5
    mchammer11 is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default

    I am trying to do this using Eclipse

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mchammer11 View Post
    I am trying to do this using Eclipse
    Then you'll need to add the jar to your project's build path. Right click on your project in the package view window on the left and then click on properties. Find the java build path and click on it, and then try to add the jar by going into the Libraries tab.

  7. #7
    mchammer11 is offline Member
    Join Date
    Apr 2010
    Posts
    14
    Rep Power
    0

    Default

    Awesome, it worked. Thanks so much Fubar!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 4
    Last Post: 04-28-2010, 07:13 PM
  2. Replies: 4
    Last Post: 03-31-2010, 01:08 PM
  3. Can NOT connect to Mysql
    By zed420 in forum New To Java
    Replies: 3
    Last Post: 12-18-2009, 09:26 PM
  4. Replies: 15
    Last Post: 04-15-2009, 02:51 AM
  5. Replies: 2
    Last Post: 06-12-2008, 08:09 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
  •