JDBC Prepared Statement Addbatch

The code illustrate an example from JDBC Prepared statement Add batch. In this code we have defined a class Jdbc PreparedstatementAddbatch,

Ads

Tutorials   
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
Ads

JDBC Prepared Statement Addbatch

     

The code illustrate an example from JDBC Prepared statement Add batch. In this code we have defined a class Jdbc PreparedstatementAddbatch, The class include a main method that follows the list of steps -

1)  Importing a java.sql package that contain the list of all classes ,provides you a network interface enables you to communicate between front end -back end database.

2) Loading a driver by simply calling a class.forName( ),that accept driver class as argument

3) DriverManager.getConnection ( ) - This method is used to built a connection between url and database.

4) prepare Statement ( ) -This method is used to create a sql object. An object of connection class  is used to execute the query in the database.

5) set String ( ) - This method is used to set the value as string in place of question mark place holder.

6) addBatch ( ) - This method is used to add a set of parameter to the batch of command to this sql server prepared object.

7) execute Batch ( ) - This method is used to submit a set of command in sql query to the database, In case all the commands successfully, return you an array update count.

8) prepare Statement ( ) -This method is used when you want to execute the statement object many times, this reduces the execution time to execute the object statement.

9) execute query ( ) - This method returns you a record set stored in a result set, The select statement is used to retrieve the record set from a database.

Finally the println print the Table name, Table field name and its type.In case the exception exists in a try block, the catch block caught and handle the exception. 

 

import java.sql.*; public class JdbcPreparedstatementAddbatch { 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); pst = con.prepareStatement("insert into lib value(?,?)"); con.setAutoCommit(false); pst.setString(1, "6"); pst.setString(2, "106"); pst.addBatch(); pst.setString(1, "7"); pst.setString(2, "107"); pst.addBatch(); pst.setString(1, "8"); pst.setString(2, "108"); pst.addBatch(); pst.executeBatch(); con.commit(); pst.close(); String sql = "select * from lib"; pst = con.prepareStatement(sql); rs = pst.executeQuery(); System.out.println("rno\tlibno"); while (rs.next()) { System.out.print(rs.getString(1) + " \t"); System.out.println(rs.getString(2)); } rs.close(); pst.close(); con.close(); } catch (Exception e) { e.printStackTrace();
       //In case of error call con.rollback()
        }
    }
}

Output

Table Name : Employees
Field		Type
--------------------
EmployeeID	 11
FirstName	 50
LastName	 50

Download code

Advertisements

Share on Google+Share on Google+

JDBC Prepared Statement Addbatch

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

Advertisements

 

Discuss: JDBC Prepared Statement Addbatch  

Post your Comment


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

Ads

 

Ads