<%
RESOURCES res = new RESOURCES();
ResultSet rs = res.getAll();
while(rs.next()){
if( res.getSecId(rs.getInt("res_id")).equals(request.getParameter("sec")) ){
out.println(res.getTitle(rs.getInt("res_id")));
}
}
%>
对于此代码,我有以下错误。
java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:928)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:850)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5768)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5688)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5728)
.
.
.
注意:getSecId,getTitle和getAll是RESOURCES java类中的方法,而我的db连接是在另一个java类中。
public String getSecId(int id) throws SQLException {
String SQL = "select section_sec_id from resource where res_id = " + id;
ResultSet res = db.getStmt().executeQuery(SQL);
return res.getString("section_sec_id");
}
public String getTitle(int id) throws SQLException {
String SQl = "select title from resource where res_id = " + id;
ResultSet res = db.getStmt().executeQuery(SQl);
res.next();
return res.getString("title");
}
答案 0 :(得分:0)
Before start of result set
这意味着您在调用next()之前尝试从ResultSet获取值,以便在结果集的开头进行定位。事实上:
ResultSet res = db.getStmt().executeQuery(SQL);
return res.getString("section_sec_id");
应该是
ResultSet res = db.getStmt().executeQuery(SQL);
if (res.next()) {
return res.getString("section_sec_id");
}
else {
throw new IllegalStateException("I expect to have at least one row, but there is none");
}
那就是说,请不要把Java代码,甚至更少的数据库访问代码放在JSP中。这不应该是它应该的地方。 JSP是一种视图技术。它的职责是生成HTML,而不是访问数据库。