0

I am currently creating a library (with books I mean), I stock books in a database (JDBC)(I don't have any problems with it) and I display these books in a Jtable.

But I have a big problem in my code with a function that get column and return it:

public ArrayList<Livre> getTable(){

    /*          Recupere toute la table de la base de données           */

    ArrayList<Livre> livres = new ArrayList<Livre>();
    try {

        //Création d'un objet Statement
        Statement state = conn.createStatement();
        //L'objet ResultSet contient le résultat de la requête SQL
        ResultSet result = state.executeQuery("SELECT * FROM `livres`");
        //On récupère les MetaData
        ResultSetMetaData resultMeta = result.getMetaData();

        while (result.next()) {

            int     id                  = (int)result.getObject(1);
            String  titre               = (String)result.getObject(2);
            String  auteur              = (String)result.getObject(3);
            int     page                = (int)result.getObject(4);
            String  resume              = (String)result.getObject(5) != null ? (String)result.getObject(5) : "Non précisé";
            int     tome                = (Integer)result.getObject(6) != null ? (Integer)result.getObject(6) : 0;
            String  parution            = (String)result.getObject(7);
            String  editeur             = (String)result.getObject(8);
            String  collection          = (String)result.getObject(9);
            String  LangueDeParution    = (String)result.getObject(10);
            String  Titreoriginal       = (String)result.getObject(11) != null ? (String)result.getObject(11) : "Non précisé";
            String  LangueOriginal      = (String)result.getObject(12) != null ? (String)result.getObject(12) : "Non précisé";

            Livre o = new Livre(id,titre,auteur,page,resume,tome,parution,editeur,collection,LangueDeParution,Titreoriginal,LangueOriginal);

            livres.add(o);

            System.out.println(livres.get(livres.size()-1).getAuteur()); // print each Autors
        }

        System.out.println("\n++++++++++++++++++++\n");
        // print each autors
        for(Livre livre : livres) {
            System.out.println(livre.getAuteur());
        }

        result.close();
        state.close();

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


    return livres;

}

It returns (I have two books at the moment, just for tests):

Beschrelle P. D. James

++++++++++++++++++++

P. D. James P. D. James

I don't know why the first element copy the second...

Note: - I'm French and the commentaries are in French - Livre() (means book) is a class that I have created to simplify the characteristics like number of pages, title etc

0

2 Answers 2

3

You call livres.get(livres.size()-1) instead of livres.get(i):

for(int i = 0; i < livres.size(); i++)
    System.out.println(livres.get(i).getAuteur());

Another way to write this in a way that makes it less likely to make that mistake is the for-each loop:

for(Livre livre : livres) {
    System.out.println(livre.getAuteur());
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are not using the increment counter in your for loop, so you are printing each time the same value :

for(int i = 0; i < livres.size(); i++)
            System.out.println(livres.get(livres.size()-1).getAuteur());

Change your print to this one :

for(int i = 0; i < livres.size(); i++)
            System.out.println(livres.get(i).getAuteur());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.