aspose file tools*
The moose likes JDBC and the fly likes Please help me with Java connection to MySql Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Please help me with Java connection to MySql" Watch "Please help me with Java connection to MySql" New topic
Author

Please help me with Java connection to MySql

juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
I try to connect to a Mysql database in java (on windows98) and I don't know what I am doing wrong (I am still more or less a agreenhorn to all this) - please help me!
I downloaded mm.mysql-2.0.4-bin.jar and unpacked it into
c:\mysql\
then I set the classpath in the autoexec.bat:
SET CLASSPATH=c:\mysql;%CLASSPATH%
My Java Code is compiling fine, but when I try to start the program,
I get the message:
Exception: No suitable driver
In my Java code the crucial lines looks like this:
String url = "jdbc:mysql://jultest";
...
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
...
Maybe my url is not correct; my database (named "jultest") is local on disk C.
I found something in the internet where they suggested:
"jdbc:mysql://localhost/yourdatabase";
but if I try this, I get an even longer error message.
Does anybody have an idea?
(Probably some very stupid and easy mistake of mine, but I just spent too many time in vain now, trying to fix it ...)

THANX A LOT!!
Hartmut Ludwig
Ranch Hand

Joined: Aug 31, 2002
Posts: 51
Hi, Juliane,
I would recommend to include the jar-file of your driver directly into the classpath. It should be something like mm.mysql-2.0.4-bin.jar or mysql.jar. This way you can exchange the driver more easily. Open a commandshell and type "set CLASSPATH" to find out if the jarfile is contained.
Originally posted by juliane gross:

In my Java code the crucial lines looks like this:
String url = "jdbc:mysql://jultest";
...
Maybe my url is not correct; my database (named "jultest") is local on disk C.

The connection URL is definitely wrong. It is missing the servername (in your case localhost).
Your URL should be something like:
"jdbc:mysql://localhost/jultest?user=me&password=secret";
if you want to connect as user "me" with password "secret". You should create a user and password to access the database - even for testing purpose.

I found something in the internet where they suggested:
"jdbc:mysql://localhost/yourdatabase";
but if I try this, I get an even longer error message.

So what you found on the internet is absolutely correct... What error do you get?
Perhaps a Security-Error? This could mean that your SDK's security policy is not configured to allow access to the database.
hope it helps
Hartmut
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1199
Julie, I think the driver is located inside of mysql-connector-java-xxx, and you need to place the bin.jar file located inside that file in your classpath. My classpath reads like this:
C:\mysql-connector-java-2.0.14\mysql-connector-java-2.0.14-bin.jar;
Hope this helps.
I find this classpath stuff very frustrating, because I really don't have a grasp on what it is exactly, or how it works. The concept I understand, not the mechanics.
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
thank you for your effort and time, Hartmut and Carol.
I don't know whether I got any nearer to solving the problem -
at least, my error messages keep changing!

I changed my url to:
String url = "jdbc:mysql://localhost/jultest";
and my classpath to:
SET CLASSPATH=c:\mysql\mm.mysql-2.0.4-bin.jar;
after this I at least got a different error message:
java.lang.NoClassDefFoundError: Dbtest
(Dbtest being the name of my java program).
Then, I found some help text saying:
"Note: On Windows systems make sure that either file and directory names in the CLASSPATH
are no longer than 8.3 or that they use the mangled names, or that the entire CLASSPATH
is surrounded by quotes. Otherwise, the Java runtime will not be able to find the classes."
well, I don't know what "mangled names" are, and the author forgot to tell
HOW to surround CLASSPATH by quotes exactly.
I do see that mm.mysql-2.0.4-bin.jar is longer
than 8.3.
I tried a version (analog to other examples I found somewhere else):
SET CLASSPATH=c:\mysql\mm-mysql\mm.mysql-2.0.4-bin.jar;%CLASSPATH%
that brought me yet another error message:
"Exception. Cannot connect to MySQL server on localhost:3306.
Is there a MySQL server running on the machine/port you are trying to connect to?
(java.net.ConnectException)"
I tried different versions of above CLASSPATH, but now the last error message
sticks with me.
I will be very happy if somebody can tell me how to get it right!
Juliane
Hartmut Ludwig
Ranch Hand

Joined: Aug 31, 2002
Posts: 51
Originally posted by juliane gross:
I changed my url to:
String url = "jdbc:mysql://localhost/jultest";
and my classpath to:
SET CLASSPATH=c:\mysql\mm.mysql-2.0.4-bin.jar;
after this I at least got a different error message:
java.lang.NoClassDefFoundError: Dbtest

The problem here is that the class Dbtest itself is not (or no longer) part of the CLASSPATH.
If you call the class from the directory where it resides you need to add . to your CLASSPATH. This is the reference to the current directory.
Now the Java runtime will not only search the CLASSPATH to locate classes, but also the current directory from where it has been called.
So your SET command could look something like:
SET CLASSPATH=.;c:\mysql\mm.mysql-2.0.4-bin.jar
but further down you will see an even better solution for the CLASSPATH issue.

well, I don't know what "mangled names" are, and the author forgot to tell


Hmm. I am not really shure, but I think this only applies to Windows NT or perhaps Windows 95.
Anyway - mangled names are the short version of long filenames. Since Windows is based on DOS which has a 8.3 Filename specification they had to find a compatibility solution when they first introduced the long filenames. The secret is: The file is stored with a short name, but the user can work with long names as well and the system translates it internally into short names. If you call "dir" on a command line in Windows 98 you see the mangled names on the left and the long names to the right.
However. Java does not now anything about this platform specific issue - translating filenames .... so on some "early" versions of this weird short/longname confusion you had to use the mangled names in the CLASSPATH. But on Windows 98 you don't need to do that. Anyway - to make it simple you could just rename your jar-file to a name that is 8.3 (e.g. mysql.jar) and everything will be fine. But again: That's not the reason for your trouble. The reason is that the java runtime was not able to find the class Dbtest itself.

I tried a version (analog to other examples I found somewhere else):
SET CLASSPATH=c:\mysql\mm-mysql\mm.mysql-2.0.4-bin.jar;%CLASSPATH%

Good idea. This is another solution to the NoClassDefFoundError. It takes the old classpath (represented by %CLASSPATH%) and adds the mysql jarfile in front of it. Since the directory where you called Dbtest was included in the old CLASSPATH you no longer get the NoClassDefFoundError. I would recommend that you continue using this CLASSPATH definition.

that brought me yet another error message:
"Exception. Cannot connect to MySQL server on localhost:3306.
Is there a MySQL server running on the machine/port you are trying to connect to?
(java.net.ConnectException)"

Aaaah. Here we come much closer to the real problem. But it still could have several reasons.
It could be a problem with the user and password.
Have you configured users on mysql properly?
Best is you create a user for testing the database (see mysql manual) and give this user a password.
Try to login to MySQL on the command line with the username and password you created. Now you can use the connect-url like shown in my previous posting.
If it still doesn't work it could be a problem with the java security policy (access denied).
To check this you should have a closer look (and maybe post us) the stack trace of your Exception. Use:
e.printStackTrace(System.out);
where e is the Exception you caught.
sl
Hartmut
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
Hartmut, I just found your last reply.
I need to wait for the weekend to try out your
suggestions, but I am completely impressed about
you taking your time for my problem.
I would like to pay you a beer or two, or sent
you some chocolate if the internet would
allow this
well - thank you very much. I feel this (your answer) was special!
and I feel much more confident to be solving
the problem, on the grounds of your analysis, soon.
thanks again,
Juliane
synapticfailure
Greenhorn

Joined: Sep 15, 2002
Posts: 1
having same probems and i have the same setup.
i changed that line to this:
Class.forName("java.sql.Driver");
i compiled fine.
i used this classpath:
SET CLASSPATH=.;c:\mysql\mm.mysql-2.0.4-bin.jar
as hartmut suggested.

i am doing this for a class, i have to make a small applet that will connect to a mysql database and be able to retrieve a list of contacts i.e. names, addresses, phones and stuff.
the problem i get is when i add this line of code:
Connection c = DriverManager.getConnection(dbUrl,user,password);
my url being: "jdbc:MySql://localhost/people";
i know user and password are okay too.
the error i get is:
$java lookup
exception in thread 'main' java.sql.sqlException:no suitable driver at
java.sql.DriverManager.getConnection(DriverManager.java:532)
as java.sql.DriverManager.getConnection(DriverManager.java:171)
at Lookup.main(Lookup.java:12)

my class is Lookup.class.
i didnt start another thread cuz my problem is exactly as the one this is about. this thread has helped me out a lot already so please respond. thanks!
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
Hartmut,
just wanted to let you know that your advice worked fine: it was a problem with my user configuration (I had no password). Now the database connection works - by the way: both classpath settings you proposed are working fine as soon as I had a password. (starting with .;c\ ... as well as the other
one ending with %CLASSPATH%).
THANX AGAIN,
Juliane
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
Hi synopticfailure,
on javaranch there is a naming policy, you should
use your real name. Please log in again accordingly.
And since I finished this thread it might
be more useful to you if you start your own now.
My code line for loading the driver is:
Class.forName( "org.gjt.mm.mysql.Driver" ).newInstance();
con = DriverManager.getConnection( url , username , password );
Juliane
Hartmut Ludwig
Ranch Hand

Joined: Aug 31, 2002
Posts: 51
Originally posted by synapticfailure:
i used this classpath:
SET CLASSPATH=.;c:\mysql\mm.mysql-2.0.4-bin.jar
as hartmut suggested.

And did you check that you use exactly mm.mysql-2.0.4-bin.jar, and not a newer or older version? And did you check that it resides in C:\mysql?
the error i get is:
exception in thread 'main' java.sql.sqlException:no suitable driver at
java.sql.DriverManager.getConnection(DriverManager.java:532)

Well. Looks like you definitely don't have the jar in your classpath (type set CLASSPATH in a DOS-Box). Does the jar have the name and location where it is supposed to be?
sl
Hartmut
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please help me with Java connection to MySql