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 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: JDBC Prepared Statement Addbatch
Post your Comment