I am trying to insert values from arraylist into mysql table.
List lstUnique=new ArrayList<Object>();
//code to feed the data into list from resultset.
try{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/MYdb","root","");
stmt=con.createStatement();
String sql1="select SourceFrom,Updated,Location_Type,Industry,ET_Rank,BT_Rank from mytable";
rs1=stmt.executeQuery(sql1);
rsmd=rs1.getMetaData();
columnNumber=rsmd.getColumnCount();
while(rs1.next()){
lstUnique.add(rs1.getString("SourceFrom")+","+rs1.getString("Updated")+","+rs1.getString("Location_Type")+","+
rs1.getString("Industry")+","+rs1.getString("ET_Rank")+","+rs1.getString("BT_Rank"));
}
String insertsql="";
String SourceFrom=lstUnique.get(0).toString(); //its first column of the table.
insertsql="Insert into java_uniquedata (SourceFrom,field2,field3,field4,field5) values(?,?,?,?,?)";
PreparedStatement ps=con.prepareStatement(insertsql);
//I tried this way also.
for(int i=0;i<lstUnique.size();i++){
SourceFrom=lstUnique.get(i).toString();
}
for(int i=0;i<lstUnique.size();i++){
System.out.println("\n" + lstUnique.get(i).toString());
}
rs1.close();
con.close();
}catch(Exception e){
System.out.println(e);
}
But I am getting error
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
My list has only one record in it, which has total 5 columns' values. Can you guide me how do I fetch values of first record from arraylist and insert it into mysql table.
soutcall inside thewhile(rs1.next())and provide a result?