我有两个问题:
1)如何在 UPDATE 查询中调用变量,因为我想使用同一行来更新大量列。 我在 INSERT 和 SELECT 中做到了这一点,但它导致 UPDATE 错误 我在哪里使用的:
string x="term";
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ourproject?useUnicode=true&characterEncoding=utf8&user=luffy&password=111111");
Statement stmt=(Statement) con.createStatement();
String select = "SELECT ('" + x + "') FROM test WHERE doc=0";
stmt.executeUpdate(select);
}
catch(Exception e)
{
e.printStackTrace();
}
2) 如果我可以调用一个变量,如何通过加 1 来更新它的值? 我尝试了这个并且有效:
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/ourproject?useUnicode=true&characterEncoding=utf8&user=luffy&password=111111");
Statement stmt=(Statement) con.createStatement();
String update = "UPDATE test SET term=term+1 WHERE doc=0";
PreparedStatement updateQuey =con.prepareStatement(update);
updateQuery.executeUpdate(update);
}
catch(Exception e)
{
e.printStackTrace();
}
但是我需要调用 X ,因为我想对多个列使用同一行。 提前谢谢
最佳答案
您可以使用带有参数的准备好的语句。 here is the docu来自甲骨文关于它的信息。
在您的上下文中:
String update = "UPDATE test SET term = ? WHERE doc=0";
PreparedStatement updateQuey = con.prepareStatement(update);
updateQuey.setInt(1, x);
updateQuery.executeUpdate();
顺便说一句:我认为您不需要更新字符串作为 executeUpdate()
关于java - 如何使用变量更新列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16636717/