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