Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default JDBC mysql driver

    Hi,

    I am trying to connect to MySQL database using JDBC but no luck so far. I get errors "not suitable driver found". What should be the driver for MySQL called? Do I have to install something or it comes with Java?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: JDBC mysql driver

    Just put the .jar file that contains your MySQL driver on your classpath; the (old) way of installing your driver is to use the Class.forName("nameOfYourDriverClass") method to make your driver install itself. The DriverManager class will know about the driver then and you can obtain a Connection from it given a suitable URL (specifying the database name etc.)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: JDBC mysql driver

    1. In folder "testapplication" I have folder "lib" (with mysql-connector) and Test.jar.
    2. In Test.jar I have Database.class with the following code:
    Java Code:
    import java.sql.*;
    
    public class Database {
        
        public static void main(String[] args) throws Exception {
         
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql:mydatabasename", "myusername", "mypassword");
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM test_table");
           
        }
        
    }
    3. When I try to run the code on server with "java Test" I get these error messages:
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: Test
    Caused by: java.lang.ClassNotFoundException: Test
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
    Could not find the main class: Test. Program will exit.

  4. #4
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: JDBC mysql driver

    try java -jar Test

  5. #5
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default Re: JDBC mysql driver

    are using any ide?

  6. #6
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: JDBC mysql driver

    @DeNiS_M
    I get "Unable to access jarfile Test" message

    @arielb
    Yes, NetBeans, but I export the jar and upload it to the server.

  7. #7
    arielb is offline Member
    Join Date
    Jan 2012
    Location
    Panamá
    Posts
    41
    Rep Power
    0

    Default Re: JDBC mysql driver

    I understand that the error is in development, if so you have to add the jar to the classpath of the IDE to recognize it.
    Here I have a guide that I made earlier.

    Add jar netbeans

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default Re: JDBC mysql driver

    Quote Originally Posted by Eleeist View Post
    @DeNiS_M
    I get "Unable to access jarfile Test" message
    The run it against the jar file?
    You can't expect it to work unless you are running that command in the directory that contains the jar file!

  9. #9
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: JDBC mysql driver

    Do I even have to use jars? Maybe I could just put the lib folder with mysql-connector in the same directory as my Test.class and run "java Test"?

  10. #10
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: JDBC mysql driver

    I experimented and got it to work... almost. I get these errors when compile:

    Java Code:
    Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:186)
        at Database.main(Database.java:7)

    (I changed the class name to Database.)

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: JDBC mysql driver

    See my first reply in this thread; the .jar file that contains the com.mysql.jdbc.Driver class is not on your classpath.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: JDBC mysql driver

    Ok, but I've got this line in my code:
    Java Code:
    Class.forName("com.mysql.jdbc.Driver");
    Am I doing something wrong?

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: JDBC mysql driver

    Quote Originally Posted by Eleeist View Post
    Ok, but I've got this line in my code:
    Java Code:
    Class.forName("com.mysql.jdbc.Driver");
    Am I doing something wrong?
    Yup, that method calls a ClassLoader and that object doesn't know where it can find the mentioned driver class. It's the classpath variable the tells a ClassLoader object where it should look.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: JDBC mysql driver

    Ok, I understand now. In Netbeans in my project I went to Libraries -> Add Library... -> and added MySQL JDBC Driver. Its Library classpath is somewhere in my NetBeans installation folder on my computer. Should it match the directory structure on my server (where I'm running the program?). If so, then I can't see a way to change it inside NetBeans (there is no option for changing the classpath).

    PS: thank you for your patience, as this is obviously quite easy and I just can't grasp it ;).

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: JDBC mysql driver

    There's another trick: the Java Runtime checks a special directory: JRE/lib/ext (JRE is where your Java Runtime is stored). .jar files in that directory don't have to be mentioned on the classpath environment variable; but be careful with it: it makes your jar (with all classes in it) available to each an every Java program you write and run (it can be a benefit though).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: JDBC mysql driver

    I put the driver in the directory you mentioned. I decided this time to not pack it into .jar and just upload and run Database.class. I get these error:

    Java Code:
    Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:mysql:mud
        at java.sql.DriverManager.getConnection(DriverManager.java:640)
        at java.sql.DriverManager.getConnection(DriverManager.java:200)
        at Database.main(Database.java:8)
    "mud" is the name of my MySQL database.

  17. #17
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: JDBC mysql driver

    I've done some research and found that I should specify the URL. Because I am using localhost and default port for MySQL installation, I have changed it to:

    Java Code:
    "jdbc:mysql://mud"
    Now I get these errors:

    Java Code:
    Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
    Can this be regarded as some kind of a progress? :)

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: JDBC mysql driver

    Yep, that's (a bit of) progress; your driver can't find a server of some sort. Check your mySQL manuals how to start up that server. (it would surprise me if that documentation doesn't give a simple example).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    Eleeist is offline Member
    Join Date
    May 2011
    Posts
    84
    Rep Power
    0

    Default Re: JDBC mysql driver

    YES! Got it to work without any errors... But the application exits from command line almsot immediately after launch. Any suggestions what is happening?

    Here is the code after modifications:

    Java Code:
    import java.sql.*;
    
    public class Database {
        
        public static void main(String[] args) throws Exception {
    
                Class.forName("com.mysql.jdbc.Driver");
          
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mud", "myusername", "mypassword");
    
        }
        
    }

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,541
    Blog Entries
    7
    Rep Power
    20

    Default Re: JDBC mysql driver

    So the driver class was found (and loaded) and it found the database; next your program stopped ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Advanced Java
    Replies: 5
    Last Post: 11-22-2011, 04:54 PM
  2. MySQL JDBC Driver ClassNotFound
    By Levian in forum New To Java
    Replies: 4
    Last Post: 06-17-2011, 09:37 AM
  3. ClassNotFoundException: com.mysql.jdbc.Driver
    By tBKwtWS in forum New To Java
    Replies: 9
    Last Post: 06-15-2011, 07:01 PM
  4. com.mysql.jdbc.Driver
    By uthpalaw in forum Eclipse
    Replies: 2
    Last Post: 10-14-2010, 05:09 AM
  5. Replies: 4
    Last Post: 03-31-2010, 12:08 PM

Posting Permissions

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