-2

这是一个我知道有效并返回 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)
4

1 回答 1

2

你需要先rs.next()打电话 rs.getString(1)。例外也暗示了同样的事情。

于 2013-03-28T19:32:33.513 回答