I have '.' in my classpath. In the directory containing both the .java
and .jar file, I type:
# javac SQLTest.java -classpath mysql-connector-java-2.0.14-bin.jar
It compiles fine. However, when I try to run it using the command line:
# java SQLTest -classpath mysql-connector-java-2.0.14-bin.jar
I get the following error:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
...
at SQLTest.<init>(SQLTest.java:35)
at SQLTest.main(SQLTest.java:65)
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
...
at SQLTest.<init>(SQLTest.java:43)
at SQLTest.main(SQLTest.java:65)
Am I typing this correctly? How do I get database connectivity with my
application?
Mark McKay
Check if mysql-connector-java-2.0.14-bin.jar does contain com.mysql.jdbc.Driver
Hwee Boon
Did you register the driver in the code...
import driverclass.*;
Class.forname("mysqldriver");
BB
I'm including my code below (passwords have been changed).
com.mysql.jdbc.Driver does exist in the jar. It's also being found
during compile since the second line doesn't cause the compilation to
fail. However, running it via 'java' from the command line can't find
the class.
import java.sql.*;
import com.mysql.jdbc.Driver;
public class SQLTest {
/** Creates a new instance of SQLTest */
public SQLTest() {
String dbUrl;
dbUrl = "jdbc:mysql://localhost:3306/myDb";
Connection con;
String qry;
qry = "SELECT * FROM region;";
Statement stmt;
ResultSet rs;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(Exception e) {
e.printStackTrace();
}
try {
// here i get the exception
con = DriverManager.getConnection(dbUrl, "myLogin", "myPw");
stmt = con.createStatement();
rs = stmt.executeQuery(qry);
while (rs.next())
{
String s = rs.getString("title");
System.err.println("Qry: " + s);
}
stmt.close();
con.close();
}
catch(SQLException ex) {
ex.printStackTrace();
}
}
static public void main(String args[])
{
SQLTest test = new SQLTest();
}
}
I somehow managed to run some sample codes in java tutorial in jdbc
but most have problems with "ClassNotFoundException: org.... com....
SQLexception: No suitable Driver", whichever driver (com or org
version) is used. I never had a luck with com version yet.
I suggest you to copy mysql-connector-java-2.0.14.jar file to
$java_home$/jre/lib/ext directory, so that you don't have to import
the driver (remove line: import com.mysql.jdbc.Driver;), or set
classpath (i.e. using jar as extension). Also remove newInstance()
method in the first try statement body. According to sun's java
tutorial, no need to do create another copy of instance as
Class.forName("com.mysql.jdbc.Driver") will automatically instantiate.
I also tried 3.0 beta jar version but the same problem persists.
Anyone else having the same problem? Help!
http://www.yoda.arachsys.com/java/extensions.html
and find more info about extension mechanism at Sun's Java Tutorial:
http://java.sun.com/docs/books/tutorial/ext/index.html
:>