# HG changeset patch
# User lancea
# Date 1339700718 14400
# Node ID 7bde6e4fff5c99efea38a3e42128c170fc9d8c39
# Parent 92c86cea72a805e7b6fb1a6d141125efc9b778b9
7145913: CachedRowSetSwriter.insertNewRow() throws SQLException
Reviewed-by: joehw, naoto, psandoz, forax
diff -r 92c86cea72a8 -r 7bde6e4fff5c jdk/src/share/classes/com/sun/rowset/internal/CachedRowSetWriter.java
--- a/jdk/src/share/classes/com/sun/rowset/internal/CachedRowSetWriter.java Wed Jun 13 16:48:30 2012 -0700
+++ b/jdk/src/share/classes/com/sun/rowset/internal/CachedRowSetWriter.java Thu Jun 14 15:05:18 2012 -0400
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -812,101 +812,119 @@
}
}
- /**
- * Inserts a row that has been inserted into the given
- * CachedRowSet
object into the data source from which
- * the rowset is derived, returning false
if the insertion
- * was successful.
- *
- * @param crs the CachedRowSet
object that has had a row inserted
- * and to whose underlying data source the row will be inserted
- * @param pstmt the PreparedStatement
object that will be used
- * to execute the insertion
- * @return false
to indicate that the insertion was successful;
- * true
otherwise
- * @throws SQLException if a database access error occurs
- */
- private boolean insertNewRow(CachedRowSet crs,
- PreparedStatement pstmt, CachedRowSetImpl crsRes) throws SQLException {
- int i = 0;
- int icolCount = crs.getMetaData().getColumnCount();
+ /**
+ * Inserts a row that has been inserted into the given
+ * CachedRowSet
object into the data source from which
+ * the rowset is derived, returning false
if the insertion
+ * was successful.
+ *
+ * @param crs the CachedRowSet
object that has had a row inserted
+ * and to whose underlying data source the row will be inserted
+ * @param pstmt the PreparedStatement
object that will be used
+ * to execute the insertion
+ * @return false
to indicate that the insertion was successful;
+ * true
otherwise
+ * @throws SQLException if a database access error occurs
+ */
+ private boolean insertNewRow(CachedRowSet crs,
+ PreparedStatement pstmt, CachedRowSetImpl crsRes) throws SQLException {
+
+ boolean returnVal = false;
- boolean returnVal = false;
- PreparedStatement pstmtSel = con.prepareStatement(selectCmd,
- ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
- ResultSet rs, rs2 = null;
- DatabaseMetaData dbmd = con.getMetaData();
- rs = pstmtSel.executeQuery();
- String table = crs.getTableName();
- rs2 = dbmd.getPrimaryKeys(null, null, table);
- String [] primaryKeys = new String[icolCount];
- int k = 0;
- while(rs2.next()) {
- String pkcolname = rs2.getString("COLUMN_NAME");
- primaryKeys[k] = pkcolname;
- k++;
- }
+ try (PreparedStatement pstmtSel = con.prepareStatement(selectCmd,
+ ResultSet.TYPE_SCROLL_SENSITIVE,
+ ResultSet.CONCUR_READ_ONLY);
+ ResultSet rs = pstmtSel.executeQuery();
+ ResultSet rs2 = con.getMetaData().getPrimaryKeys(null, null,
+ crs.getTableName())
+ ) {
+
+ ResultSetMetaData rsmd = crs.getMetaData();
+ int icolCount = rsmd.getColumnCount();
+ String[] primaryKeys = new String[icolCount];
+ int k = 0;
+ while (rs2.next()) {
+ primaryKeys[k] = rs2.getString("COLUMN_NAME");
+ k++;
+ }
+
+ if (rs.next()) {
+ for (String pkName : primaryKeys) {
+ if (!isPKNameValid(pkName, rsmd)) {
+
+ /* We came here as one of the the primary keys
+ * of the table is not present in the cached
+ * rowset object, it should be an autoincrement column
+ * and not included while creating CachedRowSet
+ * Object, proceed to check for other primary keys
+ */
+ continue;
+ }
- if(rs.next()) {
- for(int j=0;j