Hi im trying to connect to my database but i dont know why i cant load my jdbc driver:
-I've saved the jar file together with the other java files in the same folder called "a"
-I used –classpath flag:
Windows: "java -classpath .\mysql-connector-java-3.1.8-bin.jar;. LabDB4"
but I dont know if this is right.
import java.sql.*;
public class a{
public static void main(String[] args) {
String sDriver = "com.mysql.jdbc.Driver";
Connection con = null;
Statement stmt = null;
String sURL = "jdbc:mysql://studdb-mysql.fos.auckland.ac.nz/<DATABASE_NAME>";
String sUsername = "<ID>"; // my ID
String sPassword = "<password>"; // my password
try { // Attempt to load the JDBC driver with newInstance
Class.forName(sDriver).newInstance();
}
catch( Exception e ) {
System.err.println("Failed to load current driver. ");
return;
} // end catch
try { //Connect to database
con = DriverManager.getConnection (sURL, sUsername, sPassword);
stmt = con.createStatement();
}
catch ( Exception e) {
System.err.println("Problems connecting to " + sURL + ":" );
System.err.println( e.getMessage() );
if( con != null) {
try { con.close(); }
catch( Exception e2 ) {}
}
return;
} // end catch
// my code for creating tables and performing queries will go here
// Disconnect
try { stmt.close(); }
catch( Exception e ) {}
try { con.close(); }
catch( Exception e ) {}
}
}
when i compile and run it it comes up with the error "Failed to load current driver" (java.lang.ClassNotFoundException: com.mysql.jdbc.Driver)
Please help when you can any help will be very much appreciated
Thanks