import sqlj.runtime.*;
import sqlj.runtime.ref.*;
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
#sql iterator CountryIter ( String Name, String Geography, String Currency );
public class sqljSTCollectionSample {
DefaultContext m_ctx; sqljSTCollectionFrame m_GUI;
static cityRefs l_cityList = null;
public sqljSTCollectionSample() {
m_GUI = new sqljSTCollectionFrame(this); m_GUI.setVisible(true);
}
public static void main(String[] args) {
sqljSTCollectionSample collSamp = new sqljSTCollectionSample(); collSamp.dbConnection(); if (collSamp.m_ctx != null) {
collSamp.populateCountries(); }
}
public void dispatchEvent (String p_eventName) {
if (p_eventName.equals("SELECT CITIES")) {
int l_selectedRow = m_GUI.m_table.getSelectedRow();
String l_name = (String) m_GUI.m_tableModel.getValueAt(l_selectedRow,0);
displayCitiesData(l_name);
} else if (p_eventName.equals("EXIT"))
exitApplication();
}
public void dbConnection(){
try{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
m_GUI.putStatus("Trying to connect to the Database");
String l_dbConnectString =
"(DESCRIPTION=(ADDRESS=(HOST="+ConnectionParams.s_hostName+")"+
"(PROTOCOL=tcp)(PORT="+ConnectionParams.s_portNumber+"))"+
"(CONNECT_DATA=(SID="+ConnectionParams.s_databaseSID+")))";
m_ctx = new DefaultContext(
"jdbc:oracle:thin:@"+l_dbConnectString,
ConnectionParams.s_userName,
ConnectionParams.s_password,
false);
DefaultContext.setDefaultContext(m_ctx);
m_GUI.putStatus("Connected to "+ConnectionParams.s_databaseSID+ " as "+
ConnectionParams.s_userName);
} catch(Exception ex){ m_GUI.putStatus(
"Error in Connecting to the Database "+'\n'+ex.toString());
}
}
public void populateCountries() {
m_GUI.putStatus("Populating Countries ...");
m_GUI.m_tableModel.clearTable();
try {
CountryIter l_countryIter = null;
#sql l_countryIter = { Select Name, Geography, Currency
From Obj_Table_Countries ORDER BY NAME};*/
while (l_countryIter.next()) {
String l_name = l_countryIter.Name();
String l_geography = l_countryIter.Geography();
String l_currency = l_countryIter.Currency();
m_GUI.addToCountryJTable(l_name, l_geography, l_currency);
}
l_countryIter.close(); m_GUI.putStatus("Connected to database and populated all countries");
m_GUI.appendStatus("Please choose a country");
} catch (SQLException ex) { m_GUI.putStatus(" Error in Querying countries table: ");
m_GUI.appendStatus(" "+ex.toString());
}
}
public void displayCitiesData(String p_country){
try {
m_GUI.putStatus("Retrieving cities for the selected country ...");
m_GUI.m_tableModel1.clearTable();
#sql {Select CITY_LIST INTO :l_cityList FROM OBJ_TABLE_COUNTRIES WHERE NAME = :p_country};
objTypeCityRef[] l_cityRefs = l_cityList.getArray();
for (int i = 0;i<l_cityRefs.length;i= i+1) {
objTypeCity l_cityObject = (objTypeCity) l_cityRefs[i].getValue();
String l_name = l_cityObject.getName().toString();
String l_state = l_cityObject.getProvince().toString();
String l_cityCode = l_cityObject.getCityCode().toString();
m_GUI.addToCityJTable(l_name, l_state, l_cityCode);
}
m_GUI.putStatus("Retrieving cities for the selected country ...Done");
}catch (SQLException ex) { m_GUI.putStatus(" Error in Querying: ");
m_GUI.appendStatus(" "+ex.toString());
}
}
public void exitApplication() {
try {
m_GUI.putStatus("Closing the connection....please wait.....");
if(m_ctx != null)
m_ctx.close(); } catch(SQLException ex){ m_GUI.putStatus(ex.toString());
}
System.exit(0); }
}
|