0

我做错了什么?

import java.sql.*;
public class DbConnect 
    private Connection con;
    private Statement st;
    private ResultSet rs;

public DbConnect() throws SQLException{
    try{
    Class.forName("com.mysql.jdbc.Driver");

    String url = "jdbc:mysql://localhost:220/test";
    String user = "UserNameHere";
    String password = "PasswordHere";
    con = DriverManager.getConnection(url, user, password);
    st = con.createStatement();
    }catch(Exception e){
    System.out.println("ERROR: " + e);
    }
}

public void getData(){
   try{
    String query = "SELECT * FROM persons";
    rs = st.executeQuery(query);
    System.out.println("Records from database:");

    while(rs.next()){

        String name = rs.getString("name");
        String age = rs.getString("age");

        System.out.println("Name: " + name + "   Age: " + age);
    }

     }catch(Exception e){
        System.out.println("ERROR: " + e);
     }
   }
}

我收到了这个错误:

错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

最后一个成功发送到服务器的数据包是 0 毫秒前。驱动程序没有收到来自服务器的任何数据包。

错误:java.lang.NullPointerException

4

0 回答 0