public void reArrangeTrID(){
String parti = name.getText().toupperCase();
long trid = 1;
try{
String query="SELECT LONGDATE, TRID FROM PARTIACCOUNT WHERE PARTY= '"+parti+"' ORDER BY LONGDATE ASC ";
conn = new connection().db();
stmtt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmtt.executeQuery(query);
while(rs.next()) {
long tr = rs.getLong("TRID");
rs.updateLong("TRID", trid);
rs.updateRow();
trid++;
jLabel9.setText("Arranging transactions... Please wait.");
}
}
catch (sqlException ex) {
JOptionPane.showMessageDialog(null, "UnkNown Error!! Data cannot be displayed!"+ex);
}
finally{try{stmtt.close(); rs.close(); conn.close(); }catch(sqlException ex){}}
}
为什么这个方法给我一个updateLong不允许的错误,因为ResultSet不是可更新的ResultSet,而我的结果集已经设置为可更新.
我正在使用德比数据库.
解决方法:
根据Derby Documentation,具有ORDER BY的查询不可更新:
Only simple, single-table SELECT cursors can be updatable. The SELECT statement for updatable ResultSets has the same Syntax as the SELECT statement for updatable cursors. To generate updatable cursors:
- The
SELECT
statement must not include anORDER BY
clause.- The underlying Query must be a SelectExpression.
- The SelectExpression in the underlying Query must not include:
disTINCT
- Aggregates
GROUP BY
clauseHAVING
clauseORDER BY
clause- The
FROM
clause in the underlying Query must not have:
- more than one table in its
FROM
clause- anything other than one table name
- SelectExpressions
- subqueries
- If the underlying Query has a
WHERE
clause, theWHERE
clause must not have subqueries.
换句话说,您不能包含ORDER BY,但这会破坏您的目的(因为您似乎正在重新编号某些标识符).
您需要使用某些查询重新编号而不在JDBC中进行处理,或者您需要使用两个Statement对象,一个用于查询行,另一个用于更新它们.
德比也是does not support TYPE_SCROLL_SENSITIVE
结果集.根据文档,Derby支持:
> TYPE_FORWARD_ONLY
和
> TYPE_SCROLL_INSENSITIVE
请注意,您当前的代码不需要TYPE_SCROLL_INSENSITIVE,因为您只将其作为前向处理.