• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue First JDBC Pgm

 
Udhan Velayutham
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
This is my first JDBC Pgm...
I got the driver mysql driver and placed the same C Drive..
when i compile the java file it compiles fine.. but when i run the same I got an exception,

import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class LoadDriver {
public static void main(String[] args) {

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/employee?user=root&password=admin");
Statement stmt = conn.createStatement();
String query = "SELECT NAME FROM employee";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String s = rs.getString("COF_NAME");
float n = rs.getFloat("PRICE");
System.out.println(s + " " + n);
}

}
catch (Exception ex) {
// handle the error
}
}
}


C:\Program Files\jdk1.5.0_02\bin\uthand>javac -cp C:\mysql.jar LoadDriver.java

C:\Program Files\jdk1.5.0_02\bin\uthand>java -cp C:\mysql.jar LoadDriver
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver

Note ; I am having the driver jar in the c drive and using the same in the classpath too..

Thanks,
Udhan
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you can see in the error message, Java is not complaining about the driver jar file. It is saying that, it is unable to find LoadDriver class, the one which you have written.
If that file is in the current directory (C:\Program Files\jdk1.5.0_02\bin\uthand), include that directory also in the classpath, along with the C:\mysql.jar in the -cp option.
A shortcut to include the current directory in the classpath is to specify a '.' in the classpath

java -cp .;C:\mysql.jar LoadDriver

Try it out and see whether that works or not.
 
Udhan Velayutham
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mani.. Its works fine now... after adding the current directory ./ in the classpath..
Thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic