package oracle.otnsamples.jdbc;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;
import oracle.jdbc.pool.OracleDataSource;
public class UsingNamedParameters {
private static Connection conn = null;
private static CallableStatement cstmt = null;
public UsingNamedParameters() {
}
public static void main(String[] args) throws SQLException,
ClassNotFoundException {
try {
int minprice = Integer.parseInt(args[1]);
int listprice = Integer.parseInt(args[3]);
UsingNamedParameters.namedParametersInCS(args[0], minprice,
args[2], listprice);
} catch (Exception ex) {
System.out.println("unknown exception: "+ex.toString());
ex.printStackTrace();
}
}
private static void namedParametersInCS(String name, int minprice,
String desc, int listprice)
throws SQLException, ClassNotFoundException {
try {
UsingNamedParameters.dbConnection();
String sqlCall = "BEGIN createProduct(?,?,?,?,?); END;";
cstmt = (CallableStatement)conn.prepareCall(sqlCall);
cstmt.setString("productname",name); cstmt.setString("productdesc",desc); cstmt.setInt("listprice",listprice); cstmt.setInt("minprice",minprice);
cstmt.registerOutParameter("prod_id",Types.INTEGER);
cstmt.execute();
int id = cstmt.getInt("prod_id");
System.out.println("The new Product id is: "+id);
if ((id==1)||(id==-1)) {
System.out.println("Product record was not created due to some "+
" errors. "+"\n"+ "Check the Troubleshooting"+
" section in the document: Install.html");
}
} catch (SQLException sqlex) {
System.out.println("Problem while connecting and querying the " +
"database table: " + sqlex.toString());
sqlex.printStackTrace();
} catch (Exception ex) {
System.out.println("Problem with jdbc driver in the method: " +
ex.toString());
ex.printStackTrace();
} finally {
if (cstmt!=null) cstmt.close();
if (conn!=null) conn.close();
}
}
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 static void dbConnection() {
try {
Properties prop = UsingNamedParameters.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"));
conn = ods.getConnection();
} 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());
}
}
}
|