问题
When I invoke following rows:
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = statement.executeQuery("select * from user");
resultSet.next();
resultSet.refreshRow();//exception throws here
I see following exception:
com.mysql.jdbc.NotUpdatable: Result Set not updatable.This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, the query must select only one table, can not use functions and must select all primary keys from that table. See the JDBC 2.1 API Specification, section 5.6 for more details.This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, the query must select only one table, can not use functions and must select all primary keys from that table. See the JDBC 2.1 API Specification, section 5.6 for more details.
I wondered this exception because if read refreshRow method javadoc we can find following:
The refreshRow method provides a way for an application to explicitly tell the JDBC driver to refetch a row(s) from the database
Thus following direction: database --> ResultSet
I have following understanding:
updatable is possibility to use following direction:
ResultSet --> database
Thus I don't understand cause of problem.
please clarify.
回答1:
When you are using, ResultSet.CONCUR_READ_ONLY, you are getting an exception, that
com.mysql.jdbc.NotUpdatable: Result Set not updatable.This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, the query must select only one table, can not use functions and must select all primary keys from that table. See the JDBC 2.1 API Specification, section 5.6 for more details.\
So, changing ResultSet.CONCUR_READ_ONLY to ResultSet.CONCUR_UPDATABLE, solved your problem. Correct?
Now, as I understand, you don't want to do that. Am I right? If yes, then I think you are confusing ResultSet with Database,
- ResultSet.CONCUR_READ_ONLY, means read only ResultSet, not Database, similarly
- ResultSet.CONCUR_UPDATABLE, means updatable ResultSet, not Database.
The method, resultSet.refreshRow(), suppose to update the resultSet, hence it rightly requires updatable ResultSet. I hope it's clear now.
来源:https://stackoverflow.com/questions/25582698/why-do-i-see-notupdatable-when-i-invoke-resultset-refreshrow