DAO问题
public boolean insertUser(User user) {
String sql2 = \"insert into user_actor_droit values(?,?,?)\";
try {
ps = con.prepareStatement(sql2);
Actor actor = user.getActor();
ArrayList ls = actor.getDroit();
for (Iterator<Droit> it = ls.iterator(); it.hasNext();) {
Droit droit = it.next();
ps.setString(1, user.getId());
ps.setString(2, actor.getId());
ps.setString(3, droit.getId());
ps.executeUpdate();
System.out.println(droit.getId());
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
===============我又调试了一下发现问题是下面代码造成的,但我觉得原理和上面的一样啊为什么会这样呢========================
public void modifyUser(User user) {
String sql1 = \"update user set user_name=?,password = ? where id=?\";
String sql2 = \"update user_actor_droit set actor_id = ? ,droit_id = ? where user_id= ?\";
try {
ps = con.prepareStatement(sql1);
ps.setString(1, user.getName());
ps.setString(2, user.getPassword());
ps.setString(3, user.getId());
ps.executeUpdate();
ps = con.prepareStatement(sql2);
Actor actor = user.getActor();
for (Iterator<Droit> it = actor.getDroit().iterator(); it.hasNext();) {
Droit droit = it.next();
ps.setString(1, actor.getId());
ps.setString(2, droit.getId());这里这么写有没有错,为什么在数据库端插入的所有记录的第三列的ID都是相同的
ps.setString(3, user.getId());
ps.executeUpdate();
System.out.println(droit.getId());这里打印的确没有问题都是不同的ID
}} catch (SQLException e) {
e.printStackTrace();
}
}}
[此贴子已经被作者于2007-7-19 16:41:04编辑过]