JDBC INSERT Statement

0
133

In this tutorial you will learn about JDBC INSERT statement.

Subject: JDBC INSERT statement

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

First we need to create a Java Statement object.

Post creation of Java Statement object we need to execute a SQL INSERT command through the JDBC Statement object.

If anyone is aware of basic SQL queries, this is very easy to learn.

How to 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. Rahul, of a town named Bangalore:
[java]

// create a Statement from the connection

Statement statement = conn.createStatement ();

// insert the data

statement.executeUpdate (“INSERT INTO Customers “+ “VALUES (1001, ‘Rahul’, ‘Mr.’, ‘Bangalore’, 2001)”);

JDBC Insert Example with Java Program:

/**

* JdbcInsertExample.java basic program about how to INSERT data into an SQL

* Database using Java JDBC.

*/

import java.sql.*;

class JdbcInsertExample {

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 (1001, ‘Rahul’, ‘Mr.’, ‘Bangalore’, 2001)”);

conn.close ();

}

catch (Exception e) {

System.err.println (“Got an exception! “);

System.err.println (e.getMessage ());

}

}
[/java]

 

 

Readers might read also:

Facebook Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.