package oracle.otnsamples.oracle9ijdbc.ncharsupport4unicode;
import oracle.jdbc.OraclePreparedStatement;
import oracle.jdbc.pool.OracleDataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Vector;
public class NCHARsupport4UnicodeSample {
private static String[] langId = null;
Connection connection = null;
NCHARsupport4UnicodeFrame gui = null;
public NCHARsupport4UnicodeSample() {
gui = new NCHARsupport4UnicodeFrame(this);
}
public static void main(String[] args) {
NCHARsupport4UnicodeSample sample = new NCHARsupport4UnicodeSample();
sample.dbConnect();
if (sample.connection != null) {
sample.checkTables();
sample.displayProductsInfo();
}
}
public void dispatchEvent(String event) {
if (event.equals("PRODUCT CHANGED")) {
getProductDetails();
} else if (event.equals("INSERT")) {
insertProductDetails();
} else if (event.equals("UPDATE")) {
updateProductDetails();
} else if (event.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 dbConnect() {
try {
gui.putStatus("Trying to connect to the Database");
Properties prop = NCHARsupport4UnicodeSample.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());
}catch (IOException ex) { gui.putStatus("Error in reading the properties file " + '\n' +
ex.toString());
}
}
public void displayProductsInfo() {
displayProducts();
displayLang();
gui.enableSelect();
}
public void displayProducts() {
Statement stmt = null;
ResultSet rset = null;
String productId = null;
try {
stmt = connection.createStatement();
rset = stmt.executeQuery(" SELECT DISTINCT(Product_Id) " +
" FROM Product_Descriptions ");
while (rset.next()) {
productId = rset.getString(1);
gui.addToProdCombo(productId);
}
} catch (Exception ex) { gui.putStatus(" Error while querying Product Details :" +
ex.toString());
} finally {
try {
if (rset != null) {
rset.close();
}
if (stmt != null) {
stmt.close();
}
} catch (Exception ex) {
gui.putStatus(" Error : " + ex.toString());
}
}
}
public void displayLang() {
Statement stmt = null;
ResultSet rset = null;
Vector langId = new Vector();
String langName = null;
try {
stmt = connection.createStatement();
rset = stmt.executeQuery(" SELECT LangId,LangName " +
" FROM Languages ");
while (rset.next()) {
langId.addElement(rset.getString(1));
langName = rset.getString(2);
gui.addToLangCombo(langName);
}
this.langId = new String[langId.size()];
langId.copyInto(this.langId);
langId = null;
} catch (Exception ex) { gui.putStatus(" Error while querying languages :" + ex.toString());
} finally {
try {
if (rset != null) {
rset.close();
}
if (stmt != null) {
stmt.close();
}
} catch (Exception ex) {
gui.putStatus(" Error : " + ex.toString());
}
}
}
public void getProductDetails() {
PreparedStatement pstmt = null;
ResultSet rset = null;
int productId = gui.getSelectedProductId();
String langId = this.langId[gui.getSelectedLanguage()];
String productName = null;
String productDesc = null;
try {
pstmt = connection.prepareStatement(" SELECT Translated_Name, Translated_Description " +
" FROM Product_Descriptions " +
" WHERE Product_Id=? AND Language_Id=? ");
pstmt.setInt(1, productId);
pstmt.setString(2, langId);
rset = pstmt.executeQuery();
if (rset.next()) {
productName = rset.getString(1);
productDesc = rset.getString(2);
}
gui.displayProduct(productName, productDesc);
} catch (Exception ex) { gui.putStatus(" Couldn't retrieve Product Desc:" + ex.toString());
} finally {
try {
if (rset != null) {
rset.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (Exception ex) {
gui.putStatus(" Error : " + ex.toString());
}
}
}
public void updateProductDetails() {
PreparedStatement pstmt = null;
int productId = gui.getSelectedProductId();
String langId = this.langId[gui.getSelectedLanguage()];
String productName = gui.getProductName();
String productDesc = gui.getProductDesc();
try {
pstmt = connection.prepareStatement(" UPDATE Product_Descriptions " +
" SET Translated_Name=?,Translated_Description=? " +
" WHERE Product_Id=? AND Language_Id=? ");
((OraclePreparedStatement) pstmt).setFormOfUse(1,
OraclePreparedStatement.FORM_NCHAR);
pstmt.setString(1, productName);
((OraclePreparedStatement) pstmt).setFormOfUse(2,
OraclePreparedStatement.FORM_NCHAR);
pstmt.setString(2, productDesc);
pstmt.setInt(3, productId);
pstmt.setString(4, langId);
pstmt.executeUpdate();
gui.putStatus(" Product Details Updated ");
} catch (Exception ex) { gui.putStatus(" Couldn't UPDATE Product Desc:" + ex.toString());
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception ex) {
gui.putStatus(" Error : " + ex.toString());
}
}
}
public void insertProductDetails() {
PreparedStatement pstmt = null;
int productId = gui.getSelectedProductId();
String langId = this.langId[gui.getSelectedLanguage()];
String productName = gui.getProductName();
String productDesc = gui.getProductDesc();
try {
pstmt = connection.prepareStatement(" INSERT INTO Product_Descriptions " +
" VALUES(?,?,?,?) ");
pstmt.setInt(1, productId);
pstmt.setString(2, langId);
((OraclePreparedStatement) pstmt).setFormOfUse(3,
OraclePreparedStatement.FORM_NCHAR);
pstmt.setString(3, productName);
((OraclePreparedStatement) pstmt).setFormOfUse(4,
OraclePreparedStatement.FORM_NCHAR);
pstmt.setString(4, productDesc);
int inserted = pstmt.executeUpdate();
gui.putStatus(inserted + " Product Detail Inserted ");
} catch (Exception ex) { gui.putStatus(" Couldn't Insert Product Details:" + ex.toString());
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception ex) {
gui.putStatus(" Error : " + ex.toString());
}
}
}
private void checkTables() {
Statement stmt = null;
ResultSet rset = null;
try {
stmt = connection.createStatement();
rset = stmt.executeQuery(" SELECT Table_Name FROM User_Tables " +
" WHERE Table_Name = 'Languages' ");
if (!rset.next()) {
PopulateTable popTable = new PopulateTable(connection, gui);
popTable.createSchemaTable();
gui.putStatus("Languages Table created.");
}
} catch (SQLException sqlEx) {
gui.putStatus("Could not create table Languages : " +
sqlEx.toString());
} finally {
try {
if (rset != null) {
rset.close();
}
if (stmt != null) {
stmt.close();
}
} catch (SQLException ex) {
}
}
}
void exitApplication() {
if (connection != null) {
try {
connection.close();
} catch (Exception ex) { gui.putStatus(" Error while Closing the connection: " +
ex.toString());
}
}
System.exit(0);
}
}
|