Home Jdbc JDBC Insert Preparedstatement
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

JDBC Insert Preparedstatement

Advertisement
In this Tutorial we want to describe you a code that helps you in understanding JDBC Insert Prepared Statement.

JDBC Insert Preparedstatement

     

The java.sql package contain a prepared statement, This prepared statement interface is available from the Statement interface. This is used to pass parameters at runtime. In this Tutorial we want to describe you a code that helps you in understanding JDBC Insert Prepared Statement. For this we have a class Jdbc Insert Prepared statement, Inside this class we have a main method that include the list of following steps -

 import a package java.sql - this provides you a network interface that help in communicating between front end and backend in database.

  Loading a driver followed by calling a class.forname( ),this class accept a driver class as argument. The Driver Manager call getConnection (),which returns you a connection object and this is used to built a connection between url and database.

  prepareStatement ( ) : This method is useful when you want to execute the same statement object many times, This reduces the time of execution to use the same object.The method accept a sql object as parameter, that include the list of inserted values into the table set The String( ) : Set String XXX  is defined in prepared statement to substitute a value in the question  mark placeholder as string,int .In the code, we substitute a question mark value as string.executeUpdate ( ) :This method provides you the numbers of affected rows in the table and execute the statement in a given code.executeQuery ( ):This method provides you a set of records retrieved from a table in the database. The select  statement is used to obtain the records from the result set.

 next ( ) : This method  retain  you the next elements in a series.

Finally the print print the No, Name from a employee table present in database. In case there is an exception in try block, the subsequent catch block check and handle the exception.

JdbcInsertPreparedstatement.java


import java.sql.*;

public class JdbcInsertPreparedstatement {

  public static void main(String args[]) {

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

  String url = "jdbc:mysql://localhost:3306/";
  String db = "komal";
  String driver = "com.mysql.jdbc.Driver";
  String user = "root";
  String pass = "root";

  try {
  Class.forName(driver);
  con = DriverManager.getConnection(url + db, user, pass);
  con.setAutoCommit(false);// Disables auto-commit.

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

  pst.setString(1"Rakesh");
  pst.setString(2"Kanwal");
  pst.setString(3"Delhi");
  pst.setString(4"Indian");

  pst.executeUpdate();

  sql = "select * from employees";
  rs = pst.executeQuery(sql);

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

  catch (Exception e) {
  System.out.println(e);
  }
  }
}

Output

No  	Name
Girish     	Tewari     	Rohini     	Indian
Komal     	Singh     	Rohini     	Indian
Sandeep     	kumar     	Rohini     	Indian
Amit     	Singh     	Rohini     	Indian
Girish     	Tewari     	Rohini     	Indian
Darshan     	Tewari     	Rohini     	Indian
Rakesh     	Kanwal     	Delhi     	Indian

Download code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

Ask Questions?    Discuss: JDBC Insert Preparedstatement  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
karuna
August 2, 2012
jdbc

can we pass table name dynamically..using preparestatement
Manoj
June 25, 2013
question

hi.. i wants avoid the duplicate data insertion, each time i need enterd data will be unique bye user, if data will be duplicated then it will gives error message user alrady exist plz provide that code, and iam check his username,password, mob.no is also unique...
DMCA.com