简体   繁体   English

Java SQL列插入

[英]java sql column insert

I created a table with column actual & Predicted 我用实际和预测列创建了一个表

I inserted a Array of size 220 into column in table using SQL & JAVA 我使用SQL和JAVA在表的列中插入了大小为220的数组

st.execute("insert into host_1 (actual) values ('"+ac[j]+"')");

Now i try to insert predicted value 现在我尝试插入预测值

st.executeUpdate("insert into host_1 (exp_predict) values ('"+pre[i]+"')");

It get inserted from 221th row. 从第221行插入。 I want it to be inserted from row0 我希望将其从row0插入

Plz anyone help 请任何人帮助

An insert statement always creates a new row. insert语句始终创建新行。 If you want to set values to existing rows, you should use an update statement. 如果要为现有行设置值,则应使用update语句。 Or, better yet still, insert both values at the same time: 或者,更好的是,同时插入两个值:

// Assume, or check, that ac and pre are the same size:
int size = ac.length;

PreparedStatement ps = 
    myConnection.prepareStatement
    ("INSERT INTO host_1 (actual, exp_predict) VALUES (?, ?)");
for (int i = 0; i < size; ++i) {
    ps.setString(1, ac[i]);
    ps.setString(2, pre[i]);
    ps.addBatch();
}
ps.executeBatch();
"insert into host_1 (actual) values ('"+ac[j]+"')" 

Above inserts a entire row. 在上方插入整行。 Not just column. 不只是专栏。

What you should do is first insert the column actual, later update the rows inserted. 您应该做的是先插入实际列,然后更新插入的行。 Your first SQL is fine, you only need to modify the second. 您的第一个SQL很好,您只需要修改第二个即可。

st.executeUpdate("update host_1 set exp_predict = pre[i] where actual = ac[j]");

You should update host_1 with exp_predict in the same loop with the condition as like 您应该在条件相同的循环中使用exp_predict更新host_1,如下所示:

for(int i = 0; i<221; i++){
 st.execute("insert into host_1 (actual) values ('"+ac[j]+"')");

 st.executeUpdate("update host_1 set exp_predict = '"+pre[i]+"' where 
 actual = '"+ac[j]+"'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM