Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

JDBC Prepared Statement Insert

The Tutorial illustrates a program in JDBC Prepared Statement Insert.


JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout

JDBC Prepared Statement Insert


The Tutorial illustrates a program in JDBC Prepared Statement Insert. In this Tutorial  the code describe the include a class JdbcPreparedstatementInsert, Inside the class we have a main method follows the list of steps as follow -

Importing a java.sql provides you a network interface that enables you to communicate between front end application in java and database in backend.

Loading a driver inside the try block by calling a class.forName ( ),that accepts a driver class as argument.

DriverManager.getConnection ( ) - The method enables you to built a connection between url and database.

In the given below code, we declare String variable sql,that is used to hold the insert value into the employees table. 

prepare Statement ( ) - Unlike create Statement, the prepare Statement is used to execute the same statement object, if it is repeated again. Normally this reduces the time of execution to execute the code.

set String ( )  - This is a method defined in prepared Statement class, When you want to substitute a question mark place holder in code, we use set XXX,In case we want to substitute a question mark place holder by string ,then we write set String( ).

execute Update ( ) - This method return you a integer value show you the number of rows inserted in a table. This also indicates a numbers of rows affected in the table.

execute Query ( ) -  This method return you the result set ,that store the value of record set. The select sql statement is used to retrieve the record from table in database.

next ( ) - This method is used to retrieve the next series element in the result set.

Finally the Print ln show you the output as specified below the code

In case the exception exists in try  block, the catch block caught and handle the exception.
import java.sql.*;

public class JdbcPreparedstatementInsert {

  static private final String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  static private final String connection = "jdbc:odbc:emp";

  public static void main(String args[]) {

  Connection con = null;
  PreparedStatement pst = null;
  ResultSet rs = null;

  try {

  con = DriverManager.getConnection(connection);

  String sql =
  "insert into Employees (FirstName,LastName) values(?,?)";
  pst = con.prepareStatement(sql);

  pst.setString(1, "Girish");
  pst.setString(2, "tewari");


  sql = "select * from Employees";
  pst = con.prepareStatement(sql);

  rs = pst.executeQuery();

  while ( {
  System.out.print(rs.getString(1) + "\t");
  System.out.print(rs.getString(2) + "\t");

  } catch (Exception e) {


1	komal	singh
2	ajay	rawat
3	bhanu	twari
4	rakesh	kanwal
5	santosh	nagi
8	Girish	tewari

Download code


Share on Google+Share on Google+

JDBC Prepared Statement Insert

Posted on: November 19, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: JDBC Prepared Statement Insert   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image