Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ClassNotFoundException: com.mysql.jdbc.Driver (from netbeans)

347 views
Skip to first unread message

thufir

unread,
Jul 2, 2008, 8:46:28 PM7/2/08
to
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

Lew

unread,
Jul 2, 2008, 9:47:56 PM7/2/08
to
thufir wrote:
> I'm almost positive that this is a classpath problem. However, why?
> Shouldn't "apt-get libmysql-java" set the classpath for netbeans?

No.

--
Lew

thufir

unread,
Jul 2, 2008, 10:06:14 PM7/2/08
to

Ok, so that's a class path problem 100% then?

thanks,

Thufir

thufir

unread,
Jul 2, 2008, 10:12:05 PM7/2/08
to
On Thu, 03 Jul 2008 00:46:28 +0000, thufir wrote:

> I'm following the manual at:
>
> https://help.ubuntu.com/community/JDBCAndMySQL

I changed to ODBC to be more in line with the Sun tutorial.

as per https://help.ubuntu.com/community/ODBC I configured ODBC for Java
(correctly, I think).

I didn't find much informatin on this error, so I'll probably revisit the
MySQL approach, which looked to be a classpath problem.


thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$
thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$ javac a00720398/
DBDemo.java
thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$
thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$ java a00720398.DBDemo
Exception in thread "main" java.sql.SQLException: [unixODBC][Driver
Manager]Data source name not found, and no default driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize
(JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at a00720398.DBDemo.main(DBDemo.java:20)
thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$
thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$ cat a00720398/
DBDemo.java
package a00720398;

import java.sql.*;
import java.util.Properties;

public class DBDemo {

private static final String CONNECTION =
"sun.jdbc.odbc.JdbcOdbcDriver";
private static final String URL = "jdbc:odbc:COFFEEBREAK";

public static void main(String[] args) throws ClassNotFoundException,
SQLException {

Class.forName(CONNECTION);

Properties p = new Properties();
p.put("user", "java");
p.put("password", "password");

//[unixODBC][Driver Manager]Data source name not found, and no
default driver specified
Connection c = DriverManager.getConnection(URL, p);

c.close();

System.out.println("It works !");
c.close();
}
}

thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$
thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$ cat /etc/odbc.ini
[ODBC Data Sources]
odbcname = MyODBC 3.51 Driver DSN

[odbcname]
Driver = /usr/lib/odbc/libmyodbc.so
Description = MyODBC 3.51 Driver DSN
SERVER = localhost
PORT =
USER = java
Password = password
Database = COFFEEBREAK
OPTION = 3
SOCKET =

[Default]
Driver = /usr/local/lib/libmyodbc3.so
Description = MyODBC 3.51 Driver DSN
SERVER = localhost
PORT =
USER = java
Password = password
Database = COFFEEBREAK
OPTION = 3
SOCKET =
thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$
thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$ mysql -u java -
ppassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.0.51a-3ubuntu5.1 (Ubuntu)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use COFFEEBREAK;
Database changed
mysql>
mysql> quit
Bye
thufir@arrakis:~/NetBeansProjects/ubuntu-jdbc/src$

thanks,

Thufir

thufir

unread,
Jul 2, 2008, 10:32:56 PM7/2/08
to
On Thu, 03 Jul 2008 02:06:14 +0000, thufir wrote:


> Ok, so that's a class path problem 100% then?


So, I opened the library manager for netbeans and added jar file for
mysql jdbc, then added that library to the project.

It runs, so I think that was that; thanks :)

(I'll have to create a table and stuff like that, but that's for later.)


-Thufir


Lew

unread,
Jul 3, 2008, 12:54:28 AM7/3/08
to
thufir wrote:
> I changed to ODBC to be more in line with the Sun tutorial.

Gods! Don't do that!

It's especially square-peg-in-a-round-holish in Linux.

Stick with direct JDBC drivers.

--
Lew

Sabine Dinis Blochberger

unread,
Jul 3, 2008, 4:39:59 AM7/3/08
to
thufir wrote:

> On Thu, 03 Jul 2008 02:06:14 +0000, thufir wrote:
>
> So, I opened the library manager for netbeans and added jar file for
> mysql jdbc, then added that library to the project.
>

And that is the way to do it, it will include the libraries with your
final jar - in a subdirectory named lib.

Note that in your Netbeans library, you can not have directories, only
.jars, otherwise it will refuse to include it in the final product.

Another advantage of using Netbeans library manager is that you get to
include the respective javadocs aswell, making it possible for Netbeans
to point you there.

--
Sabine Dinis Blochberger

Op3racional
www.op3racional.eu

0 new messages