简体   繁体   中英

MySQL connection with JDBC

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?

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");

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);
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM