简体   繁体   中英

Before start of result set

I want to display data from two tables in a single jsp page. I am getting the following error. can you please explain the error.

java.sql.SQLException: Before start of result set
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1058)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:972)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:958)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:903)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:854)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5772)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5692)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5732)
at jsp_servlet.__userhome._jspService(__userhome.java:149)
at weblogic.servlet.jsp.JspBase.service(JspBase.java:35)
at   weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:136)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:346)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:243)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3432)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3402)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.WebAppServletContext.doSecuredExecute(WebAppServletContext.java:2285)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2201)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1572)
at weblogic.servlet.provider.ContainerSupportProviderImpl$WlsRequestExecutor.run(ContainerSupportProviderImpl.java:255)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)

UPDATE: This is the code, causing the error

     PreparedStatement pstm = null;
     String sql = "select * from owner";
     pstm = con.prepareStatement(sql);
     ResultSet rs = pstm.executeQuery();
     ResultSet firstrs=rs;
     String tname=rs.getString("tablename");
     String sql1="SELECT * FROM "+tname;
     Statement stmt=con.createStatement();
     ResultSet rs1=stmt.executeQuery(sql1);
     ResultSetMetaData rsmd=rs1.getMetaData();
PreparedStatement pstm = null;
String sql = "select * from owner";
pstm = con.prepareStatement(sql);
ResultSet rs = pstm.executeQuery();
ResultSet firstrs=rs;
String tname="";
while(rs.next())//You have to write resultset like this because if result set is empty then it will gaves an error
{
    tname=rs.getString("tablename");
    tname+=",";
}
if(!tname.equal(""))
{
    String sql1="SELECT * FROM "+tname;///It select multiple tables results
    Statement stmt=con.createStatement();
    ResultSet rs1=stmt.executeQuery(sql1);
    ResultSetMetaData rsmd=rs1.getMetaData();
}

The actual cause for this is, you have set the Cursor before the first row of the result set.

So common practice is to test it against to check if it has at least 1 row or not, normally within a If condition or within a loop as follows:

 PreparedStatement pstm = null;
 String sql = "select * from owner";
 pstm = con.prepareStatement(sql);
 ResultSet rs = pstm.executeQuery();
 ResultSet firstrs=rs;
 // So with the following line we set it to the first row of the result set.
 while (rs.next()) {
      String tname=rs.getString("tablename");
 }
 
 String sql1="SELECT * FROM "+tname;
 Statement stmt=con.createStatement();
 ResultSet rs1=stmt.executeQuery(sql1);

Above first answer is correct. But I wanted to make it more meaningful.

 ResultSetMetaData rsmd=rs1.getMetaData();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM