// Import the following packages to use JDBC.
import java.sql.*;
import java.io.*;
import oracle.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;
class SPExample {
public static void main( String args[ ] ) throws SQLException {
String user = "user_id";
String password = "password";
String database = "20.185.147.112:1521/xe";
// Open an OracleDataSource and get a connection.
OracleDataSource ods = new OracleDataSource( );
ods.setURL ( "jdbc:oracle:thin:@" + database );
ods.setUser ( user );
ods.setPassword( password );
Connection conn = ods.getConnection( );
try {
// Create a statement.
Statement stmt = conn.createStatement( );
// Create the stored function.
stmt.execute ( "CREATE OR REPLACE FUNCTION raisesal " +
"(name CHAR, raise NUMBER) RETURN NUMBER IS" +
"BEGIN RETURN raise + 100000; END;" );
// Close the statement.
stmt.close( );
// Prepare to call the stored procedure RAISESAL.
// This sample uses the SQL92 syntax
CallableStatement cstmt = conn.prepareCall ( "{? = call RAISESAL (?, ?)}" );
// Declare that the first ? is a return value of type Int.
cstmt.registerOutParameter( 1, Types.INTEGER );
// We want to raise LESLIE's salary by the web input.
cstmt.setString( 2, "LESLIE" ); // The name argument is the second ?
int raise = Integer.parseInt( args[0].trim( ) );
cstmt.setInt ( 3, raise ); // The raise argument is the third ?
// Do the raise.
cstmt.execute( );
// Get the new salary back.
int new_salary = cstmt.getInt( 1 );
System.out.print( "The new salary is <i>" + new_salary + "</i>" );
// Close the statement.
cstmt.close( );
}
catch( SQLException e ) { System.out.println( e ); }
// Close the connection.
conn.close( );
}
}
|