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;(这句能更新)
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;(这句能更新)
解决方案 »
- java 过滤敏感词替换
- Department为自关联表——我想查询出没有子部门集合的所有上级部门,HQL不明白该怎么写
- struts2 国际化中文乱码问题
- 刚刚学了jsp,学练习练习不知道做什么?
- hibernate的session是用完一次就关闭一次吗
- Struts html:select 下拉框问题
- 在线求助:请问在EJB中如何将日期的时间信息(时 分 秒)传到数据库
- synchronzied 有什么用啊,高手给讲讲!
- java做QQ机器人
- 怎样才能包含j2ee的类库,如javax.ejb? 我的环境是netbeans4.1+Sun AS8+PointDatabase
- 业务层捕捉DataAccessException异常并处理
- 急需一个完整的sturs2 ajax 案例
update student set name='test',age=22 where id=1
应该是ps.setInt(1,s.getAge()); 预处理标记age出现在第一个位置