In My first JDBC tutorial (JAVA DATABASE CONNECTION INTRODUCTION) I demonstrated how to connect your Java applications to standard SQL databases like MySQL, SQL Server, Oracle, SQLite, and others using the JDBC
In this article I'll take the next step — I'll show you how to insert data into a data table using Java, JDBC, and SQL.
Connection
object.In this article I'll take the next step — I'll show you how to insert data into a data table using Java, JDBC, and SQL.
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 databasenamed
Demo
that has a database table named Customers
.
Here’s what the
Customers
database table looks like:101 | Samuel | Mr. | Ejigbo | 2010 |
102 | Dawodu | Ms. | Isolo | 2011 |
103 | Okafor | Mrs. | Oshodi | 2012 |
104 | Ugochukwu | Miss. | Oke-Afa | 2013 |
Table 1: My sample Customers database table contains these four sample records.
How to create a JDBC INSERT statement
Inserting data into a SQL database table using Java is a simple two-step process:
- Create a Java
Statement
object. - Execute a SQL
INSERT
command through the JDBCStatement
object.
If you’re comfortable with SQL, this is an easy process. When Sun (now Oracle) created JDBC, they intended to “make the simple things simple.”
Execute the JDBC INSERT statement
Here’s an example of how to create a
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 (101, 'Samuel', 'Mr.', 'Ejigbo', 2010)");
As you can see, you just (1) create a JDBC
Statement
object from your Connection
instance, and (2) run your SQL INSERT
statement using the Statement
object's executeUpdate
method.
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, and use our new values:statement.executeUpdate("INSERT INTO Customers " + "VALUES (102, 'Dawodu', 'Ms.', 'Isolo', 2011)"); statement.executeUpdate("INSERT INTO Customers " + "VALUES (103, 'Okafor', 'Mrs.', 'Oshodi', 2012)"); statement.executeUpdate("INSERT INTO Customers " + "VALUES (104, 'Ugochukwu', 'Miss.', 'Oke-Afa', 2013)");
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 JdbcInsert1.java 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.*; /** * JdbcInsert1.java - 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://200.210.220.1:1114/Demo"; Connection conn = DriverManager.getConnection(url,"",""); Statement st = conn.createStatement(); st.executeUpdate("INSERT INTO Customers " + "VALUES (101, 'Samuel', 'Mr.', 'Ejigbo', 2010)"); st.executeUpdate("INSERT INTO Customers " + "VALUES (102, 'Dawodu', 'Ms.', 'Isolo', 2011"); st.executeUpdate("INSERT INTO Customers " + "VALUES (103,'Okafor', 'Mrs.', 'Oshodi', 2012)"); st.executeUpdate("INSERT INTO Customers " + "VALUES (104, 'Ugochukwu', 'Miss.', 'Oke-Afa', 2013)"); conn.close(); } catch (Exception e) { System.err.println("There is error in the code! "); System.err.println(e.getMessage()); } } }
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.)Summary
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.
No comments:
Post a Comment