I decided to try to connect a java client to a mysql db running on wamp. Can anybody tell me why im getting this runtime problem?
My DBConnect() class
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbConnect {
private Connection conn;
private Statement statement;
private ResultSet resultset;
public DbConnect(){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://xxx.xxx.xxx.xxx:3306/pk","root","");
} catch (Exception e){
System.out.println("Error: " + e);
}
}
public String getData(){
String result ="";
try {
resultset = statement.executeQuery("select * from data_user");
while(resultset.next())
{
result+=resultset.getString("userFirstName");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
My main() class
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
DbConnect conn = new DbConnect();
System.out.println(conn.getData());
}
My error at runtime
Error: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Exception in thread "main" java.lang.NullPointerException at DbConnect.getData(DbConnect.java:26) at Main.main(Main.java:8) ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [util.c:840]
}
Offending line
resultset = statement.executeQuery("select * from data_user");