我正在使用Java程序來查詢SQLite數據庫。我目前處於開發測試狀態,並想測試特定的選擇查詢。我有一個線程調用我的所有數據庫命令(生產者)和消費者線程來處理結果集。該的DBCommand線程(生產者)將剛剛打開的數據庫連接,創建一個新的查詢,問題是,得到的結果集,並將其放置在共享隊列和關閉連接,這樣的:ResultSet已請求
String url = "jdbc:sqlite:" + Configuration.getInstance().getDbPath();
// create a connection to the database
conn = DriverManager.getConnection(url);
try
{
if (conn.isValid(DB_TIMEOUT))
{
Statement stmt;
try
{
errorLog.info("Query Issued to Database: " + query);
stmt = conn.createStatement();
stmt.execute(query);
ResultSet rs = stmt.getResultSet();
if (rs != null)
{
try
{
TrendApp.resultSetProcQueue.put(new QueryResult(request, stmt.getResultSet()));
} catch (InterruptedException e)
{
errorLog.error("Failed to capture result set from query: " + e);
}
} else
{
try
{
TrendApp.resultSetProcQueue.put(new QueryResult(request, null));
} catch (InterruptedException e)
{
errorLog.error("Failed to capture result set from query: " + e);
}
errorLog.info(stmt.getUpdateCount() + " rows updated.");
}
} catch (SQLException e)
{
errorLog.error("Failed to issue database command: " + e);
}
}
} catch (SQLException e)
{
errorLog.error("Database connection is not valid.");
}
closeDB();
} else
{
errorLog.info("Database query is empty. No action performed.");
}
}
提個醒點這個程序的目的是從已經存在的更大的表中取出元素,並將一個子集放入一個新的表格中,這個表格是爲了更容易訪問而創建的。所以當我查詢新表時,出現錯誤「ResultSet already requested」。注意消費者方面,目前還沒有結果集處理(我還沒有爲它編寫代碼)。程序的重新啓動之間也會發生此錯誤。自從對我的新表進行初始查詢以來,它一直沒有工作。
[INFO] 2017年5月17日九時27分41秒[池-2-螺紋-4] DBExtend - 查詢頒發給數據庫:選擇不同的時間戳,設備ID,ioType,IOID,子場中,從值TrendPredef1111 WHERE machineId = 1111 AND ioType = 2 AND ioId =「25」AND timestamp < = 1495027659000000000 AND timestamp> = 1494943059000000000
[錯誤] 05-17-2017 09:27:41 [pool-2-thread-4] DBExtend - 無法發出數據庫命令:java.sql.SQLException:ResultSet已請求
任何想法我做錯了什麼?
您正在調用'stmt.getResultSet()'兩次,這是禁止的。 – Berger