Bonjour a tous,
j'essaie avec ce code d'inséré un enregistrement dans ma table "messages" puis de renvoyer au travers de cette fonction l'id du dernier message inséré.

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
 
public static int sendMessage(int numUser_id, String strTitle, String strValue) throws SQLException{
        Connection objCon = DBConnector.connectToMySQL();
        String strQuery = "INSERT INTO messages (user1_id, title, content, created_at, updated_at) VALUES (?, ?, ?, ?, ?);";
        PreparedStatement objStatement = objCon.prepareStatement(strQuery);
        objStatement.setInt(1, numUser_id);
        objStatement.setString(2, strTitle);
        objStatement.setString(3, strValue);
        objStatement.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
        objStatement.setTimestamp(5, new Timestamp(System.currentTimeMillis()));
        objStatement.execute();
        strQuery = "SELECT LAST_INSERT_ID() lastId;";
        Statement objLast = objCon.createStatement();
        ResultSet objRs = objLast.executeQuery(strQuery);
        objRs.next();
        int numLastInsert = objRs.getInt("lastId");
        objCon.close();
        return numLastInsert;
    }
Le message est bien inséré mais ma fonction me renvoie null quoi je j'essaie.

Je vous remercie d'avance des éventuelles réponses.

Gnarik