"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:

/* MySqlMultipleInserts.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.util.*;
import java.sql.*;
public class MySqlMultipleInserts {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Setting up the DataSource object - newer version
      com.mysql.cj.jdbc.MysqlDataSource ds
        = new com.mysql.cj.jdbc.MysqlDataSource();

// Setting up the DataSource object - older version
      // com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds
      //   = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource();

      ds.setServerName("localhost");
      ds.setPortNumber(3306);
      ds.setDatabaseName("HerongDB");
      ds.setUser("Herong");
      ds.setPassword("TopSecret");

// Getting a connection object and statement object
      con = ds.getConnection();
      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, '1984-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.getDouble("Point")
           + ", "+res.getDate("BirthDate")
           + ", "+res.getTimestamp("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. However, the default ModTime TIMESTAMP DEFAULT '2026-12-31 23:59:59.999' is not respected as expected. I am not sure why.

Number of rows inserted: 12
List of Profiles:
  1, Herong, null, 0.0, 2000-12-31, 2027-01-01 14:00:00.0
  2, Janet, Gates, 999.99, 1984-10-13, 2027-01-01 14:00:00.0
  3, 2122, af153, 679.05695, 2000-12-31, 2027-01-01 14:00:00.0
  4, 236b, 6743b, 832.10406, 2000-12-31, 2027-01-01 14:00:00.0
  5, 1037, 5c6b7, 816.14056, 2000-12-31, 2027-01-01 14:00:00.0
  6, 1229, 2ae95, 969.3753, 2000-12-31, 2027-01-01 14:00:00.0
  7, 1ba7, 2dce0, 246.53387, 2000-12-31, 2027-01-01 14:00:00.0
  8, 2689, a4e7d, 392.97043, 2000-12-31, 2027-01-01 14:00:00.0
  9, 246c, 14100, 969.9332, 2000-12-31, 2027-01-01 14:00:00.0
  10, 94c, 77a0, 422.45908, 2000-12-31, 2027-01-01 14:00:00.0
  11, 221e, 2b9a0, 325.69705, 2000-12-31, 2027-01-01 14:00:00.0
  12, 405, d3eb0, 210.8736, 2000-12-31, 2027-01-01 14:00:00.0

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 MySQL Installation on Windows

MySQL JDBC Driver (MySQL Connector/J)

 MySQL Connector/J - Download and Installation

 Loading JDBC Driver for MySQL Server

 JDBC Driver Connection URL

 Creating Connections with DataSource Class

 Getting Driver and Server Information

 Creating Tables with AUTO_INCREMENT Columns

"INSERT INTO" Statements

 MySQL - PreparedStatement

 MySQL - Reference Implementation of JdbcRowSet

 MySQL - JBDC CallableStatement

 MySQL CLOB (Character Large Object) - TEXT

 MySQL BLOB (Binary Large Object) - BLOB

 Using Connection Pool with JDBC

 Archived Tutorials

 References

 Full Version in PDF/EPUB