2024年4月11日发(作者:)

*/

private String genericQuery = "{ call ? := sp_get_stocks(?) }";

/**

* Connection to database

*/

private Connection conn = null;

/**

* Constructor. Loads the JDBC driver and establishes a connection

*

* @param host the host the db is on

* @param db the database name

* @param user user's name

* @param password user's password

*/

public JDBCDemo(String host, String db, String user, String password)

throws ClassNotFoundException, SQLException {

// construct the url

url = url + host + ":" + port + ":" + db;

// load the Oracle driver and establish a connection

try {

e(driver);

conn = nection(url, user, password);

}

catch (ClassNotFoundException ex) {

n("Failed to find driver class: " + driver);

throw ex;

}

catch (SQLException ex) {

n("Failed to establish a connection to: " + url);

throw ex;

}

}

/**

* Execute the stored procedure

*

* @param price price parameter for stored procedure

*/

private void execute(float price)

throws SQLException {

String query = useOracleQuery ? oracleQuery : genericQuery;

n("Query: " + query + "n");

CallableStatement stmt = eCall(query);

// register the type of the out param - an Oracle specific type

erOutParameter(1, );

// set the in param

at(2, price);

// execute and retrieve the result set

e();

ResultSet rs = (ResultSet)ect(1);

// print the results

while (()) {

n(ing(1) + "t" +

at(2) + "t" +

e(3).toString());

}

();

();

}

/**

* Cleanup the connection

*/

private void cleanup() throws SQLException {

if (conn != null)

();

}

/**

* Prints usage statement on stdout

*/

static private void usage() {

n("java mo " +

" host db user password price");

}

/**

* Runs the class

*/

public static void main(String[] args) throws Exception {

if ( != 5) {

();

(1);

}

else {

try {

// assign the args to sensible variables for clarity

String host = args[0];

String db = args[1];

String user = args[2];

String password = args[3];

float price = f(args[4]).floatValue();

// and execute the stored proc

JDBCDemo jdbc = new JDBCDemo(host, db, user, password);

e(price);

p();

}

catch (ClassNotFoundException ex) {

n("Demo failed");

}

catch (SQLException ex) {

n("Demo failed: " + sage());

}

}

}

}