java - 结果集已关闭检索时出错

标签 java sqlite jdbc resultset

我正在尝试在数据库中存储单词列表。所有其他面向数据库的函数都运行良好,但在循环中调用此特定方法时,会返回 ResultSet is Closed 错误。我正在尝试模拟 HashMap 的方法结构。这是有问题的方法:

public int get(String key){
    ResultSet rs = null;
    int ret = -1;
    try{
        if(!running)
        initialize();
        rs = connection.createStatement().executeQuery("select frequency from Commonlist where word = '"+key+"'");
        ret = rs.getInt("frequency");
    }
        catch(SQLException sqle){
        sqle.printStackTrace();
    }
    finally{
        try{rs.close();}catch(Exception e){e.printStackTrace();}
        return ret;
    }
}

它似乎抛出了错误 ret = rs.getInt()。作为数据库世界的初学者,我似乎束手无策,但每个人都曾经是初学者:)。 全类引用:

import java.sql.*;
import org.sqlite.*;
public class CommonList
{
    private static Connection connection = null;
    private static Statement query;
    private boolean running = false;
    public synchronized Connection getConnection(){
        try{
            if(running == false)
            connection = DriverManager.getConnection("jdbc:sqlite:CommonList.db");
        }
        catch(Exception e){
            e.printStackTrace();
        }
        running = true;
        return connection;
    }
    public void initialize(){
        try{
            Class.forName("org.sqlite.JDBC");
            connection = getConnection();
            query = connection.createStatement();
            query.setQueryTimeout(30);
            query.executeUpdate("create table if not exists CommonList(word string,frequency integer)");
        }
            catch(Exception sqle){
            sqle.printStackTrace();
        }
    }
    public void put(String key,int frequency){
        try{
            if(!running)
            initialize();
            query.executeUpdate("delete from CommonList where word = '"+key+"'");
            query.executeUpdate("insert into CommonList values('"+key+"',"+frequency+")");
        }
            catch(SQLException sqle){
            sqle.printStackTrace();
        }
    }
    public int get(String key){
        ResultSet rs = null;
        int ret = -1;
        try{
            if(!running)
            initialize();
            rs = connection.createStatement().executeQuery("select frequency from Commonlist where word = '"+key+"'");
            ret = rs.getInt("frequency");
        }
            catch(SQLException sqle){
            sqle.printStackTrace();
        }
        finally{
            try{rs.close();}catch(Exception e){e.printStackTrace();}
            return ret;
        }
    }
    public void delete(String key){
        try{
            if(!running)
            initialize();
            query.executeUpdate("delete from CommonList where word = '"+key+"'");
        }
            catch(SQLException sqle){
            sqle.printStackTrace();
        }
    }
    public String toString(){
//Test code
         ResultSet rs =null;
        try{
             rs =  connection.createStatement().executeQuery("select * from CommonList");
             System.out.println("Word       Frequency\n---------------------------");
             while(rs.next()){
                 System.out.println(rs.getString(1)+"    "+rs.getInt(2));
                }
            }
            catch(Exception e){
                e.printStackTrace();
            }
            finally{
                try{rs.close();}catch(Exception e){e.printStackTrace();}
                return "Java Wrapper for SQLite Database";
            }
    }
    private void closeConnection(){
        if(connection!=null)
        try{connection.close();}catch(Exception e){e.printStackTrace();}
    }
    public void close(){
        closeConnection();
        running = false;
    }
}

最佳答案

您需要首先调用 rs.next() 以确保结果集光标指向返回的 ResultSet 中的第一条记录。因此,替换以下调用:

ret = rs.getInt("frequency");

与:

if(rs.next()){
ret = rs.getInt("frequency");
}

关于java - 结果集已关闭检索时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17630720/

相关文章:

java - 无需 ssl 通过 Internet 进行 JDBC 连接

objective-c - SQLite insert语句不会在表中放置记录

sqlite - 哪个表列要索引?

c# - 如何使用C#发布的项目使SQLite数据库文件作为嵌入式资源工作

Java 数据库连接 (JDBC) session 处理?

java - JDBC - NetBeans x MySQL

java - MailChimp API java 集成

java - 在 Eclipse 中将 XML 文件添加到 Android 项目时出错

mysql - java.sql.SQLException : Column count doesn't match value count at row 1 jdbc

java - Eclipse - 无法在本地主机上发布 Tomcat8.0 服务器的服务器配置