import sqlj.runtime.*;
import sqlj.runtime.ref.*;
import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
public class sqljBFILESample {
DefaultContext m_dCtx; sqljBFILEFrame m_GUI;
public sqljBFILESample() {
m_GUI = new sqljBFILEFrame(this);
m_GUI.setVisible(true);
}
public static void main(String[] args) {
sqljBFILESample rootFrame = new sqljBFILESample(); rootFrame.dbConnection(); rootFrame.createTable(); rootFrame.insertData(); }
public void dispatchEvent(String p_eventName){
if(p_eventName.equals("SELECT"))
selectBFILE();
else if(p_eventName.equals("EXIT"))
exitApplication();
}
public void dbConnection(){
m_GUI.m_selectButton.setEnabled(false);
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_dCtx = new DefaultContext(
"jdbc:oracle:thin:@"+l_dbConnectString,
ConnectionParams.s_userName,
ConnectionParams.s_password,
false);
DefaultContext.setDefaultContext(m_dCtx);
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 createTable() {
try {
#sql { DROP TABLE BFILE_TABLE };
} catch (SQLException ex) { m_GUI.putStatus(" Error while Dropping Table " + ex.toString());
}
try {
#sql { CREATE TABLE BFILE_TABLE(BFILE_COLUMN BFILE) };
m_GUI.putStatus("Created table BFILE_TABLE. Inserting Image Data.. ");
} catch (SQLException ex) { m_GUI.putStatus(" Error while Creating Table " + ex.toString());
}
}
public void insertData() {
String l_dirPath = m_GUI.getDirectoryPath();
if (!l_dirPath.equals("Cancel"))
try {
String l_comstmt = "Create Or Replace Directory BFILEDIR AS " + "'" +
l_dirPath + "'";
#sql { BEGIN EXECUTE IMMEDIATE :(l_comstmt); END; };
#sql { INSERT INTO BFILE_TABLE VALUES (BFILENAME('BFILEDIR','architect.gif')) };
#sql { COMMIT };
m_GUI.putStatus("Created and populated BFILE_TABLE\n" +
"Press SELECT to select image." );
m_GUI.m_selectButton.setEnabled(true);
}catch (SQLException ex) { m_GUI.putStatus(" Error while inserting BFILE Data " + ex.toString());
}else
exitApplication();
}
public void selectBFILE() {
try {
oracle.sql.BFILE l_bfile;
#sql { Select BFILE_COLUMN AS bfilecolumn INTO :l_bfile
From BFILE_TABLE };
m_GUI.putStatus("Selected BFILE Locator..");
l_bfile.openFile();
InputStream l_instream = l_bfile.getBinaryStream();
m_GUI.appendStatus("Opened Binary Stream ...");
m_GUI.displayFile(l_instream); } catch (SQLException ex) { m_GUI.putStatus(" Error while selecting BFILE Locator ..." + ex.toString());
}
}
public void exitApplication(){
try{
if (m_dCtx != null)
m_dCtx.close(); }catch (SQLException ex){ m_GUI.putStatus("Error while closing connection.\n" + ex.toString());
}
System.exit(0);
}
}
|