• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Installing jdbc-Drivers and connecting to MySQL Database.

 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am searching for a way to connect my java-programm to a MySQL-Database for several days now but everything i found either didn't seem to work for me or was far too difficult to understand, as i am new to java. I have copied some java-code from one of those tutorials, but it requires a jdbc-Driver to work and i have absolutly no idea how to install/use one of those. I have looked that up, too but all i was told was to change my CLASSPATH environmental variable. I have searched for tutorials on how to change the CLASSPATH and i even found some but they were all related to other problems, therefore they were not really helpfull to me.
What i want is a step-by-step, idiot-proof explanation on what exactly to put in the CLASSPATH, where to put the driver (and wich driver) and how to load and use it in the java-project.

This is my java-code:



and my CLASSPATH is D:\java\lib\ext\mysql-connector-java-5.0.6-bin.jar

Right now it fails right at the beginning, as it cannot load the driver. I am really desperate as i am doing this for an internship wich ends tomorrow and i want to present results and not just some unfinished code.

Thanks in advance,
Fratz00
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is fine. How are you running it? Command line (there isn't a main method so I can't tell), through a server, through an IDE? The answer to this will determine how to set your classpath.

You might also want to ask a member of the team to show you. The nice thing about internships is that you get mentoring included.
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there

The program you have looks ok. Since you already downloaded the jdbc driver, what command are you using to compile the program?

Hint: you need to specify the classpath
 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am running this code via bluej, by creating a new ConectThingy and then right click > connect(); (Well, basically I'm just running the method connect();).
Unfortunately I cannot ask the team because they never learned Java. Most of them are architects who learned how to hold a server alive, the Java-program I'm writing should just help make their job a bit easier.
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know if you use the Class.forName("").newInstance() then you need to catch InstantiationException and IllegalAccessException

If you don't use the .newInstance() part then your current code stays the same.

If you are using IDE, did it tell you what the error is? Does the IDE recognize the jdbc driver?
 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gives me the following output:

Treiber kann nicht geladen werden: com.mysql.jdbc.Driver.jar
Verbindung ist fehlgeschlagen: No suitable driver found for jdbc:mysql://192.168.1.2:3306/arbeitsplatz


(in english:
Couldn't load driver: com.mysql.jdbc.Driver.jar
Connection failed: No suitable driver found for jdbc:mysql://192.168.1.2:3306/arbeitsplatz
)

The connection failure is self-explanatory as the drivers couldn't be load, the other message is in line 23, when the program catches the ClassNotFoundException so i believe the program does not find the driver and assumes it does not exist. I have no idea how to fix this issue, though.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fratz,
Ok. BlueJ uses the system classpath. Have you tried restarting BlueJ since setting the system classpath?
 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did, now, but it didn't work. I have the feeling my classpath is totally wrong, can you tell me what it should look like and wether it should point to the .zip file or the .jar file inside the .zip or something completely different?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should point to the jar, which you are doing. The documentation page shows this. It uses UNIX, not Windows though.
 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it helps, I've made a screen shot of where i set my classpath, in case i have done that completely wrong.
Unbenannt.PNG
[Thumbnail for Unbenannt.PNG]
 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:The documentation page shows this.


I've been on that page but with my rather limited english skills and my even more limited understanding of java, I could make no sense of it.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your screenshot looks good. I'm not familiar with BlueJ (just looked at the docs). You might try running this program at the command line to get BlueJ out of the picture:

Change to the directory with your code:
javac *java
java -cp ".;D:\java\lib\ext\mysql-connector-java-5.0.6-bin.jar" YourClassWithAMainMethod
 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've changed the code to


(so that it has a main method)

and the Classpath to java-cp D:\java\lib\ext\mysql-connector-java-5.0.6-bin.jar ConnectThingy and it still does not work. Have i made something wrong?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not a Java main method. A Java main method looks like
 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I see.
I wondered because in bluej main methods can be executed without creating a new object of that class and mine couldn't, thank you.

So now I changed the code to



but that still doesn't work. Is the classpath i've posted, right?

And what is this "javac *java" you mentioned? I must admit i don't understand what you meant with that.
 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took away the ".jar" in line 11 and no it executes for a few seconds and gives me this:


Verbindung ist fehlgeschlagen: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.ConnectException
MESSAGE: Connection timed out: connect

STACKTRACE:

java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:241)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:173)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:268)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2745)
at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at ConnectThingy.main(ConnectThingy.java:19)
at __SHELL3.run(__SHELL3.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at bluej.runtime.ExecServer$3.run(ExecServer.java:725)


** END NESTED EXCEPTION **



Last packet sent to the server was 32 ms ago.


At least it seems to find a driver now, so apparently that problem is solved.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a jar in line 11, but I'm glad you got past the classpath issue. (BlueJ masks some things about Java like the main method by the way.)

Now the driver is loading and can't connect. Are you able to connect to mysql at a command line? This will verify it is up. Can you ping the IP address?
 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ".jar" is in the driver name.

I can ping the computer with the MySQL-Server with 0% data loss and in 0ms, it is an almost direct LAN-connection so that should not be the problem.
 
Fratz Lloyd
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, everybody is going home now and I will do so, too. I will be checking on this thread somewhere tomorrow, you have been very helpfull, so far, thank you very much.

Bye and have a nice day!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic