I am extremely new to Java, and we were given lines of code to connect our project to our database on phpmyadmin. When I try to run the code, it says:
com.mysql.cj.jdbc.exceptions.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.
This is the code:
String email = jTextFieldEmail.getText();
String password = jTextFieldPassword.getText();
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/cis2103","root","");
Statement stmt=con.createStatement();
String sqlCode = "select * from users where "
+ "email='"+email+"' and password='"+password+"'";
System.out.println(sqlCode);
ResultSet rs=stmt.executeQuery(sqlCode);
rs.next();
if((rs.getString("Email")).compareTo(email)==0 &&
(rs.getString("Password")).compareTo(password)==0)
{
this.setVisible(false);
anothercalcu c = new anothercalcu();
c.setVisible(true);
}
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
Here are the things I tried doing:
- restarted MySQL
- changed 3306 to 8080
- changed localhost to 172.0.0.1
- turned off my firewall
- added &autoReconnect=true&failOverReadOnly=false&maxReconnects=10 to the connection string
- and a lot more...
Additional info: we use XAMPP and i have a mysql connector jar file added to the library