connection to java with mysql

1

Actually not so good JAJA, I have a problem with the connection from java to mysql and import the library the jar even though I made the connection and it tells me that the connection is not established.

I show you the connection code

package modelo.dao;
    import java.sql.*;


public class conexion  {

    static String bd = "merca_ruta";
    static String login = "root";
    static String password ="";
    static String url = "jdbc:mysql://localhost:/" + bd;


    Connection conn = null;



 public conexion (){
     try {
         // cargar nuestro driver
         Class.forName("com.mysql.jdbc.Driver");
         conn=DriverManager.getConnection("url, login, password");

         if (conn != null) {
             System.out.println("Conenecting database [" + conn + "] OK");
         }
     } catch (SQLException e) 
     {
         System.out.println("Exepcion conexion: " + e.getMessage());

        } catch (ClassNotFoundException e) 
        {
         System.out.println("Excepcion driver: " + e.getMessage());
        }
     }  

public Connection getConnection() {
    return conn;
}

public void disconnet(){
    System.out.println("closing database: ["+ conn + "] OK");
    if (conn !=null) {
        try {
            conn.close();
        } catch (SQLException e) {
            System.out.println(e);
        }
       }
    }
}

I hope you can help me thank you thank you.

    
asked by MOISES PINZON XIQUES 19.04.2018 в 17:38
source

2 answers

1

Your url is wrong jdbc:mysql://localhost:/ remove the second two points jdbc:mysql://localhost/ and also here are wrong:

conn=DriverManager.getConnection("url, login, password");

must be conn=DriverManager.getConnection(url, login, password);

    
answered by 19.04.2018 в 17:42
0

This can help you solve your problem.

public static Connection getConnection() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/nombre_bd",
                "root", "dbpass");
        return con;
    } catch (Exception ex) {
        System.out.println("Database.getConnection() Error -->" + ex.getMessage());
        return null;
    }
}

public static void close(Connection con) {
    try {
        con.close();
    } catch (Exception ex) {
    }
}
    
answered by 19.04.2018 в 17:54