我是准备好陈述中的新手,不确定我尝试做的是否合法。
String updatequery = "Update articles SET Title = ? WHERE id IN ?";
try{
prestatement = connect.prepareStatement(updatequery);
prestatement.setString(1, "Test");
prestatement.setString(2,"(4,5,6)");
我收到以下错误:
Error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ''(4,5,6)'' at line 1
似乎setString()方法正在为查询添加引号,有没有办法避免使用此方法或其他方法来完成我想要做的事情?
答案 0 :(得分:0)
试试这个:
final Collection<Integer> ids = Arrays.asList(4, 5, 6);
final StringBuilder updatequery = new StringBuilder("Update articles SET Title = ? WHERE id IN (");
for (int i = 0; i < ids.size(); i++) {
updatequery.append("?,");
}
updatequery.deleteCharAt(updatequery.length() - 1);
updatequery.append(")");
System.out.println(updatequery);
prestatement.setString(1, "Test");
int i = 2;
for (final Integer id : ids) {
prestatement.setInt(i ++, id);
}