package oracle.otnsamples.oracle9ijdbc.multiplecollection;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OracleResultSet;
import java.math.BigDecimal;
import java.util.Properties;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.io.IOException;
public class MultipleCollectionSample {
Connection m_conn = null;
GeographicRegionFrame m_GUI = null;
public MultipleCollectionSample() {
m_GUI = new GeographicRegionFrame(this);
}
public static void main(String args[]) {
MultipleCollectionSample MCS = new MultipleCollectionSample();
MCS.dbConnect();
if(MCS.m_conn != null)
MCS.displayRegion();
}
public void dispatchEvent(String p_event) {
if(p_event.equals("DISPLAY COUNTRIES"))
this.displayCountries();
else if(p_event.equals("DISPLAY CITIES"))
this.displayCities();
else if(p_event.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 {
m_GUI.putStatus("Trying to connect to the Database");
Properties prop = MultipleCollectionSample.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"));
m_conn = ods.getConnection();
m_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());
}
}
public void displayRegion() {
Statement stmt = null;
ResultSet rset = null;
String name = null;
String region_description = null;
try {
stmt = m_conn.createStatement();
rset = stmt.executeQuery(" SELECT name, region_description FROM geographic_region ");
while(rset.next()) {
name = rset.getString(1);
region_description = rset.getString(2);
m_GUI.addToJTable(name,region_description);
}
} catch(Exception ex) { m_GUI.putStatus(" Error while querying geographic regions :"+ex.toString());
} finally {
try {
if(stmt != null) stmt.close();
if(rset != null) rset.close();
} catch(Exception ex) { }
}
}
public void displayCountries(){
PreparedStatement pstmt = null;
OracleResultSet rset = null;
try {
String selectedRegion = m_GUI.getSelectedRegion();
pstmt = m_conn.prepareStatement(
"SELECT country_list FROM geographic_region WHERE name = ? ");
pstmt.setString(1,selectedRegion);
rset = (OracleResultSet)pstmt.executeQuery(); m_GUI.m_countriesList.m_tableModel.clearTable();
while ( rset.next() ) {
CountriesNt countryList = (CountriesNt) rset.getORAData(1,CountriesNt.getORADataFactory());
CountriesType[] countriesRef = countryList.getArray();
for (int i = 0;i<countriesRef.length;i= i+1) {
String name = countriesRef[i].getName();
String capital = countriesRef[i].getCapital();
m_GUI.m_countriesList.addToJTable(name, capital);
}
}
}catch (SQLException ex) {
m_GUI.putStatus(" Error in Querying Countries: "+ex.toString());
}
finally {
try {
if( pstmt != null) pstmt.close();
if(rset != null) rset.close();
} catch(Exception ex) { }
}
}
public void displayCities(){
PreparedStatement pstmt = null;
OracleResultSet rset = null;
try {
String selectedCountry = m_GUI.m_countriesList.getSelectedCountry();
pstmt = m_conn.prepareStatement(
" SELECT c.city_list FROM geographic_region g, TABLE(g.country_list) c "+
" WHERE c.name = ? ");
pstmt.setString(1,selectedCountry); rset = (OracleResultSet)pstmt.executeQuery(); m_GUI.m_countriesList.m_citiesList.m_tableModel.clearTable();
while ( rset.next() ) {
CitiesNt cityList = (CitiesNt) rset.getORAData(1,CitiesNt.getORADataFactory());
CityType[] citiesRef = cityList.getArray();
for (int i = 0;i<citiesRef.length;i= i+1) {
String name = citiesRef[i].getName();
BigDecimal population = citiesRef[i].getPopulation();
BigDecimal altitude = citiesRef[i].getAltitude();
m_GUI.m_countriesList.m_citiesList.addToJTable(name, population.intValue(),altitude.intValue());
}
}
pstmt.close();
}catch (SQLException ex) {
m_GUI.putStatus(" Error in Querying cities : "+ex.toString());
}
finally {
try {
if( pstmt != null) pstmt.close();
if(rset != null) rset.close();
} catch(Exception ex) { }
}
}
void exitApplication(){
if (m_conn != null) {
try {
m_conn.close();
m_conn=null;
}
catch (Exception ex){ m_GUI.putStatus(" Error while Closing the connection: "+ex.toString());
}
}
System.exit(0);
}
}
|