Java 结果集已关闭检索时出错
我正在尝试在数据库中存储单词列表。所有其他面向数据库的函数都工作得很好,但是这个特定的方法在循环中调用时返回一个Java 结果集已关闭检索时出错,java,sqlite,jdbc,resultset,Java,Sqlite,Jdbc,Resultset,我正在尝试在数据库中存储单词列表。所有其他面向数据库的函数都工作得很好,但是这个特定的方法在循环中调用时返回一个ResultSet is ClosedError。我试图模拟HashMap的方法结构。以下是有问题的方法: public int get(String key){ ResultSet rs = null; int ret = -1; try{ if(!running) initialize(); rs = conn
ResultSet is Closed
Error。我试图模拟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游标指向返回的resultset中的第一条记录。因此,请替换以下调用:
ret = rs.getInt("frequency");
与:
if(rs.next()){
ret = rs.getInt("frequency");
}
您看到了什么错误?您当前的代码正在泄漏资源(例如,您没有关闭
connection.createStatement()的结果)
完成后。另外:使用PreparedStatement
进行参数化查询,将值串联到查询字符串中容易出错,并使您暴露于SQL注入。请注意,我使用它来获取英语中最常用的单词。我使用的是来自Project Gutenberg的培训数据,因此SQL Injec的可能性很小tion为零。我现在正在堵塞这些漏洞。谢谢!我用更改更新了代码,错误仍然显示:(。你能更新帖子并粘贴打电话的代码吗?我已更正。它有效,已接受。我使用了下一个方法,没有if语句。