从JSP页面调用的方法总是抛出异常

时间:2013-03-28 19:22:20

标签: java sql jsp postgresql sqlexception

这是我知道的测试查询,并返回14。

private static final String TEST = "SELECT album_id FROM Album";

每次执行以下方法时,都会抛出异常并返回变量x而不是我的album_id

public String getAlbumId(String email, String album_name) {
    PreparedStatement stmt = null;
    Connection conn = null;
    ResultSet rs = null;
    try{
        conn = DbConnection.getConnection();
        stmt = conn.prepareStatement(TEST);
        //stmt.setString(1, email);
        //stmt.setString(2, album_name);
        rs = stmt.executeQuery();
        String album_id = rs.getString(1);
        return album_id;
    }
    catch(SQLException e) { String x="-1"; return x; }
}

调用此方法的jsp代码部分如下:

<table>
<%
  AlbumDao album2 = new AlbumDao();
  String myid;
  int actualid;
  for(String item : album_names) {
      myid = album2.getAlbumId(email,item);
      actualid = Integer.parseInt(myid);
      System.out.println(myid);
%>
<tr>
  <td><a href="display_album.jsp?album_id=<%=actualid%>"><% out.print(item);%></a></td>
</tr>
  <%}%>

到目前为止,我已经看过几十次代码了,我无法弄明白。出现这种情况的原因是什么?

printStackTrace()的输出如下(抱歉长输出):

org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkResultSet(AbstractJdbc2ResultSet.java:2452)
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:1882)
    at org.apache.tomcat.dbcp.dbcp.DelegatingResultSet.getString(DelegatingResultSet.java:174)
    at photoshare.AlbumDao.getAlbumId(AlbumDao.java:66)
    at org.apache.jsp.index_jsp._jspService(org.apache.jsp.index_jsp:103)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:524)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:667)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
    at java.lang.Thread.run(Thread.java:595)

1 个答案:

答案 0 :(得分:2)

您需要在rs.next()之前致电rs.getString(1)。例外情况也暗示相同。