I am trying to connect to a mysql database ... it is on the local machine ... i have downloaded the jar file and am using it while compiling.
i am using the Eclipse IDK and it does not let me compile saying "forName(String) not defined on line 16
Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbConnect {
public static void main(String[] args) {
Connection con = null;
String str = new String();
String ssn = new String();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance(); //***line 16
con = DriverManager.getConnection("jdbc:mysql:///records");
if(!con.isClosed())
System.out.println("Successfully connected to MySQL server...");
Statement stmt = con.createStatement() ;
str = "use records";
stmt.execute(str);
ssn = "000-00-0017";
str = "select ssn from ssn where sn = 4";
ResultSet rs = stmt.executeQuery(str);
while ( rs.next() ) {
System.out.println(rs.getString("ssn"));
}
//stmt.executeUpdate("INSERT INTO ssn " +
// "set ssn = \"000-00-0017\"" );
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
}
this is the stacktrace
java.lang.Error: Unresolved compilation problem:
The method forName(String) is undefined for the type Class
at DbConnect.main(DbConnect.java:16)
Exception in thread "main"