用jsp页面从数据库中提取数据

用jsp页面从数据库中提取数据,jsp,Jsp,注意:getSecId、getTitle和getAll是RESOURCES java类中的方法,my db connection在另一个java类中 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

注意:getSecId、getTitle和getAll是RESOURCES java类中的方法,my db connection在另一个java类中

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)
.
.
.
这意味着您在调用结果集开头的“定位”旁之前,试图从结果集获取值。事实上:

Before start of result set
应该是

ResultSet res = db.getStmt().executeQuery(SQL);
return  res.getString("section_sec_id");

也就是说,请不要将Java代码,甚至更少的数据库访问代码放在JSP中。这不是它应该在的地方。JSP是一种视图技术。它的职责是生成HTML,而不是访问数据库。

public ResultSet getAll抛出SQLException{String SQL=select*from resource;return db.getStmt.executeQuerySQL;}这正是我要做的!我只是尝试在ResultSet ResultSet rs=res.getAll;,之前执行查询,然后我尝试从结果集中获取值!我在帖子下面的评论中写了getAll方法,至少你没有在getSecId中这样做。由于您没有发布完整的堆栈跟踪(您应该一直这样做),我无法确认问题是否来自getAll、getSecId或其他方法。
Before start of result set
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");
}