
Statement.executeUpdate("INSERT INTO Customers " + "VALUES (1004, 'Cramden', 'Mr.', 'New York', 2001)") Īs you can see, this is pretty easy (once you've seen how it’s done). Statement.executeUpdate("INSERT INTO Customers " + "VALUES (1003, 'Flinstone', 'Mr.', 'Bedrock', 2003)") Statement.executeUpdate("INSERT INTO Customers " + "VALUES (1002, 'McBeal', 'Ms.', 'Boston', 2004)") We can just re-use the Statement object to insert our new values: Inserting the other three records is just as easy as inserting this record. (Snum stands for Salesperson Number, which we'll use later to link this table to our Salesperson table.) 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). (I show the complete process of obtaining a database Connection object below.) Statement.executeUpdate("INSERT INTO Customers " + "VALUES (1001, 'Simpson', 'Mr.', 'Springfield', 2001)") Īs 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. Statement statement = conn.createStatement() create a Statement from the connection
Insert data into database how to#
Here’s an example of how to create a Java Statement object, and then insert a record for a person named Mr. When Sun (now Oracle) created JDBC, they intended to “make the simple things simple.” Step 2: Execute the JDBC INSERT statement If you’re comfortable with SQL, this is a simple process.

Execute a SQL INSERT command through the JDBC Statement object.Inserting data into a SQL database table using Java is a simple two-step process: Here’s what the Customers database table looks like: Cnum In all of my examples in this series, I’ll be working with a database named Demo that has a database table named Customers.

Step 1: A sample databaseīefore getting into the SQL INSERT statements, you need to know what the sample database table looks like. In this article I’ll take the next step and show how to insert data into a database table using Java, JDBC, and SQL. In my first Java JDBC tutorial (How to connect to a JDBC database) I demonstrated how to connect your Java applications to standard SQL databases like MySQL, SQL Server, Oracle, SQLite, and others using the JDBC Connection object.
