0

I was trying to connect a Java Web application to a MySQL database. I used this code to see if the connection was okay:

public class Funciones {
    public boolean communicationStatus(){ 
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/practicas","root","Contrasena1.");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("show databases;");
            System.out.println("Conectado");
            return true;
        }
        catch(Exception e){
            System.out.println(e);
            return false;
        }
    }   
}

And then I get this error: 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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Stack trace please. Neither Netbeans nor MySQL Workbench has anything to do with it. You are leaking `Connection` objects in this code. The `Class.forName()` line hasn't been needed since 2006. – user207421 May 04 '22 at 01:32
  • So if I delete the line with class.Name it should work? sorry is my first time seeing this in school, my teacher showed us the code with that line – AngelCZ May 04 '22 at 04:04
  • I didn't say that. I said it wasn't *needed.* I asked for the stack trace. Where is it? – user207421 May 04 '22 at 04:48

0 Answers0