本质上,我有一种方法可以处理从表中获取的数据以创建新对象“ ZExpert”。 ZExpert具有参数int id,int domain和double ZExpert。我在从“ Z_Expert_Score”中获取数据的表中添加了一个列。
我想将对象的双倍ZExpert分数放在“ Customer_Expert_ID” =域和“ Customer_ID” = id的行的Z_Expert_Score列中。这是我的尝试
ZExpert[] allZExpert = scrCalc.getzExpertAll();
for(int i = 0; i < allZExpert.length; i++) {
ZExpert currentZExpert = allZExpert[i];
int id = currentZExpert.getId();
int domain = currentZExpert.getDomain();
double ZExpertScore = currentZExpert.getzExpert();
Statement statement = conn.createStatement();
statement.executeUpdate ("INSERT INTO CONSUMER_EXPERT_ID
where CONSUMER_EXPERT_ID="+domain+"AND CONSUMER_ID="+id+ "(Z_EXPERT_SCORE) VALUES("+ZExpertScore+")");
}
提前致谢!
最佳答案
您需要使用update SQL statement,而不是插入符。
UPDATE CUSTOMER_TABLE SET Z_EXPERT_SCORE = ? WHERE CONSUMER_EXPERT_ID= ? AND CONSUMER_ID= ?
另外,您应该使用PreparedStatement,而不是将字符串组合在一起以创建SQL语句。
ZExpert[] allZExpert = scrCalc.getzExpertAll();
for(int i = 0; i < allZExpert.length; i++) {
ZExpert currentZExpert = allZExpert[i];
int id = currentZExpert.getId();
int domain = currentZExpert.getDomain();
double ZExpertScore = currentZExpert.getzExpert();
String sql = "UPDATE CUSTOMER_TABLE SET Z_EXPERT_SCORE = ? WHERE CONSUMER_EXPERT_ID= ? AND CONSUMER_ID= ?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setDouble(1, zExpertScore);
statement.setInt(2, domain);
statement.setInt(3, id);
statement.executeUpdate();
}