I have two arraylists to insert into 2 columns in a database table as follows:
arraylist1: 123444, 324555, 6423643, 532326
arraylist2: jkfdsl, nkjfsdlj, jdslkfjdlkj, jfsldjfsk, fjdlskjfs
I wrote the following code to insert the arraylists but it is not working. I will appreciate your help.
try {
// Prepare a statement to insert a record
String sql = "INSERT INTO soundsdata.splog (arraylist1, arraylist2) VALUES(?,?)";
pstmt = (PreparedStatement) con.prepareStatement(sql);
pstmt.setArray(1,sptospring);
pstmt.setString(2,eachList.toString());
// Insert the row
pstmt.executeUpdate();
}finally {
pstmt.close();
}
Here's something that you can do:
Assuming that you're trying to create one row, where the 1st
column
will contain the content of the firstArrayList
in comma-separated format and the 2ndcolumn
will contain the content of the secondArrayList
You cannot store an ArrayList in a varchar column.
You need to store a string.
String[] stringArray = lists.toArray(new String[lists.size()]);
// then write query for insert into database
insert into tablename values(string1 ......)
Insert more than one record: