package oracle.otnsamples.jdbc;
import java.sql.Connection;
import java.sql.Types;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
import java.util.Properties;
import java.util.Vector;
import java.util.ResourceBundle;
import java.util.Enumeration;
import java.io.IOException;
public class PlsqlTablesSample {
Connection conn = null;
PlsqlTablesFrame GUI;
int[] cityIdArray = null;
String[] cityNameArray = {"Delhi", "New York", "Tokyo", "Paris","Bangalore"};
int[] populationArray = {200000, 400000, 300000, 100000,500000};
String[] languageArray = {"Hindi", "English", "Japanese", "French","Kannada"};
int maxTablLen = 100;
int eleMaxLen = 100;
public PlsqlTablesSample() {
GUI = new PlsqlTablesFrame(this);
}
public static void main(String arg[]){
PlsqlTablesSample plsqlTablesSample=new PlsqlTablesSample();
plsqlTablesSample.dbConnect();
if(plsqlTablesSample.conn!=null){
plsqlTablesSample.populateCityInfo();
plsqlTablesSample.displayCities();
}
}
public void dispatchEvent (String eventName) {
if (eventName.equals("ADD CITY"))
this.addCityDetails();
else if (eventName.equals("DELETE CITY"))
this.deleteCityDetails();
else if (eventName.equals("EXIT"))
this.exitApplication();
}
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 = PlsqlTablesSample.loadParams("Connection");
OracleDataSource ods = new OracleDataSource();
this.configureDataSource(ods, prop);
conn = ods.getConnection();
conn.setAutoCommit(false);
} catch (SQLException sqlEx){ GUI.putStatus("SQL Errors = " + sqlEx.toString());
} catch(Exception excep) { GUI.putStatus(" Couldn't configure Initial Context "
+ excep.toString());
}
}
public void populateCityInfo() {
OracleCallableStatement ocstmt=null;
try{
int currentLen = cityNameArray.length;
ocstmt =(OracleCallableStatement)conn.prepareCall(
" begin"+
" cityinfo_pkg.populate_DB(?,?,?);"+
" end;");
ocstmt.setPlsqlIndexTable (1, cityNameArray, maxTablLen,
currentLen, OracleTypes.VARCHAR, eleMaxLen);
ocstmt.setPlsqlIndexTable (2, populationArray, maxTablLen,
currentLen, OracleTypes.NUMBER, 0);
ocstmt.setPlsqlIndexTable (3, languageArray, maxTablLen,
currentLen, OracleTypes.VARCHAR, eleMaxLen);
ocstmt.execute();
conn.commit();
}catch(Exception ex){
GUI.putStatus(" Couldn't populate data in the Database "+ex.toString());
ex.printStackTrace();
}finally {
try {
if(ocstmt != null) ocstmt.close();
} catch(Exception e) {}
}
}
public void displayCities() {
OracleCallableStatement ocstmt=null;
try {
ocstmt =(OracleCallableStatement)conn.prepareCall(
" begin"+
" cityinfo_pkg.populate_Index_By_tbl();"+
" cityinfo_pkg.retrieve_All_Cities(?,?,?,?);"+
" end;");
ocstmt.registerIndexTableOutParameter(1,
maxTablLen,
OracleTypes.INTEGER,
0);
ocstmt.registerIndexTableOutParameter(2,
maxTablLen,
OracleTypes.VARCHAR,
eleMaxLen);
ocstmt.registerIndexTableOutParameter(3,
maxTablLen,
OracleTypes.INTEGER,
eleMaxLen);
ocstmt.registerIndexTableOutParameter(4,
maxTablLen,
OracleTypes.VARCHAR,
eleMaxLen);
ocstmt.execute();
cityIdArray = (int[]) ocstmt.getPlsqlIndexTable(1,
java.lang.Integer.TYPE);
cityNameArray = (String[]) ocstmt.getPlsqlIndexTable(2);
populationArray = (int[]) ocstmt.getPlsqlIndexTable(3,
java.lang.Integer.TYPE);
languageArray = (String[]) ocstmt.getPlsqlIndexTable(4);
int sizeofTbl=cityIdArray.length;
for(int i=0;i<sizeofTbl;i++){
GUI.addToJTable(cityIdArray[i], cityNameArray[i], populationArray[i],
languageArray[i]);
}
GUI.enableFrame();
GUI.putStatus(" Click ADD / DELETE to add new or delete an "+
"existing city ");
} catch(Exception ex){ GUI.putStatus("Error while retrieving City details : "+ex.toString());
} finally {
try {
if(ocstmt != null) ocstmt.close();
} catch(Exception e) {}
}
}
public void deleteCityDetails(){
OracleCallableStatement ocstmt=null;
Vector cityInfoVector = GUI.getSelectedCity();
try {
ocstmt =(OracleCallableStatement)conn.prepareCall(
" begin"+
" cityinfo_pkg.delete_city(?);"+
" end;");
ocstmt.setInt(1,Integer.parseInt((String)cityInfoVector.elementAt(0)));
ocstmt.execute();
conn.commit();
int selectedRow = GUI.cityTable.getSelectedRow();
GUI.deleteRow(selectedRow);
} catch(Exception ex){ GUI.putStatus("Error while deleting City details : "+ex.toString());
} finally {
try {
if(ocstmt != null) ocstmt.close();
} catch(Exception e) {}
}
}
public void addCityDetails(){
OracleCallableStatement ocstmt=null;
String cityName = GUI.getCityName();
int population = GUI.getPopulation();
String language = GUI.getLanguage();
try {
ocstmt =(OracleCallableStatement)conn.prepareCall(
" begin"+
" cityinfo_pkg.add_City(?,?,?,?);"+
" end;");
ocstmt.setString(1,cityName);
ocstmt.setInt(2,population);
ocstmt.setString(3,language);
ocstmt.registerOutParameter(4,Types.INTEGER);
ocstmt.executeUpdate();
int cityId=ocstmt.getInt(4);
GUI.addToJTable(cityId, cityName, population,language);
conn.commit();
} catch(Exception ex){ GUI.putStatus("Error while adding City details : "+ex.toString());
} finally {
try {
if(ocstmt != null) ocstmt.close();
} catch(Exception e) {}
}
}
public void clearTblData(){
OracleCallableStatement ocstmt=null;
try{
ocstmt =(OracleCallableStatement)conn.prepareCall(
" begin"+
" cityinfo_pkg.clear_DB();"+
" end;");
ocstmt.execute();
conn.commit();
}catch(Exception ex){
GUI.putStatus(" Couldn't Delete data in the Database "+ex.toString());
}finally {
try {
if(ocstmt != null) ocstmt.close();
} catch(Exception e) {}
}
}
private void configureDataSource(OracleDataSource ods, Properties prop) {
ods.setServerName(prop.getProperty("HostName"));
ods.setDatabaseName(prop.getProperty("SID"));
ods.setPortNumber( new Integer( prop.getProperty("Port") ).intValue());
ods.setDriverType ("thin");
ods.setUser(prop.getProperty("UserName"));
ods.setPassword(prop.getProperty("Password"));
}
void exitApplication(){
if (conn != null) {
try {
clearTblData();
conn.close();
conn = null;
}
catch (Exception ex){ GUI.putStatus(" Error while Closing the connection: "+ex.toString());
}
}
System.exit(0);
}
}
|