Reputation: 49
I'm attempting to store a list of words in a database. All of the other database oriented functions work well, but this specific method, when called in a loop returns a ResultSet is Closed
Error. I'm trying to simulate HashMap's method structure. Here's the problematic method:
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;
}
}
It seems to throw the error at
ret = rs.getInt()
. I seem to be at wits end, being a beginner to the world of databases, but everyone was once a beginner :).
For Reference, the Whole Class:
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;
}
}
Upvotes: 0
Views: 277
Reputation: 1967
You need to first call rs.next() to make sure that resultset cursor is pointing to the first record in the returned ResultSet. So, replace the below call:
ret = rs.getInt("frequency");
with :
if(rs.next()){
ret = rs.getInt("frequency");
}
Upvotes: 2