I'm trying to make a connection to a database on my computer. The password to the database is root, and so is the user. I have connector jar file in my project library, I have 7.0 jre and jdk, the table "clients" in database "Testing1" exists, and has 1 entry with 3 fields,
+--------------------+
| Tables_in_Testing1 |
+--------------------+
| clients |
| esk |
| files |
+--------------------+
clients table:
+----+------------------+-----------------------+
| id | PublicKey | Email |
+----+------------------+-----------------------+
| 1 | PublicKeyNumber1 | FirstClient@email.com |
+----+------------------+-----------------------+
And here's the code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
class JDBCTest {
private static Connection con=null;
private static Statement st=null;
private static ResultSet rs=null;
public static void main(String args[])throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Testing1?user=root&password=root");
}catch (Exception e) {
System.out.println("e");
}
try{
st=con.createStatement();
rs=st.executeQuery("select * from clients");
while(rs.next()) {
System.out.println(rs.getString("Key"));
}
}
finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
}catch (SQLException e) {
System.out.println(e);
}
}
}
}
All of this returns an error(in Eclipse)
e
Exception in thread "main" java.lang.NullPointerException at JDBCTest.main(JDBCTest.java:22)
I assume that it's because there is no connection to the databasae, so con is null... but why?