将ArrayList插入MySQL

时间:2016-05-21 16:13:14

标签: java mysql arraylist

我尝试将ArrayList插入MySQL,但我只获取了selected_seats列中的最后一个值。变量列表包含值23。当我检查MySQL时,只插入值3,不插入2。

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);
            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.executeBatch();
            }
            ps.executeUpdate();

            connect.close();
            ps.close();

        }

2 个答案:

答案 0 :(得分:1)

您打算在循环中调用addBatch(),然后在循环后调用executeBatch(),而不是调用executeUpdate()

  

<强> addBatch()

     

为此PreparedStatement对象的一批命令添加一组参数。

     

<强> executeBatch()

     

向数据库提交一批命令以供执行

此外,您必须在关闭PreparedStatement之前关闭Connection 。更好的是,使用try-with-resources。

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);
    DatabaseConnection db = new DatabaseConnection();
    try (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 (?,?,?,?,?,?,?,?)";
        try (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.addBatch();
            }
            ps.executeBatch();
         }
    }
}

<强>更新

  

是否可以只插入一行,这意味着列selected_seats值有2,3而是创建两行?

如果selected_seats列不是原始setInt(8,seat)指示的数字列,而是VARCHAR应该得到以逗号分隔的座位号列表,则批量为不需要。然后,您需要将List<Integer>转换为逗号分隔的字符串

StringJoiner buf = new StringJoiner(",");
for (Integer seat : list)
    buf.add(seat.toString());
ps.setString(8, buf.toString());
ps.executeUpdate();
在Java 8中添加了

StringJoiner。如果在早期版本上运行,请使用StringBuilder

StringBuilder buf = new StringBuilder();
for (Integer seat : list) {
    if (buf.length() != 0)
        buf.append(',');
    buf.append(seat);
}
ps.setString(8, buf.toString());
ps.executeUpdate();

答案 1 :(得分:1)

您需要将座位插入另一个表,或者可以使用String类型插入,如下所示。

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);
    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); 
    String seatList = "";
    for(Integer seat : list) { 
        if(seatList.equal("")) {
            seatList = seat;
        } else {
            seatList += "," + seat;
        }
    } 
    ps.setString(8,seatList); 
    ps.executeBatch(); 
    ps.executeUpdate(); 
    connect.close(); 
    ps.close(); 
}