如何将arraylist插入mysql [英] How to insert arraylist into mysql
本文介绍了如何将arraylist插入mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试将ArrayList插入MySQL,但我只获取列select_seats中的最后一个值。变量列表保存值2和3.当我检查MySQL时,只插入值3,未插入2。
我尝试过:
I trying to insert ArrayList into MySQL, but I only gets the last values in column select_seats. The variable list holds value 2 and 3. When I check MySQL, only value 3 get inserted, 2 is not inserted.
What I have tried:
public void insert( String Name, String NC, String acNum, String title, String day, String time, double totalPrice, ArrayList<Integer> list) throws Exception{
System.out.println(list); // [2,3]
DatabaseConnection db=new DatabaseConnection();
Connection connect=db.getConnection();
String sql="Insert into user_payment(user_name,ic_number,acc_number, movie_title,movie_day,movie_time, total_price, selected_seats)VALUES (?,?,?,?,?,?,?,?)";
PreparedStatement ps=connect.prepareStatement(sql);
ps.setString(1,Name);
ps.setString(2,NC);
ps.setString(3,acNum);
ps.setString(4,title);
ps.setString(5,day);
ps.setString(6,time);
ps.setDouble(7,totalPrice);
for(Integer seat : list)
{
ps.setInt(8,seat);
}
ps.executeUpdate();
connect.close();
ps.close();
}
推荐答案
你应该使用batch-update [ ^ ],特别是(向下滚动到)执行参数化批量更新部分。
接下来,重新看看你的for循环......
You should use batch-update[^], especially (scroll down to) the section on "Performing Parameterized Batch Update".
Next, re-look at your for loop...
我从 java - 将ArrayList插入MySQL中得到答案 - Stack Overflow
这篇关于如何将arraylist插入mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文