Hi
This is my codings in order to access mysql database from java.
Codings:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MysqlConnect {
public static void main(String[] args)throws Exception{
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3309/";
String dbName = "ananthu";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "sa";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url+dbName,userName,pa ssword);
System.out.println();
System.out.println("Connected to the database");
System.out.println();
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("select * from customer");
System.out.println("Name\t\tAge");
System.out.println();
while(reset.next()){
//System.out.println(reset.getString(1));
//System.out.println(reset.getString(2));
System.out.println(reset.getString(1)+ "\t\t" + reset.getString(2));
}
conn.close();
System.out.println();
System.out.println("Disconnected from database");
} catch (java.lang.ClassNotFoundException e) {
e.printStackTrace();
}
}
}
I have mysqlconnector 5.0.7 jar file and i have placed it in program files folder.
The following is the execution of my codings.
Execution:
c:\program~1\java\jdk1.6.0_01\bin>javac -classpath ";.\program files\mysql-connector-java-5.6.0_01" mysqlconnect
c:\progra~1\java\jdk1.6.0_01\bin>java -classpath ";.\program files\mysql-connector-java-5.6.0_01" mysqlconnect
Exception in thread "main" java.lang.noclassdeffound error: mysqlconnect
-----
The above error occurs only during running the program and not in compilation time.
How to rectify the error and run the program successfully?
Please help me...