JDBC Driver Problem
Hi everyone I have a problem I want to use a database for a project I'm working on using MySql I've created the database populated the tables and all that I also downloaded the JDBC Driver from the MySql website called connector J I've changed my Classpath to what it says in the installationexport set CLASSPATH=/path/mysql-connector-java-[ver]-bin.jar:$CLASSPATH
I then created a small test program to make sure it connects here is the code for thatimport java.sql.*;
import javax.sql.*;
publicclass SimpleJdbc{
publicstaticvoid main(String[] args)
throws SQLException, ClassNotFoundException{
//Load JDBC Driver
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded");
//Establish a Connection
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost/new_test");
System.out.println("Database Connected");
//Create a Statement
Statement statement = connection.createStatement();
//Execute a Statement
ResultSet resultSet = statement.executeQuery
("select firstName, mi, lastName from Student where lastName "
+" = 'Smith'");
//Iterate through the result and print the student names
while(resultSet.next())
System.out.println(resultSet.getString(1) +"\t" +
resultSet.getString(2) +"\t" + resultSet.getString(3));
//Close Connection
connection.close();
}
}
When I run this I get a Class Not Found Exception for the com.mysql.jdbc.Driver
I'm working on this on a mac osx 10.4.9 powerpc I would like to know why I cannnot connect ? If anyone can be of assistance it would be appreciated. Thank you
[2512 byte] By [
devika78a] at [2007-11-27 5:38:36]

# 1
How are you executing this snippet? The classpath used depends on the running JVM used.
# 2
Devika
Use this statements
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/DSN", "username", "password");
here DSN is which u have created for connecting to ur mysql
i hope this might serve ur purpose and if u still face any problem plz let me know.
cheers
Sandy
# 3
Hi Devika
I have gone through your code,few things are missing like try and catch block.
I am attcahing code with some changes ,you try to run, it will run as i tried it on my machine..
import java.sql.*;
public class SimpleJdbc {
public static void main(String[] args)
throws SQLException, ClassNotFoundException{
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver Loaded");
Connection connection = DriverManager.getConnection
("jdbc:mysql://localhost/new_test");
System.out.println("Database Connected");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery
("select firstName, mi, lastName from Student where lastName "
+ " = 'Smith'");
while(resultSet.next())
System.out.println(resultSet.getString(1) + "\t" +
resultSet.getString(2) + "\t" + resultSet.getString(3));
connection.close();
}
catch(ClassNotFoundException cnfe){
System.out.println(cnfe.getMessage());
}
}
}
Hope you must be happy now.
Cheers
Sandy
# 4
It might be because of improper connecter version. Please chek it out the mysql connecter version!!
# 5
> Use this statements
> Class.forName("com.mysql.jdbc.Driver").newInstance();
> It might be because of improper connecter version.
> Please chek it out the mysql connecter version!!
Do you both really think that it solves the CNFE problem? First understand why CNFE is being thrown instead of suggesting solutions that are for far beyond that point.
# 6
Thanks for the responses I have tried some of what you say with no successes so far I'm currently running this version of java and the java VM java version "1.5.0_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-164)
Java HotSpot(TM) Client VM (build 1.5.0_07-87, mixed mode, sharing)
the thing i notice since I'm using eclipse when I compile and run the code in the console it states that the program terminates in the folder /system/library/frameworks/javaVM.frameworks/versions/1.4.2/
I'm wondering if that could be a problem since it says I have 1.5.0 installed also here is the error messages I get when it runs Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at SimpleJdbc.main(SimpleJdbc.java:9)
again thank you for the assistance
# 7
> Thanks for the responses I have tried some of what
> you say with no successes so far
Let me try and clear this up for you.
sandysharma's advice was useless. Balus's was correct.
The problem is that your driver is not in your classpath. You need to add the jar with the driver to your runtime classpath.
# 8
I set up the classpath already thats the problem this is what i have in my classpath in my . profile file # Your previous .profile (if any) is saved as .profile.dpsaved
# Setting the path for DarwinPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export set CLASSPATH=/path/mysql-connector-java-[5.0.6]-bin.jar:$CLASSPATH
just as the installation file said I even went into eclipse and set a classpath variable pointing to the folder where the connector J is thats why I can't understand whats wrong I followed the instructions, googled it and no luck
# 9
Hii want common Database accssing code.
# 10
> I set up the classpath already thats the problem this
> is what i have in my classpath in my . profile file
> # Your previous .profile (if any) is saved as
> .profile.dpsaved
> # Setting the path for DarwinPorts.
> export PATH=/opt/local/bin:/opt/local/sbin:$PATH
>
> export set
> CLASSPATH=/path/mysql-connector-java-[5.0.6]-bin.jar:$
> CLASSPATH
>
just as the installation file said I even
> went into eclipse and set a classpath variable
> pointing to the folder where the connector J is thats
> why I can't understand whats wrong I followed the
> instructions, googled it and no luck
Okay first of all environment classpaths are a bad idea. Second you can get around this and fix your current problem by using a classpath at runtime like this.
java -cp .:/path/mysql-connector-java-[5.0.6]-bin.jar YourClassNameHere
If that doesn't work then the path to the mysql jar is incorrect.
# 11
> Hi> i want common Database accssing code.Good for you. I don't care. Start your own thread. Don't hijack someone else's.
# 12
Thank you cotton.m very much you were right the path to the jar file was wrong and it connects to the database in the command line, now i have to figure out how to get it to connect in eclipse but thanks again all the help.
# 13
> Thank you cotton.m very much you were right the path
> to the jar file was wrong and it connects to the
> database in the command line, now i have to figure
> out how to get it to connect in eclipse but thanks
> again all the help.
There would be some setting in eclipse for adding jars and other libraries. Although I dont know what it would be.
# 14
just open the zip file for mysql driver and place it under ur current working directory( that is in which the java file which access the databse code is wriiten)