Results 1 to 20 of 24
Thread: JDBC mysql driver
- 01-17-2012, 09:36 PM #1
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
- 01-17-2012, 09:52 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-17-2012, 10:06 PM #3
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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:
3. When I try to run the code on server with "java Test" I get these error messages: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"); } }
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.
- 01-18-2012, 10:08 AM #4
Member
- Join Date
- Sep 2011
- Location
- Athens Greece
- Posts
- 29
- Rep Power
- 0
Re: JDBC mysql driver
try java -jar Test
- 01-18-2012, 03:04 PM #5
Member
- Join Date
- Jan 2012
- Location
- Panamá
- Posts
- 41
- Rep Power
- 0
Re: JDBC mysql driver
are using any ide?
- 01-18-2012, 04:58 PM #6
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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.
- 01-18-2012, 05:32 PM #7
Member
- Join Date
- Jan 2012
- Location
- Panamá
- Posts
- 41
- Rep Power
- 0
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
- 01-18-2012, 05:55 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 01-18-2012, 06:40 PM #9
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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"?
- 01-18-2012, 06:46 PM #10
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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.)
- 01-18-2012, 06:54 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-18-2012, 07:04 PM #12
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
Re: JDBC mysql driver
Ok, but I've got this line in my code:
Am I doing something wrong?Java Code:Class.forName("com.mysql.jdbc.Driver");
- 01-18-2012, 07:09 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
Re: JDBC mysql driver
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-18-2012, 07:15 PM #14
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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 ;).
- 01-18-2012, 07:55 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-18-2012, 08:20 PM #16
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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:
"mud" is the name of my MySQL database.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)
- 01-18-2012, 09:48 PM #17
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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:
Now I get these errors:Java Code:"jdbc:mysql://mud"
Can this be regarded as some kind of a progress? :)Java Code:Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
- 01-18-2012, 09:56 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-18-2012, 10:01 PM #19
Member
- Join Date
- May 2011
- Posts
- 84
- Rep Power
- 0
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"); } }
- 01-18-2012, 10:09 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,386
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
JDBC Problem - com.mysql.jdbc.Driver
By icu222much in forum Advanced JavaReplies: 5Last Post: 11-22-2011, 04:54 PM -
MySQL JDBC Driver ClassNotFound
By Levian in forum New To JavaReplies: 4Last Post: 06-17-2011, 09:37 AM -
ClassNotFoundException: com.mysql.jdbc.Driver
By tBKwtWS in forum New To JavaReplies: 9Last Post: 06-15-2011, 07:01 PM -
com.mysql.jdbc.Driver
By uthpalaw in forum EclipseReplies: 2Last Post: 10-14-2010, 05:09 AM -
ClassNotFoundException com.mysql.jdbc.Driver
By Heather in forum JDBCReplies: 4Last Post: 03-31-2010, 12:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks