Home Jdbc JDBC Prepared Statement Insert
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JDBC Prepared Statement Insert

Advertisement
The Tutorial illustrates a program in JDBC Prepared Statement Insert.

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.

JdbcPreparedstatementInsert.java
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 {

  Class.forName(driver);
  con = DriverManager.getConnection(connection);
  con.setAutoCommit(false);

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

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

  pst.executeUpdate();
  pst.close();

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

  rs = pst.executeQuery();

  while (rs.next()) {
  System.out.print(rs.getString(1) + "\t");
  System.out.print(rs.getString(2) + "\t");
  System.out.println(rs.getString(3));
  }
  rs.close();
  pst.close();
  con.close();

  } catch (Exception e) {
  }
  }
}

Output

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

Download code

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 19, 2008

Ask Questions?    Discuss: JDBC Prepared Statement Insert   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com