Performance of Inserting Rows with a ResultSet

This section describes how to measure the performance of inserting rows using a ResultSet object.

Since ResultSet objects can also be used to insert rows, I wrote the following Java sample program to measure the performance of inserting multiple rows using a ResultSet object:

 * Copyright (c) All Rights Reserved.
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DerbyPerformanceResultSet {
  public static void main(String [] args) {
    Connection con = null;
    try {
      Hashtable env = new Hashtable();
      env.put(Context.PROVIDER_URL, "file:/local/fscontext");
      Context ctx = new InitialContext(env);
      DataSource ds = (DataSource) ctx.lookup("DerbyTestDB");
      con = ds.getConnection();

// Delete all rows from the table
      Statement sta = con.createStatement();
      sta.executeUpdate("DELETE FROM Profile");

// Start the test
     int count = 10000;
     long t1 = System.currentTimeMillis();

// ResultSet to insert rows
      Statement rs = con.createStatement(

      ResultSet res = rs.executeQuery("SELECT * FROM Profile");
      Random r = new Random();
      for (int i=0; i<count; i++) {

// End the test
     long t2 = System.currentTimeMillis();
     System.out.println("ResultSet insert "+count
         +" rows with "+(t2 -t1) +" milliseconds");

    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());

Here is the result on a Windows XP system with a 997MHz processor. You should compare this with the result from other performance tutorials.

Regular Statement insert 10000 rows with 5906 milliseconds

Surprisingly, ResultSet is 26% faster than PreparedStatement, see the table below:

Statement           # of inserts   Time in ms.   Comparison
ResultSet           10000           5906          74%
PreparedStatement   10000           7953         100%
Regular Statement   10000          47531         598%

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

 PreparedStatement Overview

 PreparedStatement with Parameters

 PreparedStatement in Batch Mode

 Performance of Inserting Rows with a PreparedStatement

 Performance of Inserting Rows with a Regular Statement

Performance of Inserting Rows with a ResultSet

 Summary of JDBC Drivers and Database Servers

 Using Connection Pool with JDBC

 Archived Tutorials


 Full Version in PDF/EPUB