Edit D:\app\Administrator\product\11.2.0\dbhome_1\javavm\demo\examples\jsproc\basic\basic_kprb\Employee.java
// Employee.java import java.sql.*; import oracle.*; import oracle.jdbc.driver.*; class Employee { /* * find the name of the employee with a given employee number * * emp_num is a IN parameter, and the method returns a String * */ public static String getEmpName(int emp_num) throws SQLException { String name = null; Connection conn = null; Statement stmt = null; try { // load the kprb driver DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver ()); conn = DriverManager.getConnection("jdbc:oracle:kprb:"); // create a statement stmt = conn.createStatement(); // find the name of the employee with the given employee number ResultSet rset = stmt.executeQuery( "SELECT ENAME FROM EMPLOYEE WHERE ENUM = " + emp_num); // retrieve and print the results while (rset.next()) { name = rset.getString(1); } // return the name of the employee who has the given employee number return name; } finally { // don't forget to close the statement stmt.close(); } } /* * update the position and salary of an employee with the * given employee number * * Here, emp_num and raise are IN parameters; position is an IN/OUT * parameter; salary is an OUT parameter. * */ public static void updatePositionSalary(int emp_num, String[] position, int[] salary, int raise) throws SQLException { String pos = null; int sal = 0; Connection conn = null; Statement stmt = null; PreparedStatement pstmt = null; try { // create connection and statement DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver ()); conn = DriverManager.getConnection("jdbc:oracle:kprb:"); stmt = conn.createStatement(); pstmt = conn.prepareStatement("UPDATE EMPLOYEE SET POSITION = ?, " + " SALARY = SALARY + ? WHERE ENUM = ?"); // set up bind values and execute the update pstmt.setString(1, position[0]); pstmt.setInt(2, raise); pstmt.setInt(3, emp_num); pstmt.execute(); // retrieve the updated position and salary to verify that // the data has been updated in the database ResultSet rset = stmt.executeQuery( "SELECT POSITION, SALARY FROM EMPLOYEE WHERE ENUM = " + emp_num); while (rset.next()) { pos = rset.getString ("position"); sal = rset.getInt ("salary"); } } finally { stmt.close(); position[0] = pos; salary[0] = sal; } } }
Ms-Dos/Windows
Unix
Write backup
jsp File Browser version 1.2 by
www.vonloesch.de