package oracle.otnsamples.oracle9ijdbc.stmtcache;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.SQLException;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OraclePreparedStatement;
import java.util.Properties;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.io.IOException;
public class StmtCacheSample {
StmtCacheFrame gui = null;
Connection conn = null;
public int[] data = null;
private String[] deptId = null;
public StmtCacheSample() {
gui = new StmtCacheFrame(this);
data = new int[3];
}
public static void main(String[] args) {
StmtCacheSample sample = new StmtCacheSample();
sample.populateDept();
}
public static Properties loadParams( String file ) throws IOException {
Properties prop = new Properties();
ResourceBundle bundle = ResourceBundle.getBundle( file );
Enumeration enum = bundle.getKeys();
String key = null;
while( enum.hasMoreElements() ) {
key = (String)enum.nextElement();
prop.put( key, bundle.getObject( key ) );
}
return prop;
}
public void dbConnect() {
try {
gui.putStatus("Trying to connect to the Database");
Properties prop = StmtCacheSample.loadParams("Connection");
OracleDataSource ods = new OracleDataSource();
ods.setDriverType("thin");
ods.setServerName((String)prop.get("HostName"));
ods.setDatabaseName((String)prop.get("SID"));
ods.setPortNumber(new Integer((String)prop.get("Port")).intValue());
ods.setUser((String)prop.get("UserName"));
ods.setPassword((String)prop.get("Password"));
conn = (OracleConnection)ods.getConnection();
gui.putStatus(" Connected to " + prop.get("SID") +
" Database as " + prop.get("UserName"));
} catch(SQLException ex) { System.out.println(
"Error in Connecting to the Database "+'\n'+ex.toString());
} catch(IOException ex) { System.out.println(
"Error in reading the properties file "+'\n'+ex.toString());
}
if(conn == null) gui.goButton.setEnabled(false);
}
public void dispatchEvent(String event) {
if (event.equals("SEARCH")) {
search();
} else if (event.equals("EXIT")) {
exitApplication();
}
}
public void search() {
int withoutCache = 0,expCache = 0,impCache = 0;
searchEmployee();
withoutCache = searchWithoutCache();
impCache = searchWithImpCache();
expCache = searchWithExpCache();
impCache += searchWithImpCache();
expCache += searchWithExpCache();
withoutCache += searchWithoutCache();
expCache += searchWithExpCache();
withoutCache += searchWithoutCache();
impCache += searchWithImpCache();
data[0] = withoutCache / 3;
data[1] = impCache / 3;
data[2] = expCache / 3;
}
public void searchEmployee() {
this.dbConnect();
OraclePreparedStatement opstmt = null;
ResultSet rset = null;
try {
String empName = gui.searchText.getText();
int selectedDept = gui.cmbDepartment.getSelectedIndex();
int lowerLimit = Integer.parseInt(gui.lowerLimit.getText());
int upperLimit = Integer.parseInt(gui.upperLimit.getText());
String mysql = new StringBuffer()
.append(" SELECT Employee_Id,First_Name || ' ' || Last_Name,Salary ")
.append(" FROM Employees ")
.append(" WHERE UPPER(First_Name) like UPPER(?) AND ")
.append(" Department_Id = ? AND ")
.append(" SALARY BETWEEN ? AND ? ")
.append(" ORDER BY First_Name,Last_Name ").toString();
gui.tableModel.clearTable();
opstmt = (OraclePreparedStatement) conn.prepareStatement(mysql);
opstmt.setString(1, empName);
opstmt.setInt(2, Integer.parseInt(deptId[selectedDept]));
opstmt.setInt(3, lowerLimit);
opstmt.setInt(4, upperLimit);
rset = opstmt.executeQuery();
while (rset.next()) {
gui.addToJTable(rset.getString(1), rset.getString(2),
rset.getString(3));
}
rset.close();
opstmt.close();
} catch (SQLException sqlEx) { gui.putStatus(" Error displaying results : " + sqlEx.toString());
}
this.dbDisconnect();
}
public int searchWithoutCache() {
this.dbConnect();
long start = 0, end = 0;
OraclePreparedStatement opstmt = null;
ResultSet rset = null;
try {
String empName = gui.searchText.getText();
int selectedDept = gui.cmbDepartment.getSelectedIndex();
int lowerLimit = Integer.parseInt(gui.lowerLimit.getText());
int upperLimit = Integer.parseInt(gui.upperLimit.getText());
String mysql = new StringBuffer()
.append(" SELECT Employee_Id,First_Name || ' ' || Last_Name,Salary ")
.append(" FROM Employees ")
.append(" WHERE UPPER(First_Name) like UPPER(?) AND ")
.append(" Department_Id = ? AND ")
.append(" SALARY BETWEEN ? AND ? ")
.append(" ORDER BY First_Name,Last_Name ").toString();
start = System.currentTimeMillis();
for (int i = 1; i <= 10; i++) {
opstmt = (OraclePreparedStatement) conn.prepareStatement(mysql);
opstmt.setString(1, empName);
opstmt.setInt(2, Integer.parseInt(deptId[selectedDept]));
opstmt.setInt(3, lowerLimit);
opstmt.setInt(4, upperLimit);
rset = opstmt.executeQuery();
rset.close();
opstmt.close();
}
end = System.currentTimeMillis();
} catch (SQLException sqlEx) { gui.putStatus(" Error searching without cache : " + sqlEx.toString());
}
this.dbDisconnect();
return ((int) (end - start));
}
public int searchWithImpCache() {
this.dbConnect();
long start = 0, end = 0;
OraclePreparedStatement opstmt = null;
ResultSet rset = null;
try {
String empName = gui.searchText.getText();
int selectedDept = gui.cmbDepartment.getSelectedIndex();
int lowerLimit = Integer.parseInt(gui.lowerLimit.getText());
int upperLimit = Integer.parseInt(gui.upperLimit.getText());
String mysql = new StringBuffer()
.append(" SELECT Employee_Id,First_Name || ' ' || Last_Name,Salary ")
.append(" FROM Employees ")
.append(" WHERE UPPER(First_Name) like UPPER(?) AND ")
.append(" Department_Id = ? AND ")
.append(" SALARY BETWEEN ? AND ? ")
.append(" ORDER BY First_Name,Last_Name ").toString();
((OracleConnection) conn).setStatementCacheSize(5);
((OracleConnection) conn).setImplicitCachingEnabled(true);
start = System.currentTimeMillis();
for (int i = 1; i <= 10; i++) {
opstmt = (OraclePreparedStatement) conn.prepareStatement(mysql);
opstmt.setString(1, empName);
opstmt.setInt(2, Integer.parseInt(deptId[selectedDept]));
opstmt.setInt(3, lowerLimit);
opstmt.setInt(4, upperLimit);
rset = opstmt.executeQuery();
rset.close();
opstmt.close();
}
end = System.currentTimeMillis();
} catch (SQLException sqlEx) { gui.putStatus(" Error using implicit cache : " + sqlEx.toString());
}
this.dbDisconnect();
return ((int) (end - start));
}
public int searchWithExpCache() {
this.dbConnect();
long start = 0, end = 0;
OraclePreparedStatement opstmt = null;
ResultSet rset = null;
try {
String empName = gui.searchText.getText();
int selectedDept = gui.cmbDepartment.getSelectedIndex();
int lowerLimit = Integer.parseInt(gui.lowerLimit.getText());
int upperLimit = Integer.parseInt(gui.upperLimit.getText());
String mysql = new StringBuffer()
.append(" SELECT Employee_Id,First_Name || ' ' || Last_Name,Salary ")
.append(" FROM Employees ")
.append(" WHERE UPPER(First_Name) like UPPER(?) AND ")
.append(" Department_Id = ? AND ")
.append(" SALARY BETWEEN ? AND ? ")
.append(" ORDER BY First_Name,Last_Name ").toString();
((OracleConnection) conn).setStatementCacheSize(5);
((OracleConnection) conn).setExplicitCachingEnabled(true);
start = System.currentTimeMillis();
for (int i = 1; i <= 10; i++) {
opstmt = (OraclePreparedStatement)
((OracleConnection) conn).getStatementWithKey("searchEmployee");
if (opstmt == null) {
opstmt = (OraclePreparedStatement) conn.prepareStatement(mysql);
}
opstmt.setString(1, empName);
opstmt.setInt(2, Integer.parseInt(deptId[selectedDept]));
opstmt.setInt(3, lowerLimit);
opstmt.setInt(4, upperLimit);
rset = opstmt.executeQuery();
rset.close();
opstmt.closeWithKey("searchEmployee");
}
end = System.currentTimeMillis();
} catch (SQLException sqlEx) { gui.putStatus(" Error using explicit cache : " + sqlEx.toString());
}
this.dbDisconnect();
return ((int) (end - start));
}
public void populateDept() {
this.dbConnect();
if(conn == null) return;
Statement stmt = null;
ResultSet rset = null;
ArrayList deptList = new ArrayList();
try {
stmt = conn.createStatement();
rset = stmt.executeQuery(
" SELECT Department_Id,Department_Name FROM Departments ");
while (rset.next()) {
deptList.add(rset.getString(1));
gui.cmbDepartment.addItem(rset.getString(2));
}
rset.close();
stmt.close();
deptId = (String[])deptList.toArray(new String[deptList.size()]);
} catch (SQLException sqlEx) { gui.putStatus(" Error populating Department details : " +
sqlEx.toString());
}
this.dbDisconnect();
}
public void dbDisconnect() {
try {
if (conn != null) conn.close();
} catch (SQLException sqlEx) {
gui.putStatus(" Error while disconnecting : " + sqlEx.toString());
}
}
public void exitApplication() {
gui = null;
System.exit(0);
}
}
|