Skip to content
Advertisement

Unable to retrieve data from database through JDBC query

I trying to make a connection to my DDBB in order to print all the columns from a table but I keep getting Null.

I have checked the connection parameters and everything seems correct.

I’m thinking that maybe there is something wrong with my query statement:

public List<Palabra> getTodos() throws SQLException {
     SQLConexion con = new SQLConexion();
     listaPalabras = new ListaPalabras();
     
     if(con.ConectarBasedeDatos()) {
         try{
             Statement stmt = con.getConnection().createStatement();  
             ResultSet rs=stmt.executeQuery("SELECT * FROM PALABRA"); 
             while(rs.next()) {
                 Palabra pal = new Palabra(rs.getInt("idPalabra"), rs.getString("palabra"), rs.getInt("dificultad")); //These are the columns that I need to print.
                 listaPalabras.addPalabra(pal); //adding the results to the list
             }
         }
         catch(Exception e)
         { 
             System.out.println(e);
         }  
     }
     else {
         return null;
     }
     
     con.DesconectarBasedeDatos();

     return listaPalabras.getListaPalabras();
 }

Advertisement

Answer

I managed to solve the problem by adding the MySQL Connector to the libraries so indeed there was no connection with the database because of this.

Novice error.

Thanks a lot for your time.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement