JDBC Tutorials - Herong's Tutorial Examples - 3.10, by Dr. Herong Yang
Performance of Inserting Rows with a Regular Statement
This section describes how to measure the performance of inserting rows using a Statement object.
To compare the performance result from the previous result with regular Statement objects. I wrote the following Java program to measure the performance of inserting rows using a regular Statement object into an empty table:
/* SqlServerPerformanceRegularStatement.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.sql.*; public class SqlServerPerformanceRegularStatement { public static void main(String [] args) { Connection con = null; try { com.microsoft.sqlserver.jdbc.SQLServerDataSource ds = new com.microsoft.sqlserver.jdbc.SQLServerDataSource(); ds.setServerName("localhost"); // ds.setPortNumber(60782); ds.setInstanceName("SQLEXPRESS"); ds.setDatabaseName("AdventureWorks2014"); ds.setUser("Herong"); ds.setPassword("T0pSecret"); 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(); // Regular Statement to insert rows Statement rs = con.createStatement(); java.util.Random r = new java.util.Random(); for (int i = 0; i < count; i++) { rs.executeUpdate("INSERT INTO Profile (FirstName, LastName)" +" VALUES ('"+Integer.toHexString(r.nextInt(9999)) +"', '"+Integer.toHexString(r.nextInt(999999))+"')"); } rs.close(); // End the test long t2 = System.currentTimeMillis(); System.out.println("Regular Statement insert "+count +" rows with "+(t2 -t1) +" milliseconds"); con.close(); } catch (Exception e) { e.printStackTrace(); } } }
Here is the result on a Windows 7 system with a 2.5GHz processor, running JDK 1.8, SQL Server 2014 and JDBC Driver 4.2:
Regular Statement insert 10000 rows with 13872 milliseconds
Comparing with PreparedStatement, regular Statement performs much slower than PreparedStatement, see the table below:
Operationg System: Windows 7 CPU speed: 2.5GHz JVM version: 1.8.0_45 SQL Server: 2014 JDBC driver: JDBC Driver 4.2 Statement # of inserts Time in ms. Comparison PreparedStatement 10000 9701 100% Regular Statement 10000 13872 143%
As a comparison, here is the result of SqlServerPerformanceRegularStatement.java on a Windows XP system with a 997MHz processor, running JDK 1.6, SQL Server 2005 and JDBC Driver 1.0, which was actually faster:
Regular Statement insert 10000 rows with 6750 milliseconds
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
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
Create a New User in SQL Server
Creating a Table with an IDENTITY Column
Inserting Rows to the Test Table
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
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