In my code, whenever I call the following function, the database Emitter can be reached. The function basically extracts data from the databases and stores it in variables.
public void fillEmitterDataLists()
{
EmitterStructVo emitter = new EmitterStructVo();
try {
stmt = dbConn.createStatement();
rs = stmt.executeQuery( "SELECT * FROM Emitter;" );
while ( rs.next() ) {
emitter = new EmitterStructVo();
model.emitterList.add(rs.getString("E_ID"));
String pri = rs.getString("PRI_ID");
String pw = rs.getString("PW_ID");
String rf = rs.getString("RF_ID");
String reps = rs.getString("Reps");
String interval = rs.getString("Interval");
Statement stmt1;
stmt1 = dbConn.createStatement();
ResultSet rs1 = stmt1.executeQuery("SELECT * FROM PRI where PRI_ID = '" + pri + "';");
model.priList.add(rs1.getString("PRI_Values"));
int priType = Integer.parseInt(rs1.getString("PRI_TYPE"));
rs1 = stmt1.executeQuery("SELECT * FROM PW where PW_ID = '" + pw + "';");
model.pwList.add(rs1.getString("PW_Values"));
rs1 = stmt1.executeQuery("SELECT * FROM RF where RF_ID = '" + rf + "';");
model.rfList.add(rs1.getString("RF_Values"));
stmt1.close();
rs1.close();
}
stmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
But if I execute the following code after calling the above function, it gives the error that the Emitter database file is locked. Whereas, if I don't call the above function before, the following code works without a glitch. So, clearly, the problem is with the fillEmitterDataLists() function.
Class.forName("org.sqlite.JDBC");
Connection dbConn = DriverManager.getConnection("jdbc:sqlite:FssDb.db");
Statement stmt = dbConn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM Emitter where E_ID = '" + name + "';");
Now I have closed all of the statements and result sets as suggested in the similar questions on stack overflow so I have no idea why it is still producing the exception.
Please help?
StatementandResultSetin the wrong way. Closing aStatementshould (not all JDBC do it) close theResultSetwith it. So ifSqlitedo it properly, closingstmt1will closers1. The next statement would produce an error (I think). Note that this should be done propertly in a finally statement (or usingtry-with-resource)dbConnfrom the method above and try to get a new connection with the second snippet, this will not be possible, only oneConnectionis possible at the same time. It is good to use aSingletonon an SQlite DB to prevent any problem