2

I created a class called BD in which i try to establish a connection with a database called bono

public class BD {
String url,login,password,driver;
Connection conexion=null;

public BD(){
    driver = "com.mysql.jdbc.Driver";
    url = new String("jdbc:mysql://localhost:8000/bono");
    login = new String("root");
    password = new String("mypassword");
    try {
        Class.forName(driver);
        conexion = DriverManager.getConnection(url, login, password);
        System.out.println("Conexi�n con Base de datos Ok....");
    } catch (ClassNotFoundException e) { // 
        System.out.println("error 1");
    } catch (SQLException e) {
        System.out.println("error 2");
    }
}

but when i execute it, it just stays in this following line:

conexion = DriverManager.getConnection(url, login, password);

it doesn't send any errors nor does it establish the connection. What should i do?

afzalex
  • 8,598
  • 2
  • 34
  • 61

2 Answers2

2

try changing your port to 3306.

public class BD {
String url,login,password,driver;
Connection conexion=null;

public BD(){
    driver = "com.mysql.jdbc.Driver";
    url = new String("jdbc:mysql://localhost:3306/bono");
    login = new String("root");
    password = new String("mypassword");
    try {
        Class.forName(driver);
        conexion = DriverManager.getConnection(url, login, password);
        System.out.println("Conexi�n con Base de datos Ok....");
    } catch (ClassNotFoundException e) { // 
        System.out.println("error 1");
        e.printStackTrace();
    } catch (SQLException e) {
        System.out.println("error 2");
        e.printStackTrace();
    }
}

Hope it helps.

Note: be sure that MySQL is running. Try something like: mysqladmin -u root -p status from your console, there shouldn't display any error

OscarBcn
  • 626
  • 4
  • 11
2

i think your port maybe incorrect: try this code hopefully at least you see some exception

public class BD {
String url,login,password,driver;
Connection conexion=null;

public BD(){
    driver = "com.mysql.jdbc.Driver";
    url = new String("jdbc:mysql://127.0.0.1:3306/bono");
    login = new String("root");
    password = new String("mypassword");
    try {
        Class.forName(driver);
        conexion = DriverManager.getConnection(url, login, password);
        System.out.println("Ok....");
    } catch (ClassNotFoundException e) { // 
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
Bilal
  • 138
  • 1
  • 8