public static Boolean userImage(String userIdString, InputStream stream) {
PreparedStatement statement = null;
String updateTableSQL = "UPDATE `users` SET `image` = ? WHERE `id` = ? ;";
try {
Integer userId = Integer.parseInt(userIdString);
statement = DatabaseManager.getConnection().prepareStatement(updateTableSQL);
statement.setBinaryStream(1, stream);
statement.setInt(2, userId);
Log.write("USER IMAGE UPDATED> " + statement);
statement.executeUpdate(updateTableSQL);
return true;
} catch (Exception e) {
Log.write("USER IMAGE UPDATED> " + e.getMessage());
return false;
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
日志:
09:55:59:0496: USER IMAGE UPDATED> com.mysql.jdbc.JDBC42PreparedStatement@1c902d70: UPDATE
users
SETimage
= ** STREAM DATA ** WHEREid
= 29 ;09:55:59:0511: USER IMAGE UPDATED> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? WHERE
id
= 29' at line 1
最佳答案
您使用的 executeUpdate
版本错误
因为您已经创建了带参数的语句
将statement.executeUpdate(updateTableSQL);
更改为statement.executeUpdate();
关于java - 从 java 更新 Blob 时出现 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44819587/