JDBC Tutorials - Herong's Tutorial Examples - v3.13, by Herong Yang
"INSERT INTO" Statements
This section describes how to insert data rows with INSERT INTO statements.
INSERT statements are used very often by database applications to insert data rows into tables. The syntax of INSERT statements is very simple. You need to provide a list of column names and a list of values for those columns. There are a couple of simple rules about INSERT statements:
INSERT statements should be executed with the executeUpdate() method. Here is a simple program that insert some rows into my table Profile:
/* DerbyMultipleInserts.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.util.*; import java.sql.*; public class DerbyMultipleInserts { public static void main(String [] args) { Connection con = null; try { con = DriverManager.getConnection("jdbc:derby://localhost/TestDB"); Statement sta = con.createStatement(); int count = 0; // insert a single row using default values count += sta.executeUpdate( "INSERT INTO Profile" + " (FirstName)" + " VALUES ('Herong')"); // insert a single row using provided values count += sta.executeUpdate( "INSERT INTO Profile" + " (FirstName, LastName, Point, BirthDate)" + " VALUES ('Janet', 'Gates', 999.99, '2004-10-13')"); // insert rows with loop with random values Random r = new Random(); for (int i=0; i<10; i++) { Float points = 1000*r.nextFloat(); String firstName = Integer.toHexString(r.nextInt(9999)); String lastName = Integer.toHexString(r.nextInt(999999)); count += sta.executeUpdate( "INSERT INTO Profile" + " (FirstName, LastName, Point)" + " VALUES ('"+firstName+"', '"+lastName+"', "+points+")"); } // How many rows were inserted System.out.println("Number of rows inserted: "+count); // Checking inserted rows ResultSet res = sta.executeQuery( "SELECT * FROM Profile"); System.out.println("List of Profiles: "); while (res.next()) { System.out.println( " "+res.getInt("ID") + ", "+res.getString("FirstName") + ", "+res.getString("LastName") + ", "+res.getString("Point") + ", "+res.getDate("BirthDate") + ", "+res.getDate("ModTime")); } res.close(); sta.close(); con.close(); } catch (Exception e) { System.err.println("Exception: "+e.getMessage()); } } }
Notice that Random class was used to generate some random strings and numbers. The output confirms that the insert statement was executed correctly:
herong> java -cp %DERBY_HOME%\lib\derbyclient.jar DerbyMultipleInserts.java herong> java -cp $DERBY_HOME/lib/derbyclient.jar DerbyMultipleInserts.java Number of rows inserted: 12 List of Profiles: 1, Herong, null, 0.0, 1999-12-31, 2016-12-31 2, Janet, Gates, 999.99, 2004-10-13, 2016-12-31 3, 1403, 896f7, 62.594772, 1999-12-31, 2016-12-31 4, 256d, aa3f5, 203.51392, 1999-12-31, 2016-12-31 5, d7a, 3a06f, 604.9995, 1999-12-31, 2016-12-31 6, 8ef, 466ca, 31.71128, 1999-12-31, 2016-12-31 7, 1a34, 59e87, 674.8019, 1999-12-31, 2016-12-31 8, 10cc, ab361, 325.30505, 1999-12-31, 2016-12-31 9, 11bd, 99552, 315.1111, 1999-12-31, 2016-12-31 10, 8c4, 68909, 23.095905, 1999-12-31, 2016-12-31 11, 17e1, 99a5d, 186.09494, 1999-12-31, 2016-12-31 12, 1ea2, 480c7, 131.6408, 1999-12-31, 2016-12-31
Table of Contents
JDBC (Java Database Connectivity) Introduction
Installing and Running Derby (Java DB)
Derby (Java DB) JDBC DataSource Objects
►Derby (Java DB) - DML Statements
Tables with Primary Key Column "GENERATED ... AS IDENTITY"
"INSERT INTO" Statements with INDENTITY Columns
Handling Date and Timestamp Values
Derby (Java DB) - ResultSet Objects of Queries
Derby (Java DB) - PreparedStatement
Summary of JDBC Drivers and Database Servers