【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-CallableStatementExample.java,希望本篇文章对您学习有所帮助。
package ejava.jdbcch10;
import java.util.Properties;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.CallableStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSetMetaData;
import java.lang.ClassNotFoundException;
import java.sql.PreparedStatement;
import java.util.Vector;
import java.sql.Types;
import ejava.jdbcch09.ConnectToDatabase;
/**
* Class CallableStatementExample
* @author
* @version %I%, %G%
*/
public class CallableStatementExample extends ConnectToDatabase {
/**
* Constructor
*
*/
public CallableStatementExample() {
super();
}
/**
* Constructor
* @param propertiesFile
* @see
*/
public CallableStatementExample(String propertiesFile)
throws FileNotFoundException, IOException {
super(propertiesFile);
}
/**
* Method getCallableStatement
* @param sqlStatement
* @return
* @throws Exception
* @throws SQLException
* @see
*/
protected CallableStatement getCallableStatement(String sqlStatement)
throws SQLException, Exception {
Connection connection = super.getConnection();
CallableStatement statement = connection.prepareCall(sqlStatement);
return statement;
}
/**
* Method
* @param sqlStatement
* @param resultSetType
* @param resultSetConcurrency
* @return
* @throws Exception
* @throws SQLException
* @see
*/
protected CallableStatement getCallableStatement(String sqlStatement,
int resultSetType,
int resultSetConcurrency) throws SQLException, Exception {
Connection connection = super.getConnection();
CallableStatement statement = connection.prepareCall(sqlStatement,
resultSetType, resultSetConcurrency);
return statement;
}
/**
* Method
*
*
* @throws Exception
* @throws SQLException
*
* @see
*/
public void executeGetTotalPriceFunction()
throws SQLException, Exception {
String sqlStatement = "begin ? := getTotalPriceForOrder( 1001);end;";
CallableStatement callableStatement =
getCallableStatement(sqlStatement);
callableStatement.registerOutParameter(1, Types.FLOAT);
callableStatement.execute();
System.out.println("Total price :" + callableStatement.getFloat(1));
}
/**
* Method
*
*
* @throws Exception
* @throws SQLException
*
* @see
*/
public void executeGetCustomerPhoneNumberFunction()
throws SQLException, Exception {
String sqlStatement = "begin ? := getCustomerPhoneNumber(101); end;";
CallableStatement callableStatement =
getCallableStatement(sqlStatement);
callableStatement.registerOutParameter(1, Types.VARCHAR);
callableStatement.execute();
System.out.println("Phone # :" + callableStatement.getString(1)
+ " for Customer_id 101");
}
/**
* Method
*
*
* @throws Exception
* @throws SQLException
*
* @see
*/
public void executeTotalOrdersFortheCustomer()
throws SQLException, Exception {
String sqlStatement = "begin getTotalOrders(104, ? ); end;";
CallableStatement callableStatement =
getCallableStatement(sqlStatement);
callableStatement.registerOutParameter(1, Types.INTEGER);
callableStatement.execute();
System.out.println("Total Orders # :" + callableStatement.getInt(1)
+ " for Customer_id 104");
}
/**
* Method
*
*
* @param args
*
* @see
*/
public static void main(String[] args) {
if (args.length == 0) {
System.out
.println("Error: you run the program as "
+ "java ejava.jdbcch10.CallableStatementExample <propertiesFileName> ");
System.exit(0);
}
String fileName = args[0];
CallableStatementExample callStExample =
new CallableStatementExample();
try {
callStExample.setProperties(fileName);
callStExample.executeGetCustomerPhoneNum