aspose file tools*
The moose likes JDBC and the fly likes another JDBC driver question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "another JDBC driver question" Watch "another JDBC driver question" New topic
Author

another JDBC driver question

Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
I feel like there are a million of these posts, so I'll keep checking to see if I find what I am looking for. However, here is where I am stuck. I have downloaded the jdbc driver for mysql. Supposedly (this is coming from the sun tutorial), all you have to do is extract the jar file, and then point the CLASSPATH system variable to that same jar file.

Here is my CLASSPATH vairiable.

CLASSPATH=/home/suavecu/MySql/mysql-connector-java-3.1.12/mysql-connector-java-3.1.12-bin.jar


When I compile this code:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// Notice, do not import com.mysql.jdbc.*
// or you will have problems!

public class LoadDriver {
public static void main(String[] args) {

try {

// The newInstance() call is a work around for some
// broken Java implementations

Class.forName("com.mysql.jdbc.Driver").newInstance();

} catch (Exception ex) {
// handle the error
ex.printStackTrace();
}

try {

String url = "jddb:mysql:coffeebreak";
Connection conn = DriverManager.getConnection(url,"someuser","somepassword");

System.out.println("It worked!");


} catch (SQLException ex) {

System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());

}


}

}


it compiles fine, but when I run java LoadDriver I get this error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at LoadDriver.main(LoadDriver.java:16)
SQLException: No suitable driver
SQLState: 08001
VendorError: 0


I'm wondering if I went wrong somewhere simple.

A little system info. I'm running Mandriva 2006 linux and it came with MySQL 4.1. I downloaded the jdbc driver from the MySQL website.

As always, thanks for your help,

Nick
[ February 06, 2006: Message edited by: Nicholas Carrier ]

Teaching yourself anything is always the cheapest way, but it definitely takes a lot of time and effort.<br /> <br />Thank you javaranch <a href="http://"http://faq.javaranch.com/view?HowToAskQuestionsOnJavaRanch"" target="_blank" rel="nofollow">Learn How to Ask Your Question</a> and be nice
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

No, the classpath has to be able to point to all the classes you are compiling and/or running. So if you typed "javac LoadDriver.java" with the CLASSPATH variable you say you have, then the compiler would not be able to find your LoadDriver.java (because it isn't in that jar file). And you would get an error message saying so, not a clean compile. Likewise if you typed "java LoadDriver" with that CLASSPATH variable, you the ClassNotFoundException message would refer to LoadDriver.

But it doesn't. Therefore that CLASSPATH variable doesn't have anything to do with your problem. Whatever it contains is completely irrelevant and ignored.

Now I don't know much about Unix shells so I don't know why that would be the case. But I don't care because I'm going to advise you not to use it anyway. Use the "-cp" option at the command line to specify your classpath. And remember to put the current working directory into the classpath, so the compiler and JRE can find your LoadDriver class. Like this:
Nilesh Molankar
Greenhorn

Joined: Nov 13, 2005
Posts: 3
Hi,
I tired running the code snippet you put, and yes I did get the same error.
That is :
SQLException: No suitable driver
SQLState: 08001
VendorError: 0

I made the following changes to the code and it worked for me.
1. String url = "jddb:mysql:coffeebreak"; --> a typo it should be "jdbc:mysql:coffeebreak"
2. But even with this change it did not work. Got the same error message.
Then I tired this
//String url = "jdbc:mysql:coffeebreak";
String url = "jdbc:mysql://localhost/coffeebreak";
Connection conn = DriverManager.getConnection(url,"someuser","somepassword");
And this fixed it.

Hope this helps.

--Nilesh
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
See I exported my classpath and did get that error that LoadDriver not found and took it away because it confused the hell out of me. Ok, so with the classpath variable, you have to have all the diretories with all your classes in that variable for you to compile anything, correct?

Also, I'll have to try changing the jddb to jdbc when I get home, hopefully it works.

Thanks everyone,
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
Originally posted by Nicholas Carrier:
Ok, so with the classpath variable, you have to have all the diretories with all your classes in that variable for you to compile anything, correct?

The classpath is used at compile time to resolve external references, to things in jars, etc. AND at runtime, to actually use the things in jars.

However, an eternal reference created by the use of Class.forName() is a dynamic reference, not checkable at compile time, but only at runtime, because the referenced thing, the name of the class to load is "data" and not "program" at this point. This is by design, so that programs can be written and compiled independent of the JDBC driver that's going to be used when the program is deployed.
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
Aight, it was definitely a classpath problem. I added the file to my classpath, then added my JavaApps director to my classpath, fixed the two errors in the code, compiled it, ran it and it worked, kind of.

The error message that was plagueing me is now gone, although now it's a new one plaguing me, and unfortunately I don't think it java related (at least if it was java related I'd have a wonderfull community to help me).

Anyways, here is the error that I am getting:

SQLException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2555)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at LoadDriver.main(LoadDriver.java:26)


** END NESTED EXCEPTION **



Last packet sent to the server was 15 ms ago.
SQLState: 08S01
VendorError: 0


Now I'm assuming this error is coming from my database (as in my database is saying you can't get in, which is different than not being able to connect to it at all) and has nothing to do with Java anymore. Therefore, I have to find my answer in MySql.

Once again, thanks ahead of time,

Nick
graham king
Ranch Hand

Joined: Dec 30, 2004
Posts: 133
did ya create 'someuser', 'somepassword' in mysql?


write, revise, re-write, revise, again...<br />write, revise, re-write, revise, again...
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
well I have a different userID and pass than that (as in my real userID and pass) in my java file, but yes, the user ID has been created. I even tried to connect to it as root and got nothing.

Any ideas?
[ February 07, 2006: Message edited by: Nicholas Carrier ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

"Connection refused" is a network error message which means one of two things. Either the computer you tried to connect to (I don't see any code so I can't tell you what that was) does not exist, or there's no route to it from your computer. Or it does exist, and there's no server listening at the port you tried to connect to.

According to the documentation I scrounged up on the Internet, the JDBC URL must look like this (and yours in the original post didn't):

jdbc:mysql://<computer_name>/<database_name>

If you're running the server on your own computer then you would use "localhost" for the computer name. I also suspect you haven't got to the level of understanding where you have started the MySQL server running yet.
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
Originally posted by Nicholas Carrier:



Now I'm assuming this error is coming from my database (as in my database is saying you can't get in, which is different than not being able to connect to it at all) and has nothing to do with Java anymore. Therefore, I have to find my answer in MySql.


The problem is with Mandriva and the way MySQL is configured. It isn't binding to the IP.

There are two issues here
>The my.cnf
>The startup parameters sent to mysql when calling safe_mysqld from the /etc/init.d script

You have to edit my.cnf (probably in /etc or /etc/mysql)
add under [mysqld]
bind-address = 127.0.0.1
port = 3306

<please check the above, I'm typing from memory, but it could or could not require the "=" >

That will tell MySQL to bind to the loopback IP. Sometimes it works if you put the IP of one of your network cards. Example 10.1.23.234 or whatever.

Second issue is that mysql is starting with the ignore networking flag ON! So it isn't taking any requests through an IP stack. This is a Mandriva security setting and not a MySQL problem. It calls mysqld with --skip-networking.

If not mistaken you have to go to /etc/sysconfig (or something like that) and look for a file called mysqld. Edit it to remove the line with --skip-networking.

I'm sure you can find this if you read the first few lines of the /etc/init.d/mysqld script. It will be a variable called something like settings. There is a line just on top that says something like networking. Look for something like settings which is set to a path that looks something like "/etc/sysconf/mysqld" then go to that file and take out the --skip-networking. Restart MySQL and enjoy

You can also check by running netstat -ant and looking for a line with something like 0.0.0.0:3306. If you find that then MySQL is now bound to a local port and you can run your java app.
[ February 07, 2006: Message edited by: Gerardo Tasistro ]
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
I'm speechless.

It worked.

A million thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another JDBC driver question
 
Similar Threads
JDBC to connect to MYSQL
Problem with loading driver
sql exception thrown
how to setup mysql connector j to work in linux
do i need odbc to run java applications - reg connection