Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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();

    }
Posted
Updated 21-May-16 7:51am

You should use batch-update[^], especially (scroll down to) the section on "Performing Parameterized Batch Update".
Next, re-look at your for loop...
 
Share this answer
 
v3
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900