在将数据插入到一个表的fileds中,然后将resultSet SQL语句更改为另一个表,并插入到表2的字段中时,我发生了此错误:
SEVERE:null com.mysql.jdbc.NotUpdatable:Result set not updatable。此结果集必须来自已创建的语句 结果集类型为ResultSet.CONCUR_UPDATABLE,查询必须 只选择一个表,不能使用功能,必须全选 该表中的主键。请参阅JDBC 2.1 API规范, 第5.6节了解更多详情。
resultSet语句设置为可更新:
statement = (Statement) connection.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_INSENSITIVE);
表1和表格的结果集:
resultSet = (ResultSet) statement.executeQuery(SELECT * FROM employees);
+----------------------+-----------+----------+------------+----------------------------+----------------+
| socialSecurityNumber | firstName | lastName | birthday | employeeType | departmentName |
+----------------------+-----------+----------+------------+----------------------------+----------------+
| 111-11-1111 | John | Smith | 1945-01-02 | salariedEmployee | R&D |
| 222-22-2222 | Sue | Jones | 1961-02-03 | commissionEmployee | SALES |
| 234435 | ciaran | mooney | 2013-11-28 | commissionEmployee | Sales |
| 333-33-3333 | Bob | Lowis | 1958-10-05 | basePlusCommissionEmployee | SALES |
| 444-44-4444 | Karen | Price | 1972-05-25 | hourlyEmployee | HR |
+----------------------+-----------+----------+------------+----------------------------+----------------+
表2:
+----------------------+------------+----------------+-------+
| socialSecurityNumber | grossSales | commissionRate | bonus |
+----------------------+------------+----------------+-------+
| 222-22-2222 | 10100 | 0.05 | 0 |
| 222-22-2222 | 12234445 | 0.6 | 300 |
+----------------------+------------+----------------+-------+
2 rows in set (0.00 sec)
//using a object of class employee to update Table employees via resultSet
//manipulate the data in the resultSet, then upadteRow to save changes on DataBase
resultSet.moveToInsertRow();//insert a into a empty row
resultSet.updateString("socialSecurityNumber", addEmployee.getSocialSecurityNumber());
resultSet.updateString("firstName", addEmployee.getFirstName());
resultSet.updateString("lastName", addEmployee.getLastName());
resultSet.updateString("departmentName", addEmployee.getDepartment());
resultSet.updateDate("birthday", new java.sql.Date(addEmployee.getDOB().getTime()));
resultSet.updateString("employeeType", "commissionEmployee");
resultSet.updateString("departmentName", addEmployee.getDepartment());
//EXCEPTION THROWN HERE
//new resultSet for commsionRate employee
statement = (Statement) connection.createStatement(ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_INSENSITIVE);
resultSet = (ResultSet) statement.executeQuery("SELECT * FROM commissionEmployees");
resultSet.updateString("socialSecurityNumber", addEmployee.getSocialSecurityNumber());
resultSet.updateInt("grossSales", (int)addEmployee.getGrossSales());
resultSet.updateDouble("commissionRate", addEmployee.getCommissionRate());
resultSet.updateDouble("bonus", 0);
//uopdate the database*/
resultSet.insertRow();
//return cursor to orginal position
resultSet.beforeFirst();
主键是两个表中都存在的SocialSecurityNumber字段。 当我尝试将resultSet更改为上面突出显示的表2时抛出异常..
提前致谢...
答案 0 :(得分:1)
您使用错误的参数顺序调用createStatement(int resultSetType, int resultSetConcurrency)
:
<强>参数:强>
中的一个
resultSetType
- 结果集类型;ResultSet.TYPE_FORWARD_ONLY
,ResultSet.TYPE_SCROLL_INSENSITIVE
或ResultSet.TYPE_SCROLL_SENSITIVE
之一resultSetConcurrency
- 并发类型;ResultSet.CONCUR_READ_ONLY
或ResultSet.CONCUR_UPDATABLE
所以你应该把它称为:
createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
可能不一定支持某些类型和并发组合。我不知道什么适用于MySQL驱动程序。驱动程序可能会降级(JDBC 4.1规范第15.1.2节):
如果驱动程序无法以请求的类型和并发性返回
ResultSet
对象,则在确定并发之前确定适当的类型