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.

Our sample database

Before getting into our SQL statements, you need to know what our database table looks like. In all of our examples in this series, we're going to be working with a database named Demo. In today's example I'll populate a database table named Customers. Customers is a table contained within the overall Demo database.

How to create a JDBC INSERT statement

Today I'll show you that inserting data into an SQL database table is a simple two-step process:

Create a Statement object.

Execute the SQL INSERT command through the JDBC Statement object.

If you're comfortable with SQL, this is an easy process. When Sun created JDBC, they intended to "make the simple things simple".

Execute the JDBC INSERT statement

Our SQL INSERT statements are also pretty simple, especially if you're comfortable with SQL. Here's an example of how we create our Statement object, and then insert a record for a person named Mr. Simpson, of a town named Springfield:

As you can see, you just (1) create a JDBC Statement object, 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:

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, we've assumed that the database table named Customers is already created. You can create your database tables through your database management tools. For instance, we created our sample Customers tables using msqladmin for Mini SQL (mSQL), and through Interbase Server Manager tools under Windows95 for our Interbase server. You can also create your tables through JDBC code, which we'll tackle in a later tutorial.)

The JdbcInsert1.java program

We named our completed application Insert1.java. To help you understand the entire chain of events that are necessary, the full source code for the application is shown in Listing 1.

That's actually a great suggestion for a future article, but to write all that code is actually a lot of work, and I won't have time for it in the next few days.

In short, you need to read the data from the database table into a Collection of Java objects that more or less mirror the database table; create a TableModel, and populate it with this Collection; use that TableModel to construct a JTable; put that JTable into a scroll pane, and then display all of that however you want in a JFrame.