函数如下,不知道能不能返回按照条件查询的ResultSet结果集
请高手赐教!!
public ResultSet getExperiments(String teacherID)throws SQLException {
Connection conn=null; //创建新连接
String sql = "select * from TeacherToExperiment where TeacherID="+teacherID;
Statement stmt=conn.createStatement(); //创建语句对象
ResultSet rset=stmt.executeQuery(sql); //执行数据查询语句
rset.close();
stmt.close();
return rset;
/**
* 返回一个教师的所有实验
*/
因为这几个类都是组长给发过来的,函数头都是写好的,所以我就按照她写的返回值来编的,能再讲细一些吗?为什么不可以,怎样才能返回resultSet结果集?如果改成你说的那个List应该怎么做呢?谢谢!
追答嗯嗯,ResultSet只是jdbc内部封装的一个接口而已,你用它来做方法的返回值,好像不行吧,呵呵,如果用list集合的话应该是这样的:这是我刚学java的时候写的,比较繁琐,希望对你有用哦,呵呵
public List queryAllEmployee() {//关键是方法的返回值的问题
List list = new ArrayList();
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
conn = DBUtil.getConnection();
try {
pstmt = conn.prepareStatement("select*from employeeInfo");
rs = pstmt.executeQuery();
while (rs.next()) {
EmployeeInfo employee = new EmployeeInfo();
employee.setEmployeeID(rs.getInt("employeeID"));
employee.setIsLead(rs.getString("isLead"));
list.add(employee);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.closeAll(pstmt, rs, conn);
}
return list;//这里返回list集合就行了,呵呵
}
如果在关闭之前返回ResultSet 行吗?就是想吧查询结果返回。
追答不关闭是可以的,但如果关闭了ResultSet或者Statement都不能再在其它地方使用ResultSet.