1. 程式人生 > >Java中呼叫資料庫的儲存過程儲存函式和包體

Java中呼叫資料庫的儲存過程儲存函式和包體

1.java連線oracle資料庫的jdbc程式

public class JDBCUtils {
    private static String driver = "oracle.jdbc.OracleDriver";
    private static String url = "jdbc:oracle:thin:@192.168.209.130:1521/orcl";
    private static String user = "scott";
    private static String password = "tiger";

    static {
        //註冊驅動
try { Class.forName(driver); } catch (ClassNotFoundException e) { throw new RuntimeException(); } } //連線資料庫 public static Connection getConnection() { try { return DriverManager.getConnection(url, user, password); } catch
(SQLException e) { e.printStackTrace(); } return null; } public static void release(Connection conn,Statement stat,ResultSet rs) { if(conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); }finally
{ conn = null; } } if(stat != null) { try { stat.close(); } catch (SQLException e) { e.printStackTrace(); }finally { stat = null; } } if(rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); }finally { rs = null; } } } }

2.呼叫儲存過程

String sql = "{call queryEmpInformation(?,?,?,?)}";
        Connection conn = null;
        CallableStatement call = null;

        try {
            conn = JDBCUtils.getConnection();
            call = conn.prepareCall(sql);
            //對於in引數 賦值
            call.setInt(1, 7839);
            //對於out引數,申明
            call.registerOutParameter(2, OracleTypes.VARCHAR);
            call.registerOutParameter(3, OracleTypes.NUMBER);
            call.registerOutParameter(4, OracleTypes.VARCHAR);
            //執行
            call.execute();
            //取值
            String name = call.getString(2);
            double sal = call.getDouble(3);
            String job = call.getString(4);
            System.out.println(name +'\t' +sal+'\t' +job);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.release(conn, call, null);
        }

3.呼叫儲存函式

String sql = "{?= call sumsal(?)}";

        Connection conn = null;
        CallableStatement call = null;

        try {
            conn = JDBCUtils.getConnection();
            call = conn.prepareCall(sql);
            //對於out引數,申明
            call.registerOutParameter(1, OracleTypes.NUMBER);
            //對於in引數,賦值
            call.setInt(2, 7839);
            //執行
            call.execute();
            //取值
            double sum = call.getDouble(1);
            System.out.println(sum);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.release(conn, call, null);
        }

4.呼叫包體

String sql = "{call mypackage.queryemplist(?,?)}";
        Connection conn = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            call = conn.prepareCall(sql);
            //對於in引數 賦值
            call.setInt(1,30);
            //對於out引數,申明
            call.registerOutParameter(2, OracleTypes.CURSOR);
            //執行
            call.execute();
            //取值
            rs = ((OracleCallableStatement)call).getCursor(2);
            //遍歷結果集
            while(rs.next()) {
                String name = rs.getString("ename");
                double sal = rs.getDouble("sal");
                String job = rs.getString("job");
                System.out.println(name + '\t' +sal +'\t' +job);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.release(conn, call, rs);
        }
    }