Anda di halaman 1dari 8

alvin alexander

favorite books categories Java JDBC Insert Example:

How to insert data into a SQL
alaska (25) table
android (138)
By Alvin Alexander. Last updated: August 16 2018
best practices (63)
career (50)
colorado (21) In my
cvs (27) first Table of Contents
design (33)
drupal (120)
eclipse (6)
◦ Step 1: A sample database
funny (3)
◦ How to create a JDBC
gadgets (108)
git (15)
INSERT statement
Hello, Scala intellij (4)
◦ Step 2: Execute the JDBC
java (429)
jdbc (26) INSERT statement
swing (74)
jsp (9)
◦ The program
latex (26)
◦ Related Java JDBC content
linux/unix (289)
mac os x (315) ◦ Summary
mysql (54)
ooa/ood (11)
perl (156)
php (97)
postgresql (17)
programming (43) Table of Contents 
ruby (56)
tutorial (How to connect to a JDBC database)
scala (640)
sencha (23) I demonstrated how to connect your Java
servlets (10) applications to standard SQL databases like
technology (84) MySQL, SQL Server, Oracle, SQLite, and
testing (13)
uml (24)
others using the JDBC Connection object.
zen (47)
In this article I’ll take the next step and show
how to insert data into a database table using
Java, JDBC, and SQL.

Step 1: A sample database

Before getting into the SQL INSERT statements, you need to know what the sample database table
looks like. In all of my examples in this series, I’ll be working with a database named Demo that has
a database table named Customers.

Here’s what the Customers database table looks like:

Cnum Lname Salutation City Snum

1001 Simpson Mr. Springfield 2001

1002 McBeal Ms. Boston 2004

1003 Flinstone Mr. Bedrock 2003

1004 Cramden Mr. New York 2001

How to create a JDBC INSERT statement

Inserting data into a SQL database table using Java is a simple two-step process:

1. Create a Java Statement object.

2. Execute a SQL INSERT command through the JDBC Statement object.

If you’re comfortable with SQL, this is a simple process. When Sun (now Oracle) created JDBC, they intended to “make the
simple things simple.”

Step 2: Execute the JDBC INSERT statement

Here’s an example of how to create a Java Statement object, and then insert a record for a person named Mr. Simpson, of a
town named Springfield:

// create a Statement from the connection

Statement statement = conn.createStatement();
// insert the data
statement.executeUpdate("INSERT INTO Customers " + "VALUES (1001, 'Simpson', 'Mr.', 'Springfield', 2001)");

As this shows, you (1) create a JDBC Statement object from your Connection instance, and (2) run your SQL INSERT statement
using the Statement object's executeUpdate method. (I show the complete process of obtaining a database Connection object

If you're not familiar with SQL, note that you must insert your fields in the order in which your table is defined (Cnum,
Lname, Salutation, City, and Snum). (Snum stands for Salesperson Number, which we'll use later to link this table to our
Salesperson table.)

Inserting the other three records is just as easy as inserting this record. We can just re-use the Statement object to insert our
new values:

statement.executeUpdate("INSERT INTO Customers " + "VALUES (1002, 'McBeal', 'Ms.', 'Boston', 2004)");
statement.executeUpdate("INSERT INTO Customers " + "VALUES (1003, 'Flinstone', 'Mr.', 'Bedrock', 2003)");
statement.executeUpdate("INSERT INTO Customers " + "VALUES (1004, 'Cramden', 'Mr.', 'New York', 2001)");

As you can see, this is pretty easy (once you've seen how it’s done). In a real application you’ll just replace the string
constants we’ve used with variables that you obtain from (a) an end-user or (b) an input data source.

Note: In this example, I assumed that the database table named Customers is already created. You can
create your database tables through your database management tools.
The program

To help you understand how this process works, the following source code shows a complete Java program that creates a
Connection to the database, and then inserts the data as shown previously:

import java.sql.*;

* - Demonstrates how to INSERT data into an SQL
* database using Java JDBC.
class JdbcInsert1 {
public static void main (String[] args) {
try {
String url = "jdbc:msql://";
Connection conn = DriverManager.getConnection(url,"","");
Statement st = conn.createStatement();
st.executeUpdate("INSERT INTO Customers " +
"VALUES (1001, 'Simpson', 'Mr.', 'Springfield', 2001)");
st.executeUpdate("INSERT INTO Customers " +
"VALUES (1002, 'McBeal', 'Ms.', 'Boston', 2004)");
st.executeUpdate("INSERT INTO Customers " +
"VALUES (1003, 'Flinstone', 'Mr.', 'Bedrock', 2003)");
st.executeUpdate("INSERT INTO Customers " +
"VALUES (1004, 'Cramden', 'Mr.', 'New York', 2001)");

} catch (Exception e) {
System.err.println("Got an exception! ");


If you’re interested, you can download the Java source code for the program here. You can test the
code on your own system, but note that you’ll need to change the lines where I create my url and conn objects to reflect your
own database configuration. You’ll also need the appropriate JDBC driver for the database you’re using.
Related Java JDBC content

When it comes to inserting data into a database, most developers prefer to use a PreparedStatement instead of a Statement, as
the PreparedStatement is more secure. (But when you’re first learning JDBC it’s helpful to see the Statement first.) Please see
these articles for more information on how to use a PreparedStatement:

◦ How do I create and use a JDBC PreparedStatement?

◦ A sample JDBC PreparedStatement that uses SQL UPDATE

In summary, inserting data into an SQL database table with JDBC is a simple two-step process. Just (1) create a Statement
object, and (2) use the object to run your normal SQL INSERT commands.

category: jdbc
tags: table statement sql insert sql jdbc insert jdbc java insert database query


◦ Java JDBC: A SQL SELECT query example

◦ Java PreparedStatement: A SQL INSERT example

◦ A simple Java JDBC example that shows SQL insert, update, delete, and select

◦ A Java MySQL INSERT example (using Statement)

◦ A Java MySQL INSERT example (using PreparedStatement)

◦ Spring JDBC - How to retrieve the auto-generated database key after an INSERT

books i’ve written