package oracle.otnsamples.jdbc.lobtype;
import java.sql.Connection; import java.sql.Clob; import java.sql.ResultSet; import java.sql.Blob; import java.sql.Statement; import java.sql.SQLException; import java.sql.PreparedStatement; import oracle.jdbc.pool.OracleDataSource;
import java.util.Properties; import java.util.ResourceBundle; import java.util.Enumeration;
import java.io.IOException; import java.io.OutputStream; import java.io.File; import java.io.Reader; import java.io.FileOutputStream; import java.io.InputStream; import java.io.Writer; import java.io.FileReader; import java.io.FileInputStream;
public class LOBSample {
Connection connection;
LOBFrame gui;
public LOBSample() { gui = new LOBFrame(this); gui.setVisible(true); }
public static void main(String[] args) { LOBSample lobs = new LOBSample(); lobs.dbConnection(); if( lobs.connection != null ) { lobs.checkTables(); lobs.populateAirports(); } }
public void dispatchEvent( String eventName ) { if( eventName.equals("Load Sample Files")) loadSamples(gui.getSelectedCode()); else if( eventName.equals("Add Suggestions") ) { String suggestions = gui.getSuggestionText(); if( !suggestions.equals("CANCEL")) { addSuggestions(gui.getSelectedCode(), suggestions); gui.sugArea.append(new String(suggestions)); } } else if( eventName.equals("Airport Selected in Table") ) airportSelected(gui.getSelectedCode()); else if( eventName.equals("EXIT") ) exitApplication(); }
private 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; }
private void dbConnection() { try { gui.putStatus("Trying to connect to the Database"); Properties prop = this.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(); connection.setAutoCommit(false); 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()); } catch(IOException ex) { gui.putStatus( "Error in reading the properties file "+'\n'+ex.toString()); } }
private void populateAirports() { Statement stmt = null; try { gui.appendStatus("\nPopulating Airports. Please wait...");
stmt = connection.createStatement();
ResultSet resultSet = stmt.executeQuery( "SELECT airport_code, description, name"+ " FROM otn_airports");
while( resultSet.next() ) { String code = resultSet.getString(1); String desc = resultSet.getString(2); String city = resultSet.getString(3);
gui.addToJTable(code, desc, city); } gui.putStatus("Connected to database and retrieved all airport rows"); gui.appendStatus("\nPlease Choose an airport "); } catch( SQLException ex ) { gui.putStatus("Error Querying OTN_AIRPORTS table: \n" + ex.toString()); } finally { try { stmt.close(); } catch(SQLException ex) { } } }
private void loadSamples( String airportCode ) { PreparedStatement pstmt = null; Statement stmt = null; try { gui.putStatus("Creating row for airport in OTN_AIRPORT_LOB_DETAILS.. ");
pstmt = connection.prepareStatement( "INSERT INTO OTN_AIRPORT_LOB_DETAILS( airport_code, airport_map,"+ "airport_sug_book) VALUES(? , EMPTY_BLOB() , EMPTY_CLOB())"); pstmt.setString(1, airportCode); pstmt.execute();
gui.appendStatus("Created.\n Loading <map.gif> into Blob column for airport...");
stmt = connection.createStatement(); ResultSet lobDetails = stmt.executeQuery( "SELECT airport_map, airport_sug_book FROM OTN_AIRPORT_LOB_DETAILS "+ "WHERE airport_code = '" + airportCode + "' FOR UPDATE");
Properties prop = this.loadParams("Misc"); String mapFileName = (String)prop.get("map"); String sugBookFileName = (String)prop.get("suggestions");
if( lobDetails.next() ) { Blob mapBlob = lobDetails.getBlob(1); OutputStream blobOutputStream = ((oracle.sql.BLOB)mapBlob).getBinaryOutputStream();
File mapFile = new File(mapFileName); InputStream sampleFileStream = new FileInputStream(mapFile);
byte[] buffer = new byte[10* 1024];
int nread = 0; while( (nread= sampleFileStream.read(buffer)) != -1 ) blobOutputStream.write(buffer, 0, nread);
sampleFileStream.close(); blobOutputStream.close();
gui.appendStatus("Done\nLoading <suggestionbook.txt> into Clob column ..");
Clob sugBookClob = lobDetails.getClob(2); Writer clobWriter = ((oracle.sql.CLOB)sugBookClob).getCharacterOutputStream();
File sugbookFile = new File(sugBookFileName); FileReader sugFileReader = new FileReader(sugbookFile);
char[] cbuffer = new char[10* 1024];
nread = 0; while( (nread= sugFileReader.read(cbuffer)) != -1 ) clobWriter.write( cbuffer, 0, nread);
sugFileReader.close(); clobWriter.close();
gui.putStatus("Done Loading sample files");
gui.appendStatus("\nRetrieving and displaying sample files.."); drawBlob(mapBlob, airportCode); writeClob(sugBookClob, airportCode); gui.putStatus("Done loading and displaying LOB data"); } } catch( Exception ex ) { gui.putStatus("Error loading sample files for the selected airport"); gui.appendStatus("\n" + ex.toString()); } finally { try { pstmt.close(); stmt.close(); } catch(SQLException ex) { } } }
private void addSuggestions(String airportCode, String suggestions) { Statement stmt = null; try { gui.putStatus(" Appending entered suggestions to Clob column. Please wait..."); stmt = connection.createStatement();
ResultSet lobDetails = stmt.executeQuery( "SELECT airport_sug_book "+ "FROM OTN_AIRPORT_LOB_DETAILS "+ "WHERE airport_code ='" + airportCode + "' FOR UPDATE");
if( lobDetails.next() ) { Clob clob = lobDetails.getClob(1);
((oracle.sql.CLOB)clob).putString(((oracle.sql.CLOB)clob).length() + 1, suggestions); gui.appendStatus(" Done"); } } catch( SQLException ex ) { gui.putStatus("Error appending suggestions to the Clob column"); gui.appendStatus("\n" + ex.toString()); } finally { try { stmt.close(); } catch(SQLException ex) { } } }
private void airportSelected(String airportCode) { Statement stmt = null; try { gui.putStatus("Retrieving LOB details for selected airport.."); stmt = connection.createStatement();
ResultSet lobDetails = stmt.executeQuery( "SELECT airport_map, airport_sug_book FROM OTN_AIRPORT_LOB_DETAILS "+ "WHERE airport_code = '" + airportCode + "'");
if( lobDetails.next() ) {
drawBlob(lobDetails.getBlob(1), airportCode); writeClob(lobDetails.getClob(2), airportCode); gui.putStatus("Done retrieving and displaying LOB details"); } else { gui.loadButton.setEnabled(true); gui.putStatus(" No airport map and suggestion book exist for selected airport"); gui.appendStatus("\n Press <Load Sample Files> to load LOB details"); } } catch( Exception ex ) { gui.putStatus("Error retrieving LOB Details for the selected airport"); gui.appendStatus(ex.toString()); } finally { try { stmt.close(); } catch(SQLException ex) { } } }
private void drawBlob(Blob blob, String airPCode) { try { InputStream blobStream = blob.getBinaryStream();
String userHome = System.getProperty("user.home");
String fileName = userHome+File.separator+airPCode+".gif";
FileOutputStream fileOutStream = new FileOutputStream(fileName);
byte[] buffer = new byte[10]; int nbytes = 0; while( (nbytes = blobStream.read(buffer)) != -1 ) fileOutStream.write(buffer, 0, nbytes);
fileOutStream.flush(); fileOutStream.close(); blobStream.close();
gui.drawMap(fileName); } catch( Exception ex ) { gui.putStatus(" Error in retrieving and drawing map for selected airport"); gui.appendStatus("\n" + ex.toString()); } }
void writeClob(Clob clob, String airPCode) { try { Reader clobStream = clob.getCharacterStream();
StringBuffer suggestions = new StringBuffer();
int nchars = 0; char[] buffer = new char[10];
while( (nchars = clobStream.read(buffer)) != -1 ) suggestions.append(buffer, 0, nchars);
clobStream.close(); gui.sugArea.append(new String(suggestions)); } catch( Exception ex ) { gui.putStatus("Error in getting and drawing Clob for the airport, "+ airPCode +":"); gui.appendStatus(ex.toString()); } }
public void exitApplication() { try { gui.putStatus("Closing the connection....please wait....."); if( connection != null ) connection.close(); } catch( SQLException ex ) { gui.putStatus(ex.toString()); } System.exit(0); }
private void checkTables() { Statement stmt = null; ResultSet rset = null; PopulateTable popTable = null; try { stmt = connection.createStatement(); rset = stmt.executeQuery(" SELECT Table_Name FROM User_Tables "+ " WHERE Table_Name = 'OTN_AIRPORTS' ");
if (!rset.next()) { if (popTable == null) popTable = new PopulateTable(connection,gui); popTable.createSchemaTable(); gui.putStatus("OTN_AIRPORTS Table created."); } rset = stmt.executeQuery(" SELECT Table_Name FROM User_Tables "+ " WHERE Table_Name = 'OTN_AIRPORT_LOB_DETAILS' ");
if (!rset.next()) { if (popTable == null) popTable = new PopulateTable(connection,gui); popTable.createLOBTable(); gui.putStatus("OTN_AIRPORT_LOB_DETAILS Table created."); } } catch (SQLException sqlEx) { gui.putStatus("Could not create required tables : "+sqlEx.toString()); } finally { try { if( rset != null ) rset.close( ); if( stmt != null ) stmt.close( ); } catch(SQLException ex) { } } } }
|