English 中文(简体)
使用Java填充SQL表中的特定列
原标题:Using Java to populate a specific column in an SQL table

从本质上讲,我有一个方法来处理从表中获取的数据,以创建一个新的对象“ZExpert”。ZExpert有参数int id、int domain和double ZExpert。我在我获取数据的表中添加了一列,名为“Z_Expert_Score”

我想把对象的双倍ZExpert分数放在Z_Expert_score列中,在“Customer_Expert_ID”=domain和“Customer_ID”=ID的行中。这是我的尝试

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+")");
}

提前感谢!

最佳回答

您需要使用更新SQL语句,而不是插入。

UPDATE CUSTOMER_TABLE SET Z_EXPERT_SCORE = ? WHERE CONSUMER_EXPERT_ID= ? AND CONSUMER_ID= ?

此外,您应该使用PreparedStatement,而不是将Strings组合在一起创建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();
}
问题回答

如果您重新扩充已经存在的行,您将希望使用UPDATE语句。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签