com.mysql.jdbc.Driver not found
I copied a program for the web to connect to a database in java.
But there is some problem with specifying the jdbc driver in program.
/==============
Program
===================/
import java.sql.*;
public class RetriveAllEmployees{
public static void main(String[] args) {
System.out.println("Getting All Rows from employee table!");
Connection con = null;
String url = "jdbc:mysql://localhost:8080/";
String db = "sqlexamples";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db, user, pass);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM employee");
System.out.println("Employee Name: " );
while (res.next()) {
String employeeName = res.getString("emp_name");
System.out.println(employeeName );
}
con.close();
}
catch (ClassNotFoundException e){
System.err.println("Could not load JDBC driver"); System.out.println("Exception: " + e); e.printStackTrace(); }
catch(SQLException ex){
System.err.println("SQLException information");
while(ex!=null) { System.err.println ("Error msg: " + ex.getMessage());
System.err.println ("SQLSTATE: " + ex.getSQLState());
System.err.println ("Error code: " + ex.getErrorCode());
ex.printStackTrace();
ex = ex.getNextException(); // For drivers that support chained exceptions
}
}
}
}
==============================================
NO error in compilation,but when i run i got the following error.
===============================================
Station71:~/RESA/db# vim RetriveAllEmployees.java
Station71:~/RESA/db# javac RetriveAllEmployees.java
Station71:~/RESA/db# java RetriveAllEmployees
Getting All Rows from employee table!
Could not load JDBC driver
Exception: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
at java.net.URLClassLoader.findClass(libgcj.so.70)
at gnu.gcj.runtime.SystemClassLoader.findClass(libgcj.so.70)
at java.lang.ClassLoader.loadClass(libgcj.so.70)
at java.lang.ClassLoader.loadClass(libgcj.so.70)
at java.lang.Class.forName(libgcj.so.70)
at java.lang.Class.forName(libgcj.so.70)
at RetriveAllEmployees.main(RetriveAllEmployees.java:14)
==============================================
What should i do ?
THANX in advance
Last edited by Kakarot_Rathish; 03-09-2010 at 10:47 PM.
|