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) {
    String tzid = java.util.TimeZone.getDefault().getID();
    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&serverTimezone="+tzid);

// 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) {
    String tzid = java.util.TimeZone.getDefault().getID();
    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&serverTimezone="+tzid);

// 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

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

MySQL - Reference Implementation of JdbcRowSet

 Overview of RowSet Objects

 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

 Using Connection Pool with JDBC

 Archived Tutorials

 References

 Full Version in PDF/EPUB