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 Oracle server:

/* OracleRowSetInsert.java
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
import java.sql.*;
import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.JdbcRowSet;
public class OracleRowSetInsert {
  public static void main(String [] args) {
    try {

// RowSetFactory should be used now
//      JdbcRowSet jrs 
//        = new com.sun.rowset.JdbcRowSetImpl();
      JdbcRowSet jrs 
        = RowSetProvider.newFactory().createJdbcRowSet();

// Set the connection URL for the DriverManager
      jrs.setUrl("jdbc:oracle:thin:Herong/TopSecret"
        +"@//localhost:1521/XE");

// Set JdbcRowSet to be updatable
      jrs.setConcurrency(java.sql.ResultSet.CONCUR_UPDATABLE);

// 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();
    }
  }
}

The program executed correctly:

C:\herong>\Progra~1\java\jdk1.8.0_45\bin\java
   -cp .;\local\lib\ojdbc6.jar
   OracleRowSetInsert

java.sql.SQLException: 
Invalid operation for read only resultset: moveToInsertRow
   at oracle.jdbc.driver.BaseResultSet.moveToInsertRow(...)
   at com.sun.rowset.JdbcRowSetImpl.moveToInsertRow(...)
   at OracleRowSetInsert.main(OracleRowSetInsert.java:31)   

Notice that my program failed because the Oracle JDBC driver does not support the updatable RowSet function. I have tried to call the setConcurrency() method. But it did not help:

// Set JdbcRowSet to be updatable
       jrs.setConcurrency(java.sql.ResultSet.CONCUR_UPDATABLE);

Last update: 2015.

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

Oracle - Reference Implementation of JdbcRowSet

 Overview of RowSet Objects

 Installation of JdbcRowSet Reference Implementation

 Connecting JdbcRowSet to Database Servers

 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

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Microsoft SQL Server 2005 Express Edition

 Microsoft JDBC Driver for SQL Server - sqljdbc42.jar

 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

 Additional Tutorial Notes to Be Added

 Outdated Tutorials

 References

 PDF Printing Version