This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes unable to run this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "unable to run this program" Watch "unable to run this program" New topic
Author

unable to run this program

nash avin
Ranch Hand

Joined: Nov 13, 2003
Posts: 96
hi all,
I am just trying to get a feel of connecting mysql to java and I am having a horrid time.The code is...
import java.sql.*;
//JDBCSample.java
import java.sql.*;
import java.io.*;
public class JDBCSample{
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (Exception e) {
e.printStackTrace();
System.out.println("Unable to load Driver Class");
return;
}

try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM pet");
while(rs.next()) {
System.out.println(rs.getString("name"));
}
rs.close();
stmt.close();
con.close();
}
catch (SQLException se) {
System.out.println("SQL Exception: " + se.getMessage());
}
} // End of main
} // End of class JDBCSample
The error I am getting is ....

/*-------------------------*/
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at JDBCSample.main(JDBCSample.java:17)
Unable to load Driver Class
/*--------------------------*/
Kindly let me know how to run the program
Thanking you
nash
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

According to the MySQL Connector/J documentation, you have to put the connector jar file in the classpath.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
nash avin
Ranch Hand

Joined: Nov 13, 2003
Posts: 96
Hello,
Thanks for the prompt information.It was silly of me wasnt it?.Will get in touch with more questions.
nash
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to run this program
 
Similar Threads
MySQL db - migrating from Access (but where is the driver?!)
Hibernate Dialect for MS Access
MySQL DataBase
problem with mysql jdbc connection
Unable to connect MYSQL :(