0

What have I done wrong?

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

I got this error:

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.

ERROR: java.lang.NullPointerException

Beryllium
  • 12,808
  • 10
  • 56
  • 86

0 Answers0