JDBC Tutorials - Herong's Tutorial Examples - v3.12, by Dr. Herong Yang
Inserting Rows with JdbcRowSet Objects
This section describes how to insert data rows with a JdbcRowSet object.
Based on the JDBC documentation, a JdbcRowSet object is extended from a ResultSet object and defined to be updatable by default. This means that you can use JdbcRowSet objects to update, delete or insert rows back to target tables in the database server. I wrote a sample program to use a JdbcRowSet object to insert 2 data rows back to the Profile table in MySQL server:
/* MySqlRowSetInsert.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.sql.*; import javax.sql.rowset.RowSetProvider; import javax.sql.rowset.JdbcRowSet; public class MySqlRowSetInsert { public static void main(String [] args) { try { // Get a new JdbcRowSet object with the default implementation JdbcRowSet jrs = RowSetProvider.newFactory().createJdbcRowSet(); // Set the connection URL for the DriverManager jrs.setUrl("jdbc:mysql://localhost/HerongDB" +"?user=Herong&password=TopSecret"); // Set a SQL statement with parameters jrs.setCommand("SELECT * FROM Profile WHERE 1=2"); // Connect and run the statement jrs.execute(); // Move to the insert row jrs.moveToInsertRow(); // Set column values and insert jrs.updateString("FirstName", "Herong"); jrs.updateString("LastName", "Yang"); jrs.insertRow(); // Repeat for another row jrs.updateString("FirstName", "Bush"); jrs.updateString("LastName", "Gate"); jrs.insertRow(); System.out.println("2 rows inserted."); // Close resource jrs.close(); } catch (Exception e) { e.printStackTrace(); } } }
Try to run the program with JDK 13. I got an error:
herong> java -cp .:mysql-connector-java.jar MySqlRowSetInsert.java java.sql.SQLException: ResultSet is not updatable at java.sql.rowset/com.sun.rowset.JdbcRowSetImpl.checkTypeConcurrency() at java.sql.rowset/com.sun.rowset.JdbcRowSetImpl.updateString() at MySqlRowSetInsert.main(MySqlRowSetInsert.java:29)
Okay. The ResultSet is having a problem. So I updated the program to print its concurrency type:
/* MySqlRowSetInsert2.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.sql.*; import javax.sql.rowset.RowSetProvider; import javax.sql.rowset.JdbcRowSet; public class MySqlRowSetInsert2 { public static void main(String [] args) { try { // Get a new JdbcRowSet object with the default implementation JdbcRowSet jrs = RowSetProvider.newFactory().createJdbcRowSet(); // Set the connection URL for the DriverManager jrs.setUrl("jdbc:mysql://localhost/HerongDB" +"?user=Herong&password=TopSecret"); // Set a SQL statement with parameters jrs.setCommand("SELECT * FROM Profile WHERE 1=2"); // Connect and run the statement jrs.execute(); System.out.println("getConcurrency() = "+jrs.getConcurrency()); System.out.println("CONCUR_READ_ONLY = "+ResultSet.CONCUR_READ_ONLY); System.out.println("CONCUR_UPDATABLE = "+ResultSet.CONCUR_UPDATABLE); // Move to the insert row jrs.moveToInsertRow(); // Set column values and insert jrs.updateString("FirstName", "Herong"); jrs.updateString("LastName", "Yang"); jrs.insertRow(); // Repeat for another row jrs.updateString("FirstName", "Bush"); jrs.updateString("LastName", "Gate"); jrs.insertRow(); System.out.println("2 rows inserted."); // Close resource jrs.close(); } catch (Exception e) { e.printStackTrace(); } } }
Run the updated program with JDK 13. I got the same error with more information:
herong> java -cp .:mysql-connector-java.jar MySqlRowSetInsert2.java getConcurrency() = 1008 CONCUR_READ_ONLY = 1007 CONCUR_UPDATABLE = 1008 java.sql.SQLException: ResultSet is not updatable ...
The output is confusing. The getConcurrency() returns CONCUR_UPDATABLE. But it throws "ResultSet is not updatable" when calling the updateString() method. Something is wrong with the MySQL JDBC 8.0.19 driver.
The same program had no issue with JDK 8 and the MySQL JDBC 5.1.36 driver:
The program executed correctly:
herong> javac MySqlRowSetInsert.java herong> java -cp .:mysql-connector-java-5.1.36-bin.jar MySqlRowSetInsert 2 rows inserted.
Table of Contents
JDBC (Java Database Connectivity) Introduction
Installing and Running Java DB - Derby
Derby (Java DB) JDBC DataSource Objects
Java DB (Derby) - DML Statements
Java DB (Derby) - ResultSet Objects of Queries
Java DB (Derby) - PreparedStatement
MySQL JDBC Driver (MySQL Connector/J)
►MySQL - Reference Implementation of JdbcRowSet
Connecting JdbcRowSet to Database Servers
Sun Implementation of JdbcRowSet API
Connecting JdbcRowSet with a Connection URL
Connecting JdbcRowSet with a Predefined Connection Object
Connecting JdbcRowSet with a Predefined ResultSet Object
Connecting JdbcRowSet with JNDI Directory Service
JdbcRowSet Query Statement with Parameters
►Inserting Rows with JdbcRowSet Objects
MySQL - JBDC CallableStatement
MySQL CLOB (Character Large Object) - TEXT
MySQL BLOB (Binary Large Object) - BLOB
Oracle Express Edition Installation on Windows
Oracle - Reference Implementation of JdbcRowSet
Oracle - JBDC CallableStatement
Oracle CLOB (Character Large Object) - TEXT
Oracle BLOB (Binary Large Object) - BLOB
Microsoft SQL Server Express Edition
Microsoft JDBC Driver for SQL Server
Microsoft JDBC Driver - Query Statements and Result Sets
Microsoft JDBC Driver - DatabaseMetaData Object
Microsoft JDBC Driver - DDL Statements
Microsoft JDBC Driver - DML Statements
SQL Server - PreparedStatement
SQL Server CLOB (Character Large Object) - TEXT
SQL Server BLOB (Binary Large Object) - BLOB
JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver
JDBC-ODBC Bridge Driver - Flat Text Files
JDBC-ODBC Bridge Driver - MS Access
JDBC-ODBC Bridge Driver - MS SQL Server
Summary of JDBC Drivers and Database Servers