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表中指定班级的学号最大值。但是不知道怎么获得返回值。求大神解答!
}

解决方案 »

  1.   

    resultSet 有两种取法一是通过getInt("student_id");  student_id 就是数据库的字段名,或者getInt(1);  按序号来取.数据库的序号从1开始.
    当然要循环结果集resultSet对象
    代码:
    while(resultSet.next()){
       getInt("student_id");//或   getInt(1);
    }
      

  2.   

    搞定了,谢谢大家了。
    正确的方法应该是: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);
    }