0

I've seen this question many times here but I couldn´t make my program work with any answer! The problem is basically that i can't connect to my XAMPP server with Eclipse.

String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql//localhost:80/glosario";
String user = "root";

public void conexion() {
    
    {
        try {
            
            Class.forName(driver);
            
            java.sql.Connection conexion = DriverManager.getConnection(url, user, "");
            
            if(conexion != null) {
                
                System.out.println("Conectado a la base de datos");
            }
            
        } catch (Exception e) {
            
            System.out.println("Error al conectarse a la base de datos");
            e.printStackTrace();
        }
    }
}

This is my code, I tried some recommended variations but neither of them worked for me. I have mysql connector in my classpath also. I am really noob in this area since I am just starting, this is a practice database but I can't even connect to it hehe. I also checked if my XAMPP connection is fine, and it seems that I can enter to the server, so I think the problem is not there. I have the feeling that im making some dumb mistake and I can't see it.

Any help would be very appreciated, thank you!

Oto
  • 83
  • 4

3 Answers3

0

You don't need the Class.forName line; it accomplishes nothing.

The mysql driver needs to be on the classpath as you run your code. If it is, you can connect. If it is not, you can't - the Class.forName line doesn't change the result either way.

You should post another question if the classpath isn't the issue. In general, getting an error and not pasting the full stacktrace is highly unlikely to lead to useful answers - paste the error next time.

rzwitserloot
  • 44,252
  • 4
  • 27
  • 37
0

Instead of com.mysql.cj.jdbc.Driver please try with com.mysql.jdbc.Driver

  • `com.mysql.cj.jdbc.Driver` is the correct class name of driver for Connector/J 8, says [the doc](https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html). – Basil Bourque Jul 14 '20 at 16:26
0

One issue is your url is missing a colon (see syntax here; example here). Try this:

String url = "jdbc:mysql://localhost:80/glosario";

If that doesn't work, there's this question, to which the present one seems perhaps a duplicate.

J Woodchuck
  • 2,265
  • 24
  • 44