In traditional jdbc programming, to perform any transaction (insert, update and delete) we must send a separate request to database. If there is 'n' number of transactions then we must make 'n' number of request to the database and finally it leads to poor performance.
To avoid the above disadvantages we must use batch processing. Batch processing is the process of grouping 'n' number of interrelated transactions in a single unit and processing at a same time.
For example:
con.setAutoCommit (false);
For example:
st.addBatch ("insert into dept values (10,"abc","hyd")");
For example:
int res []=st.executeBatch ();
This method returns individual values of DML statements.
For example:
con.commit ();
For example:
con.rollback ();
Write a java program which illustrates the concept of Batch processing?
Answer:
import java.sql.*; class BatchProConcept { public static void main(String[] args) throws Exception { Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver"); System.out.println("DRIVERS LOADED..."); Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger"); System.out.println("CONNECTION ESTABLISHED..."); con.setAutoCommit(false); Statement st = con.createStatement(); st.addBatch("insert into student values (3, 'j2ee')"); st.addBatch("delete from student where sno=1"); st.addBatch("update student set sname='java' where sno=2"); int res[] = st.executeBatch(); for (int i = 0; i < res.length; i++) { System.out.println("NUMBER OF ROWS EFFECTED : " + res[i]); } con.commit(); con.rollback(); con.close(); }// main };// BatchProConcept
With batch processing we can obtain effective performance to the jdbc applications by executing group of SQL DML statements.
Write a java program to create a table through frontend application?
Answer:
import java.sql.*; class CreateTable { public static void main(String[] args) { try { Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver"); System.out.println("DRIVERS LOADED..."); Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger"); System.out.println("CONNECTION ESTABLISHED..."); Statement st = con.createStatement(); int i = st.executeUpdate("create table kalyan (eno number (4), ename varchar2 (15))"); System.out.println("TABLE CREATED..."); con.close(); } catch (Exception e) { e.printStackTrace(); } }// main };// CreateTable