Retourner un Resultset par une fonction
salut tout le monde
j'ai un problème c'est que j'arrive pas a retourner un ResultSet par une fonction voila la portion du code :
classe AcceeMysal
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public static ResultSet chercher(){
ResultSet resultat=null;
try {
Connection conn = getConnection("connect.conf");//retourne les parametre de la connction
Statement stat = conn.createStatement();
resultat = stat.executeQuery("SELECT * FROM membres WHERE nom='abdessamad'");
stat.close();
conn.close();
}
catch (SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
return resultat;
} |
Apres je fais un appel depuis une classe :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
import java.sql.*;
class TestBase{
public static void main(String[] args) throws SQLException{
ResultSet r=AcceeMysql.chercher();
while(r.next())
System.out.println(r.getString("prenom"));
r.close();
}
} |
il m'affiche une erreur :
Operation not allowed after ResultSet closed
!!!!!plz help me
meme lorsque je tente d'affciher le ResultSet a l'interieur de la fonction et en dehors du bloc try ca marche pas
ce code retourne la meme erreur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
public static ResultSet chercher() throws SQLException{
ResultSet resultat=null;
try {
Connection conn = getConnection("connect.conf");
Statement stat = conn.createStatement();
resultat = stat.executeQuery("SELECT * FROM membres WHERE nom='abdessamad'");
stat.close();
conn.close();
}
catch (SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
while(resultat.next())
System.out.println(resultat.getString("prenom"));
resultat.close();
return resultat;
} |
mais celui ci-dessous marche très bien :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
public static ResultSet chercher(){
ResultSet resultat=null;
try {
Connection conn = getConnection("connect.conf");
Statement stat = conn.createStatement();
resultat = stat.executeQuery("SELECT * FROM membres WHERE nom='abdessamad'");
while(resultat.next())
System.out.println(resultat.getString("prenom"));
resultat.close();
stat.close();
conn.close();
}
catch (SQLException ex) {
while (ex != null) {
ex.printStackTrace();
ex = ex.getNextException();
}
}
catch (IOException ex) {
ex.printStackTrace();
}
return resultat;
} |
l'idée c'est de pouvoir effectuer un recherche dans la base de données est retourner un ResultSet afin d'en profiter pour remplir des champs
merci de votre aide