Bonjour,
J'ai des exceptions lorsque j’exécute le code suivant code.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public User getUserByName(Connection connection, String name) { String sql = "SELECT * FROM user WHERE nom=? "; User user = new User(); try { PreparedStatement call = connection.prepareStatement(sql); call.setString(1, name); // retourn 0 si pas de résultats if (call.executeUpdate() != 0) { System.out.println("User trouvé"); ResultSet result = call.getResultSet(); while (result.next()) { user.setId(result.getInt(1)); user.setName(result.getString("nom")); } call.close(); return user; } } catch (SQLException e) { e.printStackTrace(); } System.out.println("Secteur NON trouvé"); return null; }
et l'erreur :
java.sql.SQLException: Can not issue executeUpdate() for SELECTs
Secteur NON trouvé
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2320)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
etc ...
je cherche simplement voir si dans la table il y a un (ou des) enregistrement(s) ayant le champs nom correspondant au paramètre, si c'est le cas, je renvoie un ResultSet, sinon je revoie null.
Or la ligne «if (call.executeUpdate() != 0) { » me renvoie cette erreur étrange : «java.sql.SQLException: Can not issue executeUpdate() for SELECTs»
Et on remarque que je ne rentre pas dans le if()
Merci d'avance
Partager