0

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?

deer deer
  • 49
  • 5

2 Answers2

0

You replace System.out.println("e") with e.printStackTrace(), you will get the detailed Exception when create connection.
It could be ClassNotFoundException.

And, if it is password not right exception, replace

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Testing1?user=root&password=root");

With

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Testing1", "root, "root");
sanigo
  • 625
  • 4
  • 14
  • After replacing System.out.println("e") with e.printStackTrace() I still get the same error, unfortunately. – deer deer Nov 06 '14 at 01:16
  • I meant, you can get the detailed Exception, so we can know how to solve the problem. – sanigo Nov 06 '14 at 01:19
  • Uhhh, I don't know what you mean. Replacing it makes no difference at all. Am I making some dumb rookie mistake? – deer deer Nov 06 '14 at 01:28
  • Still get "e" in output? Did you re-compile? – sanigo Nov 06 '14 at 01:32
  • Yeah I have ClassNotFoundException now. I added this export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar to my bash.bashrc file. Is it wrong? – deer deer Nov 06 '14 at 01:53
0

I have tried your code and it works for me, just made some changes in url. Also print exception when getting a connection.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public 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 users");
            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);
            }
        }
    }
}
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • Which means that there is no database like mine or is there some other error? – deer deer Nov 06 '14 at 01:29
  • @deerdeer just run above program, and let share the output – Ankur Singhal Nov 06 '14 at 01:36
  • Now it shows: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver – deer deer Nov 06 '14 at 01:42
  • @deerdeer so now you got the issue, you do not have the jar present in your classpath – Ankur Singhal Nov 06 '14 at 01:44
  • java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Exception in thread "main" java.lang.NullPointerException at JDBCTest.main(JDBCTest.java:20) – deer deer Nov 06 '14 at 01:45
  • @deerdeer yes i got your exception, it is class not found, since you are catching it so it is flow is moving ahead giving null pointer. refer [link1](http://stackoverflow.com/questions/2591505/java-lang-classnotfoundexception-com-mysql-jdbc-driver) and [link2](http://stackoverflow.com/questions/17484764/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in-eclipse) – Ankur Singhal Nov 06 '14 at 01:46
  • Okay, I added this export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar to my bash.bashrc file. Is it wrong? – deer deer Nov 06 '14 at 01:46
  • @deerdeer did you add it to your project, is it working – Ankur Singhal Nov 06 '14 at 01:56
  • Apparently I did not add it, I could swear I did! Thank you so much, it works now. I'm so incredibly happy, holy crap. Thank you thank you thank you! – deer deer Nov 06 '14 at 02:01