public Integer getStudentIDTop(int classes_id) throws TjException {
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Student stu = null;
int max_id = 0;
try {
conn = JdbcUtils.getConnection();
String sql = "select MAX(student_id) from student where classes_id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, classes_id);
rs = ps.executeQuery();
if(rs.next()){}
} catch (SQLException e) {
throw new TjException(e, "jdbc.getBook.db", "查询数据库错误!");
} catch (Exception e) {
throw new TjException(e, "jdbc.getBook.unknown", "查询其它错误!");
} finally {
JdbcUtils.release(rs, ps, conn);
}
return max_id;
这是dao类中的一个方法,目的是取到student表中指定班级的学号最大值。但是不知道怎么获得返回值。求大神解答!
}
// TODO Auto-generated method stub
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Student stu = null;
int max_id = 0;
try {
conn = JdbcUtils.getConnection();
String sql = "select MAX(student_id) from student where classes_id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, classes_id);
rs = ps.executeQuery();
if(rs.next()){}
} catch (SQLException e) {
throw new TjException(e, "jdbc.getBook.db", "查询数据库错误!");
} catch (Exception e) {
throw new TjException(e, "jdbc.getBook.unknown", "查询其它错误!");
} finally {
JdbcUtils.release(rs, ps, conn);
}
return max_id;
这是dao类中的一个方法,目的是取到student表中指定班级的学号最大值。但是不知道怎么获得返回值。求大神解答!
}
解决方案 »
- 请教一个关于Linux下java内存的问题
- org.hibernate.exception.ConstraintViolationException: Could not execute JDBC bat
- 求助!关于java中web service的开发与调用
- 关于Tomcat无法启动问题
- weblogic8.1在console下布署应用时,应用名问题
- spring和struts1.X集成的问题
- <ec:table>鼠标经过事件
- tomcat client deployer 求例子文件
- Pet Store+jBoss3.0.3
- 如何用java代码判断当天是不是星期一啊
- 对象实体一个字符串中删除一个字符串,并返回String是什么方法
- jquery ajax的问题
当然要循环结果集resultSet对象
代码:
while(resultSet.next()){
getInt("student_id");//或 getInt(1);
}
正确的方法应该是:conn = JdbcUtils.getConnection();
String sql = "select MAX(student_id) from student where classes_id=?";
System.out.println(sql);
ps = conn.prepareStatement(sql);
ps.setInt(1, classes_id);
System.out.println(classes_id);
rs = ps.executeQuery();
if(rs.next()){
max_id = rs.getInt("1");
//max_id = rs.getInt(1);也可,1为列的标志。
System.out.println(max_id);
}