Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unable to run this program

 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9312
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the MySQL Connector/J documentation, you have to put the connector jar file in the classpath.
 
nash avin
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Thanks for the prompt information.It was silly of me wasnt it?.Will get in touch with more questions.
nash
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic