动态网页中数据库操作的一个问题

来源:百度知道 编辑:UC知道 时间:2024/05/20 19:29:44
怎样实现多条记录同时更新?
例如:在需要提交的网页中有如下内容:
学号 成绩
001 80
002 81
003 85
…… ……
“成绩”这一列是文本框,我的意思就是要实现将所有学生的成绩一次全部提交上到数据库中修改。怎样实现?我见过用ID号单个修改的例子,但是这种批量的修改我不会。

封装成实体对象,然后将实体对象添加到list或者arraylist集合里,将集合传递到数据库操作的方法里,用循环取出集合里的实体对象。
如果是arraylist要进行类型转换。将arraylist里的元素转换成实体对象,
然后在循环里调用数据修改的方法。

例如: 有学生类 StudentScore() 实体类,

ArrayList arrayList = new ArrayList();

StudentScore stuScore = new StudentScore(001,80);
arrayList.add(stuScore);
StudentScore stuScore1 = new StudentScore(002,81);
arrayList.add(stuScore1);
StudentScore stuScore2 = new StudentScore(003,85);
arrayList.add(stuScore2);
....

将封装好的对象集合传递到更新方法里.
public int ExecuteUpdate(ArrayList array)
{
StudentScore student = null;
//PerparedStatement
PerparedStatement ps = null;
// 先检测数组是否为空
if(array.size() > 0)
{
try{
//调用获得数据库连接的方法获得连接对象
con = getConnection();
for(int i = 0; i < array.size(); i++)
{
student