ClassNotFoundException: com.mysql.jdbc.Driver (from netbeans)
I'm almost positive that this is a classpath problem. However, why?
Shouldn't "apt-get libmysql-java" set the classpath for netbeans?
How do I know what the problem is?
When run, it's indicating that it's using com.mysql.jdbc.Driver, so
doesn't that indicate that the classpath is fine?
I'm following the manual at:
https://help.ubuntu.com/community/JDBCAndMySQL
Possibly it's because I'm running ubuntu 8, but this seems unlikely.
This example code is almost exactly as what I've seen elsewhere.
init:
deps-jar:
compile:
run:
com.mysql.jdbc.Driver
Exception in thread "main" 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:276)
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:169)
at a00720398.DBDemo.main(DBDemo.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
thufir@arrakis:~$ cat NetBeansProjects/ubuntu-jdbc/src/a00720398/
DBDemo.java
package a00720398;
import java.sql.*;
import java.util.Properties;
public class DBDemo
{
// The JDBC Connector Class.
private static final String dbClassName = "com.mysql.jdbc.Driver";
// Connection string. emotherearth is the database the program
// is connecting to. You can include user and password after this
// by adding (say) ?user=paulr&password=paulr. Not recommended!
private static final String CONNECTION =
"jdbc:mysql://127.0.0.1/emotherearth";
public static void main(String[] args) throws
ClassNotFoundException,SQLException
{
System.out.println(dbClassName);
// Class.forName(xxx) loads the jdbc classes and
// creates a drivermanager class factory
Class.forName(dbClassName);
//ClassNotFoundException:com.mysql.jdbc.Driver
// Properties for user and password. Here the user and password are
both 'paulr'
Properties p = new Properties();
p.put("user","java");
p.put("password","password");
// Now try to connect
Connection c = DriverManager.getConnection(CONNECTION,p);
System.out.println("It works !");
c.close();
}
}
thufir@arrakis:~$
thufir@arrakis:~$ ll /usr/share/java/mysql-connector-java.jar
lrwxrwxrwx 1 root root 30 2008-07-02 17:10 /usr/share/java/mysql-
connector-java.jar -> mysql-connector-java-5.1.5.jar
thufir@arrakis:~$
thufir@arrakis:~$ java -version
java version "1.6.0_06"
Java(TM) SE Runtime Environment (build 1.6.0_06-b02)
Java HotSpot(TM) Client VM (build 10.0-b22, mixed mode, sharing)
thufir@arrakis:~$
thufir@arrakis:~$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=8.04
DISTRIB_CODENAME=hardy
DISTRIB_DESCRIPTION="Ubuntu 8.04"
thufir@arrakis:~$
thanks,
Thufir