gpt4 book ai didi

java - 使用准备好的语句执行更新查询

转载 作者:行者123 更新时间:2023-12-01 00:25:52 25 4
gpt4 key购买 nike

 int selectie = toernooienUitvoer.getSelectedRow();
int selectiec = toernooienUitvoer.getSelectedColumn();
String primarykey = (String) toernooienUitvoer.getValueAt(selectie, 0).toString();
String waarde = toernooienUitvoer.getValueAt(selectie, selectiec).toString();

String columnaam = toernooienUitvoer.getModel().getColumnName(selectiec).toString();
String input = JOptionPane.showInputDialog("wijzig geselecteerde data", waarde);
toernooienUitvoer.setValueAt(input, selectie, selectiec);


PreparedStatement stat = con.prepareStatement("UPDATE fullhouse.toernooi SET ? = ? WHERE toernooi.T_code = ?");
stat.setString(1, columnaam);
stat.setString(2, input);
stat.setString(3, primarykey);

伙计们,如果我输入值,我知道查询是正确的。我猜我的错误是在准备好的陈述中的某个地方我收到 MySQLSyntaxErrorException:

最佳答案

如其他答案所述,占位符 ? 只能用于值,不能用于表名和列名。因为您没有重用 PreparedStatement,所以这很简单。

改变自

PreparedStatement stat = con.prepareStatement("UPDATE fullhouse.toernooi SET ? = ? WHERE toernooi.T_code = ?")

PreparedStatement stat = con.prepareStatement("UPDATE fullhouse.toernooi SET " + columnName + " = ? WHERE toernooi.T_code = ?")

并在 setString 调用中调整索引参数。

关于java - 使用准备好的语句执行更新查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14512115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com