I want to connect to my database by using the java code below, but I got the following error:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

public static void main(String[] args) {
    try{
        Class.forName("com.mysql.jdbc.Driver");

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/nofoodleft?useSSL=false","root","123");
        Statement myStmt = conn.createStatement();
        ResultSet rs = myStmt.executeQuery("SELECT * FROM nofoodleft");
        while(rs.next()) {
            System.out.println(rs.getString("site"));
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

0 Answers