JDBC Tutorials - Herong's Tutorial Examples - v3.12, by Dr. Herong Yang
Performance of Inserting Rows with a PreparedStatement
This section describes how to measure the performance of inserting rows using a PreparedStatement object.
Running SQL statements using PreparedStatement objects is supposed to be faster than using regular Statement objects. To test this, I wrote the following Java program to measure the performance of inserting rows using a PreparedStatement object into an empty table:
/* OraclePerformancePreparedStatement.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.sql.*; public class OraclePerformancePreparedStatement { public static void main(String [] args) { Connection con = null; try { oracle.jdbc.pool.OracleDataSource ds = new oracle.jdbc.pool.OracleDataSource(); ds.setDriverType("thin"); ds.setServerName("localhost"); ds.setPortNumber(1521); ds.setDatabaseName("XE"); ds.setUser("Herong"); ds.setPassword("TopSecret"); 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(); // PreparedStatement to insert rows PreparedStatement ps = con.prepareStatement( "INSERT INTO Profile (ID, FirstName, LastName)" + " VALUES (?, ?, ?)"); java.util.Random r = new java.util.Random(); for (int i = 0; i < count; i++) { ps.setInt(1,i+1); ps.setString(2,Integer.toHexString(r.nextInt(9999))); ps.setString(3,Integer.toHexString(r.nextInt(999999))); ps.executeUpdate(); } ps.close(); // End the test long t2 = System.currentTimeMillis(); System.out.println("PreparedStatement insert "+count +" rows with "+(t2 -t1) +" milliseconds"); con.close(); } catch (Exception e) { e.printStackTrace(); } } }
The table below shows execution results with different computer environments:
macOS 10, 2.6 GHz processor, JDK 15, Oracle 21.3 and odbc11.jar PreparedStatement insert 10000 rows with 9962 milliseconds Windows 10, 3.6 GHz processor, JDK 17, Oracle 21.3 and odbc11.jar PreparedStatement insert 10000 rows with 14281 milliseconds Windows 7, 2.5 GHz processor, JDK 1.8, Oracle 11.2 and odbc6.jar PreparedStatement insert 10000 rows with 22574 milliseconds Windows XP, 997 MHz processor, JDK 1.6, Oracle 10.2 and odbc14.jar PreparedStatement insert 10000 rows with 7843 milliseconds
I am very surprised to see that my old Windows XP computer performed much better my new computers!
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
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
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
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