Domanda

I want to display my record in the consol of netbeans. I have two Column: "naam" and "AantalTespelenRondes". I want to display them + the records of the database. Here is my code of connecting to the database through code.

I work with a List where it saves the data of the database. But when I exucute I get this

[domein.Spel@bddc6, domein.Spel@b0cf230]

And that are not the records. How can I show the records + column names in the consol of netbeans

private final static String LEES_SPELERS_SQL = "SELECT Naam, AantalTeSpelenRondes FROM Spel";

  public List<Spel> geefSpel()
{
    List<Spel> spelLijst = new ArrayList<Spel>();
    //create Statement for querying database
    Statement statement;
    Connection connection = PersistentieController.getInstance().getConnection();
    try
    {
        statement = connection.createStatement();

        // query database
        ResultSet resultSet = statement.executeQuery(LEES_SPELERS_SQL);

        while (resultSet.next())
        {

            String Naam = resultSet.getString("Naam");
            int AantalRondes = resultSet.getInt("AantalTeSpelenRondes");


               Spel spel = new Spel(Naam,AantalRondes);
               spelLijst.add(spel);


        }
        statement.close();
       // return Spel;
    } catch (SQLException e)
    {
        e.printStackTrace();
    }

    return spelLijst;
È stato utile?

Soluzione

I work with a List where it saves the data of the database. But when I exucute I get this

[domein.Spel@bddc6, domein.Spel@b0cf230]

You're currently printing the Spel object reference and it will use toString method. Since you haven't overridden it, it will use Object#toString.

You have two approaches to solve this problem:

  • Override toString method in Spel class:

    @Override
    public String toString() {
        //assuming this class has Naam and aantalRondes fields
        //replace by the real fields in your class
        return "{ Naam: " + naam + ", AantalRondes: " + aantalRondes + "}";
    }
    
  • Go through every element in the List and manually print the desired data:

    for (Spel spel : spelLijst) {
        //use the format you want/need
        System.out.println(spel.getNaam() + " " + spel.getAantalRondes());
    }
    
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top