I am trying to work myself into using SQLite databases in Java.
(I am also using the sqlite-jdbc-3.20.0 library, but I think this problem isn't influenced by that.)
I have created some functions I want to use later on to simplify my workflow. For that I created the class DBInterface.java, from which I created an object in my Main.java class.
Simplified content of DBInterface.java:
/**
* Executes a sql String and returns it's results as ResultSet[].
*
* @param sql as String holidng the SQL statement to execute
* @return ResultSet[] containing the results of the query, or empty ResultSet[] if no results
*/
public ArrayList<ResultSet> excecute(String sql)
{
if(!isConnected()) return null;
try
{
return getResultSets(statement.execute(sql));
}
catch (SQLException e)
{
Main.log(tag, "An SQLException has occured for statement '"+sql+"':");
Main.log(tag, e);
}
return null;
}
/**
* Returns an array containing all resultSets of the last executed statement.
*
* @param isResultSet as boolean defining if the statement resulted in an result set
* @return
*/
private ArrayList<ResultSet> getResultSets(boolean isResultSet)
{
ArrayList<ResultSet> results = new ArrayList<>();
try
{
int count = 0;
while(true) {
if(isResultSet)
{
ResultSet set = statement.getResultSet();
results.add(set);
/*
while(set.next())
{
//Here the information can still be retrieved
Main.log(tag, ""+set.getString("name")+", row:"+set.getRow());
}
*/
}
else
{
if(statement.getUpdateCount() == -1)
{
break;
}
Main.log(tag, "Result '"+count+"' is just a count: '"+statement.getUpdateCount()+"'");
}
count ++;
isResultSet = statement.getMoreResults();
}
}
catch(SQLException e)
{
Main.log(tag, "An SQLException has occured while processing a request:");
Main.log(tag, e);
}
return results;
}
Simplified content of Main.java:
public static void main(String[] args)
{
[...]
dbInterface.excecute("DROP TABLE IF EXISTS Users");
dbInterface.excecute("CREATE TABLE IF NOT EXISTS Users (id INTEGER, name STRING(20));");
dbInterface.excecute("INSERT INTO Users (id, name) VALUES (1, \"Hans\");");
dbInterface.excecute("INSERT INTO Users (id, name) VALUES (2, \"Peter\");");
ArrayList<ResultSet> results = dbInterface.excecute("SELECT * FROM Users;");
log(tag, ""+results.size());
for(int i = 0; i < results.size(); i++)
{
ResultSet set = results.get(i);
try
{
///*
while(set.next())
{
//TODO Here the information can no longer be retrieved... :(
Main.log(tag, ""+set.getString("name")+", row:"+set.getRow());
}
//*/
}
catch (Exception e)
{
Main.log(tag, e);
}
}
}
Since from how I understand it is possible in some cases that an SQL query creates multiple ResultSet's, I've though that I will simply add all ResultSet objects to an ArrayList, so I can see when I'm processing the results somewhere else how many ResultSet's exist. Stranegly though, this doesn't work. By uncommenting the block in getResultSets() in DBInterface.java I can confirm that the result set does contain the information.
However, doing the same thing in Main.java from the ArrayList, for some reason I can no longer access the information. I have tested to change the functions to instead return the ResultSet object directly, which did work, however it does not fulfill my goal, since I want to return all possible ResultSet objetcs, not just one.
List<Map<String,Object>>ResultSetis not something you can pass around and use, since it's associated with a query (i.e.Statement) andConnection. You need to read it, preferably parse the results into your own domain objects and pass those around.Statementobject, 1b execute any statement on the same connection in auto-commit, 2) close theStatement, 3) close theConnection, 4) commit/rollback a transaction (and the result set is not holdable over commit), or 5) callgetMoreResults()on a statement (which you do). Result sets should be processed and closed ASAP, not passed around.