Connect to MYSQL server

4

I am creating a small application to connect to my local MySQL server and make a DATABASES SHOW so I can fill a ComboBox with all the databases there are and based on that, fill in another combo with all the tables of that BD.

But when I do the query, it returns a nullPointer because it seems that it is not connecting to my server.

attached connection and query code.

Conxion variables:

private static final String DDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306";

private static final String USER = "root";
private static final String PASS = "1234";

public static void conecta() throws ClassNotFoundException, SQLException {
    try {
        conexion = DriverManager.getConnection(DB_URL, USER, PASS);

        stmt = conexion.createStatement();


    } catch (SQLException se) {
        se.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Where I call the method

public void llenarComboBD() throws SQLException{
    MySQL.ejecutaConsulta("show databases");
    while(MySQL.getRs().next()){
        System.out.println(MySQL.getRs().getString(0));
    }
}

Method runConsultation

public static void ejecutaConsulta(String consulta) {
    try {
        rs = stmt.executeQuery(consulta);
        meta = rs.getMetaData();
    } catch (SQLException ex) {
        Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
    }
}

My question is, how can I connect to the server without having to select the BD, since I think it is giving me a nullPointer in the resultSet because a BD is not selected. The null pointer is given on the line: rs = stmt.executeQuery(consulta); of the method run query and I can not find a way to solve it.

Thanks in advance and greetings

SOLUTION

In the method where it filled the combo, it was not connecting to the DB, since it was a static class, no instance of the connection was happening to it since it does not need it because it is a static class, but if it needed connect, because I have entered the connection line in the method of filling the combo and already.

public void llenarComboBD() throws SQLException, ClassNotFoundException{
        MySQL.conecta("jdbc:mysql://localhost:3306/", "root", "1234");
        MySQL.ejecutaConsulta("show databases");
        ResultSet rs = MySQL.getRs();
        //MySQL.getMeta();

        while(rs.next()){
            //String bd = rs.getString(1);
            jcBD.add());
        }

}
    
asked by daviserraalonso 07.04.2018 в 21:10
source

1 answer

2

SOLUTION

In the method where it filled the combo, it was not connecting to the DB, since it was a static class, no instance of the connection was happening to it since it does not need it because it is a static class, but if it needed connect, because I have entered the connection line in the method of filling the combo and already.

public void llenarComboBD() throws SQLException, ClassNotFoundException{
        MySQL.conecta("jdbc:mysql://localhost:3306/", "root", "1234");
        MySQL.ejecutaConsulta("show databases");
        ResultSet rs = MySQL.getRs();
        //MySQL.getMeta();
    while(rs.next()){
        //String bd = rs.getString(1);
        jcBD.add());
    }

}

    
answered by 26.05.2018 / 13:04
source