package oracle.otnsamples.jdbc.bfile;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.io.IOException;
import java.io.InputStream;
import oracle.jdbc.driver.OracleResultSet;
import oracle.jdbc.pool.OracleDataSource;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Enumeration;
import java.awt.Dimension;
import java.awt.Toolkit;
public class BFILESample {
Connection connection; BFILEFrame gui;
public BFILESample() {
gui = new BFILEFrame(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) {
BFILESample rootFrame = new BFILESample(); rootFrame.dbConnection(); if( rootFrame.connection != null ) {
rootFrame.createTable(); rootFrame.insertData(); }
}
public void dispatchEvent(String eventName){
if( eventName.equals("SELECT") )
selectBFILE();
else if( eventName.equals("EXIT") )
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 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) { 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 createTable() {
try {
Statement stmt = connection.createStatement();
stmt.executeQuery(" DROP TABLE BFILE_TABLE ");
stmt.close();
} catch( SQLException ex ) { gui.putStatus(" Error while Dropping Table " + ex.toString());
}
try {
Statement stmt = connection.createStatement();
stmt.executeQuery(" CREATE TABLE BFILE_TABLE(BFILE_COLUMN BFILE)");
stmt.close();
gui.putStatus("Created table BFILE_TABLE. Inserting Image Data..");
} catch (SQLException ex) { gui.putStatus(" Error while Creating Table " + ex.toString());
}
}
public void insertData() {
String dirPath = gui.getDirectoryPath();
if( !dirPath.equals("Cancel") ) try {
PreparedStatement pstmt = connection.prepareStatement(
"CREATE OR REPLACE DIRECTORY BFILEDIR AS '" + dirPath + "' ");
pstmt.execute();
pstmt.executeQuery(" INSERT INTO BFILE_TABLE VALUES " +
"( BFILENAME('BFILEDIR', 'architect.gif'))");
pstmt.close();
gui.putStatus("Created and populated BFILE_TABLE\n" +
"Press SELECT to select image" );
gui.selectButton.setEnabled(true);
} catch (SQLException ex) { gui.putStatus(" Error while inserting BFILE Data " + ex.toString());
}
else {
gui.putStatus(" Exiting the application..");
exitApplication();
}
}
public void selectBFILE(){
try {
Statement stmt = connection.createStatement();
ResultSet rset = stmt.executeQuery("SELECT * FROM BFILE_TABLE ");
oracle.sql.BFILE bfile = null;
while( rset.next() ) {
bfile = ((OracleResultSet)rset).getBFILE(1); }
gui.putStatus("Retrieved BFILE Locator..");
rset.close(); stmt.close();
bfile.openFile();
InputStream instream = bfile.getBinaryStream();
gui.appendStatus("Opened Binary Stream ...");
gui.displayFile(instream);
} catch( SQLException ex ) { gui.putStatus(" Error while selecting BFILE Locator " + ex.toString());
}
}
public void exitApplication(){
try {
if (connection != null)
connection.close();
} catch( SQLException ex ){
gui.putStatus("Error while closing Connection \n" + ex.toString());
}
System.exit(0);
}
}
|