0
votes

I trying to make a simple query and print the data from one column on my console but something is not right with my code.

I use the following code which is creating the connection and launching the query:

public List<Palabra> getTodos() throws SQLException {
     SQLConexion con = new SQLConexion();
     listaPalabras = new ListaPalabras();
     
     if(con.ConectarBasedeDatos()) {
         try{
             //Delegacion deg = null;
             // Si ya existe Base de Datos
             Statement stmt = con.getConnection().createStatement();  
             ResultSet rs=stmt.executeQuery("SELECT * FROM PALABRA");  
             while(rs.next()) {
                 // Recorro todas las palabras guardadas en persistencia
                 Palabra pal = new Palabra(rs.getInt("idPalabra"), rs.getString("palabra"), rs.getInt("dificultad"));
                 listaPalabras.addPalabra(pal);
             }
         }
         catch(Exception e)
         { 
             System.out.println(e);
         }  
     }
     else {
         return null;
     }
     
     con.DesconectarBasedeDatos();

     return listaPalabras.getListaPalabras();
 }

This is the MySQL table (I want to get the data from the column Palabra):

enter image description here

And this is the code that I'm using to execute the query:

SQLPalabraDAO query = new SQLPalabraDAO();
List<Palabra> palabras = query.getTodos();
System.out.println(palabras);

As you can see I'm using the DAO pattern, and every time that I execute the query, I'm getting the result Null.

I have checked the connection parameters and everything seems correct.

Sounds like con.ConectarBasedeDatos() returns false. Please provide a minimal reproducible example.Mark Rotteveel