entity理的字段
private int id;
private String name;
private int age;
dao里的更新方法
public void update(Student s) {
// TODO Auto-generated method stub
sql = "update student set age = ? where id = "+s.getId();
try{
conn = u.getConnection();
conn.setAutoCommit(false);
ps = conn.prepareStatement(sql);
ps.setInt(3,s.getAge());
ps.executeUpdate();
conn.commit();
}catch(Exception e){
try {
if(conn!=null)
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
u.release(rs, ps, conn);
}
}}
调用方法方法测试怎么就更新不了
Student s = new Student(1,"lili",18);
Dao d = new Dao();
d.update(s);
调用方法方法测试怎么就更新不了?
错误信息
java.sql.SQLException: Parameter index out of bounds. 3 is not between valid values of 1 and 1
at com.mysql.jdbc.ServerPreparedStatement.getBinding(ServerPreparedStatement.java:751)
at com.mysql.jdbc.ServerPreparedStatement.setInt(ServerPreparedStatement.java:1705)
at dao.ImDao.update(ImDao.java:106)
at service.ImService.updateByStudent(ImService.java:28)
at test.Test.main(Test.java:15)
还有就是直接在数据库中更新也更新不了
数据库建表语句
create table student(int id not null,char(10) name,int age);
id  name age
1   tome 20
2   lucy 18
我的更新语句是
update student set name = "jons",set age = 22 where id = 1;(直接在数据库中更新不了)
update student set set age = 22 where id = 1;(这句能更新)