Session - 2: SEED Infotech Pvt. LTD
Session - 2: SEED Infotech Pvt. LTD
Session - 2: SEED Infotech Pvt. LTD
Session - 2
Local Transactions.
Distributed Transactions.
Prerequisite:
Write a stored procedure in Oracle which takes
employee id as the input parameter and returns
the salary of that Employee.
Step 1 – Write Stored procedure
try{
// Load Driver for Oracle.
//Get Connection
String query="{ call emp_proc (?,?) }";
CallableStatement cst=con.prepareCall(query);
}
catch(ClassNotFoundException cnfe){………}
catch(SQLException sqle){………………}
Connection Interface Revisited
try
{
// Get Connection --- Refer previous code snippet.
cst.setInt(1,100);
cst.registerOutParameter(2,Types.DOUBLE);
cst.execute();
System.out.println(cst.getDouble(2));
}
catch(ClassNotFoundException cnfe){…….}
catch(SQLException sqle){…………..}
CallableStatement Interface
try{
//Code as in previous slide
}
catch(ClassNotFoundException cnfe){…}
catch(SQLException sqle){…}
finally{
try
con.close();
}
catch(SQLException sqle){…}
}
The JDBC 2.0 API
• Two packages
java.sql package--the JDBC 2.0 core API
• Type
TYPE_FORWARD_ONLY
TYPE_SCROLL_INSENSITIVE
TYPE_SCROLL_SENSITIVE
• Concurrency
CONCUR_READ_ONLY
CONCUR_UPDATEABLE
Methods of ResultSet Interface
try{
con.setAutoCommit(false);
Statement stmt = con.createStatement();
//Execute Batch
int [] updateCounts = stmt.executeBatch();
//remaining code continues….
Code Snippet for Batch Updates
con.commit();
con.setAutoCommit(true);
} //end of try block
catch(BatchUpdateException buex) {
int [] updateCounts = buex.getUpdateCounts();
for (int i = 0; i < updateCounts.length; i++) {
System.out.print(updateCounts[i] +”\n”);
}
}
DataSource
com.dbaccess.BasicDataSource ds =null;
//Create DataSource class instance
ds=new com.dbaccess.BasicDataSource();
//set properties of datasource.
ds.setServerName("gurukul");
ds.setDatabaseName("CUSTOMER_ACCOUNTS");
ds.setDescription( "Customer accounts database for
billing”);
"The variable ‘ds’ now represents the database
CUSTOMER_ACCOUNTS installed on the server
gurukul. Any connection produced by ‘ds’ will be with
CUSTOMER_ACCOUNTS. "
Register DataSource with JNDI
DataSource ds=null;
//Lookup for Database JNDI Name
ds=(DataSource) ctx.lookup("jdbc/billingDB");
fin=new FileInputStream(“Duke.jpg”);
con.prepareStatement("INSERT into B_Tab values(?,?)");
pstat.setInt(2,id);
pstat.setBinaryStream(4,fin,(int)length);
pstat.executeUpdate();
}
catch(Exception ex){….}
RowSet
WebRowSet
JoinRowSet
FilteredRowSet
JDBCRowSet