在while()循环

时间:2016-04-15 13:27:05

标签: java jdbc sqlite

我理解人们之前遇到过此异常,主要是因为:

  • next()未被调用
  • ResultSet或连接在完成任务之前已关闭

似乎不是我的情况。

我使用 preparedstatement 进行查询,并在while循环中调用 rs.next()

发生的事情是我在while循环中遇到异常。并且它不会在任何特定的行给我异常,它每次都会改变(我无法检测到模式)。

我在此堆栈跟踪中显示此错误:

java.sql.SQLException: ResultSet closed
at org.sqlite.core.CoreResultSet.checkOpen(CoreResultSet.java:69)
at org.sqlite.jdbc3.JDBC3ResultSet.findColumn(JDBC3ResultSet.java:38)
at org.sqlite.jdbc3.JDBC3ResultSet.getString(JDBC3ResultSet.java:437)
at music.store.DBHelper.queryAnything(DBHelper.java:175)
at music.store.Search.doPost(Search.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

这是我正在运行的代码

public ArrayList<Album> queryAnything(String query) throws SQLException {
    ArrayList<Album> albums = new ArrayList<>();
    PreparedStatement statement = null;
    Connection conn = null;

    try { 
        Class.forName("org.sqlite.JDBC");
        conn = DriverManager.getConnection("jdbc:sqlite:" + _dbPath);

        statement = conn.prepareStatement("SELECT "
                + "s.id as sid, " 
                + "s.title as songTitle, "
                + "s.price,"
                + "a.id as aid,"
                + "a.title as albumTitle,"
                + "a.artist,"
                + "a.publisher,"
                + "a.year,"
                + "a.genre "
                + "FROM song s JOIN album a ON s.aid = a.id WHERE s.title = ? OR a.title = ? OR a.artist = ?;");
        statement.setString(1, query);
        statement.setString(2, query);
        statement.setString(3, query);
        ResultSet rs = statement.executeQuery();

        Song song = null;
        Album album = null;
        while (rs.next()) {
            album = new Album(rs.getString("aid"), 
                              rs.getString("artist"), 
                              rs.getString("albumTitle"), 
                              rs.getString("genre"), 
                              rs.getString("publisher"), 
                              rs.getInt("year"));

            song = new Song(rs.getString("sid"), 
                            rs.getString("aid"),
                            rs.getString("songTitle"),
                            rs.getString("artist"),
                            rs.getString("albumTitle"),
                            rs.getFloat("price"));


            if (!albums.contains(album)) {
                albums.add(album);
            }

            album.set_songList(song);
        } 
    } catch (Exception e) { 
        e.printStackTrace(); 
    } finally {
        if (statement != null) statement.close();
        if (conn != null) conn.close();
    }

    return albums;
}

谢谢你的帮助 升

1 个答案:

答案 0 :(得分:0)

来自@vagelis回答:

  

您似乎不止一次从结果集中获取某些列。直接使用JDBC时,最好只获取一次值,并按照在SQL中声明的顺序。我甚至会引用索引列而不是名称。首先尝试在变量中分配所有值,然后在构造函数调用中使用变量。