Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
thufir - 03 Jul 2008 01:46 GMT
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 - 03 Jul 2008 02:47 GMT
> I'm almost positive that this is a classpath problem.  However, why?
> Shouldn't "apt-get libmysql-java" set the classpath for netbeans?

No.

Signature

Lew

thufir - 03 Jul 2008 03:06 GMT
>> I'm almost positive that this is a classpath problem.  However, why?
>> Shouldn't "apt-get libmysql-java" set the classpath for netbeans?
>
> No.

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

thanks,

Thufir
thufir - 03 Jul 2008 03:32 GMT
> 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
Sabine Dinis Blochberger - 03 Jul 2008 09:39 GMT
> 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.

Signature

Sabine Dinis Blochberger

Op3racional
www.op3racional.eu

thufir - 03 Jul 2008 03:12 GMT
> 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
Lew - 03 Jul 2008 05:54 GMT
> 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.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.