package oracle.otnsamples.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.Properties;
import java.io.IOException;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.OracleResultSet;
import oracle.sql.BINARY_DOUBLE;
import oracle.sql.BINARY_FLOAT;
import oracle.jdbc.pool.OracleDataSource;
public class IeeeApplication {
private IeeeFrame gui;
public IeeeApplication() {
gui = new IeeeFrame(this);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = gui.getSize();
if( frameSize.height > screenSize.height ) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
gui.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
gui.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
IeeeApplication sample = new IeeeApplication();
sample.checkTables();
sample.selectAllRecords();
} catch(Exception e) {
System.out.println("Some error in the Application main method: "+
e.toString());
}
}
public void dispatchEvent (String eventName) {
String planet = gui.planet.getText();
String distance = gui.distance.getText();
if( eventName.equals("INSERT") )
insertRecord( planet, distance );
else if( eventName.equals("EXIT") )
exitApplication();
}
private void insertRecord( String planet,String dist) {
Connection conn = null;
OraclePreparedStatement opstmt = null;
try {
conn = this.dbConnection();
String sqlQuery = "INSERT INTO planet_au_distance "+
" VALUES(?,?,?,?,?,149600000)";
opstmt = (OraclePreparedStatement)conn.prepareStatement(sqlQuery);
double distDble = 0;
int flag = 1;
if (("".equals(planet)) || ("".equals(dist))) {
gui.putStatus("Enter Planet's name and its distance from the Sun "+
"in kilometers");
}else if (dist.length()>19) {
gui.putStatus("Enter a valid number for 'Distance' with "+
"length < 20 and without spaces.");
} else if (!("".equals(dist))) {
try {
distDble = Double.parseDouble(dist);
flag = 0;
} catch (NumberFormatException numEx) {
gui.putStatus("Enter a valid number for 'Distance' with "+
"length < 20 and without spaces.");
flag =1;
}
}
if (flag==0) {
opstmt.setString(1,planet);
opstmt.setDouble(2,distDble);
Double astrUnit = new Double("149600000");
double distance = Double.parseDouble(dist);
double distVal = distance/149600000;
Double dbl1 = new Double(distVal);
BINARY_DOUBLE biD = new BINARY_DOUBLE(dbl1.doubleValue());
opstmt.setBinaryDouble(3,biD);
BINARY_FLOAT biF = new BINARY_FLOAT(dbl1.floatValue());
opstmt.setBinaryFloat(4,biF);
int biN = (int)distVal;
opstmt.setInt(5,biN);
int val =opstmt.executeUpdate();
gui.addToJTable( planet, Double.parseDouble(dist), dbl1.doubleValue(),
dbl1.floatValue(),biN );
gui.putStatus("The Record is inserted");
}
} catch (SQLException ex) { gui.putStatus("Error in inserting record to the database "
+ex.toString());
System.out.println(ex.toString());
} catch (Exception ex) { gui.putStatus("Exception in the method while inserting: "
+ex.toString());
System.out.println(ex.toString());
} finally {
try {
if (opstmt != null ) opstmt.close();
if (conn != null ) conn.close();
} catch(SQLException ex) {
gui.putStatus(" SQLException : "+ex.toString());
}
}
}
private void selectAllRecords() {
Connection conn = null;
OraclePreparedStatement opstmt = null;
String sqlQuery = "SELECT planet, dist_kms, au_dist_binaryfloat, "+
" au_dist_binarydouble, au_dist_num "+
" FROM planet_au_distance ORDER BY dist_kms ";
try {
conn = this.dbConnection();
opstmt = (OraclePreparedStatement)conn.prepareStatement(sqlQuery);
OracleResultSet orset =(OracleResultSet)opstmt.executeQuery();
long distKms = 0;
int distNum = 0;
double dbleVal = 0;
float fltVal = 0;
String name = null;
String val = null;
while (orset.next()) {
name = orset.getString(1);
distKms = orset.getLong(2);
val = String.valueOf(orset.getDouble(3));
dbleVal = orset.getDouble(3);
fltVal = orset.getFloat(4);
distNum = orset.getInt(5);
gui.addToJTable( name,distKms, dbleVal, fltVal,distNum );
}
opstmt.close();
gui.putStatus(gui.tmodel.getRowCount() +" Records selected."+
"\n Enter values to insert a new record.");
} catch( SQLException ex ) { gui.putStatus("Error in selecting from the database " +ex.toString());
} finally {
try {
if (opstmt != null ) opstmt.close();
if (conn != null ) conn.close();
} catch(SQLException ex) {
gui.putStatus(" SQLException : "+ex.toString());
}
}
}
private 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;
}
private Connection dbConnection() {
Connection connection = null;
try {
Properties prop = 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"));
connection=ods.getConnection();
gui.putStatus("Connected to " + prop.get("SID") +
" Database as " + prop.get("UserName"));
} catch(SQLException ex) { gui.putStatus("Error in Connecting to the Database "+
'\n'+ex.toString());
System.out.println(ex.toString());
} catch(IOException ex) { gui.putStatus("Error in reading the properties file "+
'\n'+ex.toString());
System.out.println(ex.toString());
}
return connection;
}
private void checkTables() {
Statement stmt = null;
ResultSet rset = null;
Connection conn = null;
try {
conn = this.dbConnection();
stmt = conn.createStatement();
rset = stmt.executeQuery(" SELECT table_name FROM user_tables "+
" WHERE table_name = 'PLANET_AU_DISTANCE' ");
if (!rset.next()) {
PopulateTable pop = new PopulateTable(conn, gui);
pop.createDbObjects();
gui.putStatus( "planet_au_distance Table created.");
}
} catch (SQLException sqlEx) {
gui.putStatus(" Could not create table planet_au_distance : "
+sqlEx.toString());
} finally {
try {
if( rset != null ) rset.close();
if( stmt != null ) stmt.close();
if (conn != null ) conn.close();
} catch(SQLException ex) {
gui.putStatus(" SQLException : "+ex.toString());
}
}
}
private void exitApplication() {
try {
gui.putStatus("Closing the application....please wait.....");
} catch( Exception ex ) { gui.putStatus(ex.toString());
}
System.exit(0); }
}
|