關於另一個編程項目,我正在對一個JDBC項目(使用Netbeans,Java和Derby數據庫)進行原型設計。我的程序需要進行如下迭代更新數據庫中的表中的所有行:使用ResultSet迭代更新到jdbc derby
有三個表中的列:
famousName
,famousQuote
,hashKey
。最初,
famousQuote
列包含逐字引用。我想用while循環取下該列,獲取ascii字母的校驗和,用hashKey
值逐位「AND」,然後用逐個「加密」值替換逐字引號。
至於現在,我嘗試使用ResultSet
對象提取逐字famousQuote
,進行必要的加密,然後將該更新值的SQL語句。所有這一切都發生在while(rs.next())
循環如下:
(pseudo-code): //all necessary database connections and variable declared here.
String sqlStatement = "Select * FROM mainTable ORDER BY famousName";
ResultSet rs = stmt.executeQuery(sqlStatement);
while(rs.next()){
tempString1 = rs.getString("famousQuote");
tempString2 = rs.getString("hashKey");
tempString3 = EncryptionAlgorithm.EncryptStatement(tempString1, tempString2);
sqlStatement = "UPDATE maintable SET famousQuote=tempString3 WHERE hashKey=tempString2";
(note, there is a bit of pseudo-code regarding the WHERE part,
but I'm sure it's immaterial to the error message I'm getting.)
stmt.executeUpdate(sqlStatement);
}
這似乎是個好主意,直到節目開始拋出錯誤,如:(關於ResultSet的
ResultSet not open. Operation 'next' not permitted. Verify that autoCommit is off
後來我的文檔中讀取):
A ResultSet object is automatically closed when the Statement object that generated it
is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.
最後,我想我可以嘗試移動while循環中的ResultSet rs
聲明,以便它會實例化一個新RESU ltSet對象,但我很確定這會失去我在數據庫中的位置(永久重新更新第一行)。
我現在處於靜止狀態,不知道如何迭代我的表格行,在每個報價上執行我的EncryptionAlgorithm
,然後更新列值。
這個代碼不工作完全按原樣,但肯定是足夠接近我需要什麼我終於可以取得一些進展了!非常感謝您花時間回答關於這個非常先進和利基主題的問題! –
@the_photon這不是一個高級主題,它是基本的JDBC,它在javadoc中有解釋(包括解決方案)。另請參閱重複問題。 –