如何将列表保存到sql中?假设列表包含{1,2,3,0,0},我希望它们保存到表首选项中,该首选项有5列(pre1,pre2,pre3,pre4,pre5)。我怎样才能做到这一点?
public void addPre(List<Integer>preferences) throws Exception{
DatabaseConnection db=new DatabaseConnection();
Connection connect=db.getConnection();
String sql="Insert into preferences(Pre1,Pre2,Pre3,Pre4,Pre5)VALUES (?,?,?,?,?) ";
PreparedStatement ps=connect.prepareStatement(sql);
for(int i=0;i<preferences.size();i++)
{
ps.setInt(i+1,preferences[i] );
}
ps.executeUpdate();
connect.close();
ps.close();
}
答案 0 :(得分:1)
您需要更改
ps.setInt(i+1,preferences[i] );
要
ps.setInt(i+1,preferences.get(i) );
preferences
是List
而不是array
。
答案 1 :(得分:0)
您无法使用preferences[i]
访问它,它是列表,因此它应该像preferences.get(i)