@@ -36,23 +36,21 @@ public boolean insert(CachedMessage message) throws SQLException {
3636 }
3737
3838 /**
39- * Inserts a {@link List} of {@link CachedMessage} objects..
39+ * Inserts a {@link List} of {@link CachedMessage} objects.
4040 *
4141 * @param messages The List to insert.
42- * @return Whether there were rows affected by this process.
4342 * @throws SQLException If an error occurs.
4443 */
45- public boolean insertList (List <CachedMessage > messages ) throws SQLException {
46- StringBuilder statementString = new StringBuilder ("INSERT INTO message_cache (message_id, author_id, message_content) VALUES" );
47- for (CachedMessage msg :messages ) {
48- statementString .append (String .format (" (%s, %s, '%s')," , msg .getMessageId (), msg .getAuthorId (), msg .getMessageContent ()));
49- }
50- statementString .deleteCharAt (statementString .toString ().length () - 1 ).append (";" );
51- try ( PreparedStatement stmt = con .prepareStatement (statementString .toString (),
44+ public void insertList (List <CachedMessage > messages ) throws SQLException {
45+ try (PreparedStatement stmt = con .prepareStatement ("INSERT INTO message_cache (message_id, author_id, message_content) VALUES (?, ?, ?)" ,
5246 Statement .RETURN_GENERATED_KEYS
5347 )) {
54- int rows = stmt .executeUpdate ();
55- return rows > 0 ;
48+ for (CachedMessage msg :messages ) {
49+ stmt .setLong (1 , msg .getMessageId ());
50+ stmt .setLong (2 , msg .getAuthorId ());
51+ stmt .setString (3 , msg .getMessageContent ());
52+ stmt .executeUpdate ();
53+ }
5654 }
5755 }
5856
0 commit comments