Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)

时间:2021-06-11 04:25:02
一:无返回值的存储过程调用

存储过程:

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)CREATE OR REPLACE PROCEDURE PRO_1(PARA1 IN VARCHAR2, PARA2 IN VARCHAR2) AS  
   BEGIN          
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    INSERT INTO DBO.EMP (ID,NAME) VALUES(PARA1, PARA2);   
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)END PRO_1; 

Java代码:

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)import java.sql.*;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)import java.sql.ResultSet;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)public class CallProcedureTest1 {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    public CallProcedureTest1() {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        super();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    public static void main(String[] args) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String driver = "oracle.jdbc.driver.OracleDriver";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl ";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String user = "admin";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String pwd = "password";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        Connection conn = null;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        CallableStatement cs = null;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        ResultSet rs = null;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        try {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            Class.forName(driver);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            conn = DriverManager.getConnection(url, user, pwd);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs = conn.prepareCall("{ call DBO.PRO_1(?,?) }");
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs.setString(1, "10");
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs.setString(2, "Peter");
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs.execute();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }
 catch (SQLException e) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            e.printStackTrace();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }
 catch (Exception e) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            e.printStackTrace();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }
 finally {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            try {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                if (rs != null{
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    rs.close();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                if (cs != null{
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    cs.close();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                if (conn != null{
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    conn.close();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            }
 catch (SQLException e) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)}

备注,存储过程PRO_1中用到了表EMP(ID, NAME),需事先建好

 

二:有返回值的存储过程(非结果集)   

存储过程:

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)CREATE OR REPLACE PROCEDURE PRO_2(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2) AS   
   BEGIN        
       SELECT INTO PARA2 FROM EMP WHERE ID= PARA1;    
   END PRO_2; 

 

Java代码:

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)import java.sql.*;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)public class CallProcedureTest2 {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    public CallProcedureTest2() {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        super();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    public static void main(String[] args) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String driver = "oracle.jdbc.driver.OracleDriver";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String user = "admin";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String pwd = "password";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        Connection conn = null;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        CallableStatement cs = null;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        ResultSet rs = null;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        try {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            Class.forName(driver);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            conn = DriverManager.getConnection(url, user, pwd);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs = conn.prepareCall("{ call DBO.PRO_2(?,?) }");
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs.setString(1, "10");
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs.registerOutParameter(2, Types.VARCHAR);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs.execute();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            String name = cs.getString(2);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            System.out.println("name: " + name);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }
 catch (SQLException e) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            e.printStackTrace();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }
 catch (Exception e) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            e.printStackTrace();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }
 finally {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            try {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                if (rs != null{
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    rs.close();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                if (cs != null{
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    cs.close();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                if (conn != null{
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    conn.close();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            }
 catch (SQLException e) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)}

 

注意:cs.getString(2)中的数值2并非任意的,而是和存储过程中的out列对应的,如果out是在第一个位置,那就是proc.getString(1),如果是第三个位置,就是proc.getString(3),当然也可以同时有多个返回值,那就是再多加几个out参数了。   

 

三:返回列表     

由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用pagkage了.所以要分两部分,   

 

1. 建一个程序包。如下:

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)CREATE OR REPLACE PACKAGE MYPACKAGE AS TYPE MY_CURSOR IS REF CURSOR;   END MYPACKAGE;

 

2. 建立存储过程,如下:

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)CREATE OR REPLACE PROCEDURE PRO_3(p_CURSOR out MYPACKAGE.MY_CURSOR) AS   
   BEGIN          
       OPEN p_CURSOR FOR SELECT * FROM DBO.EMP;   
   END PRO_3; 


可以看到,它是把游标(可以理解为一个指针),作为一个out 参数来返回值的。   

  

Java代码:

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)import java.sql.*;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)import java.sql.ResultSet;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)public class CallProcedureTest2 {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    public CallProcedureTest2() {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        super();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    public static void main(String[] args) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String driver = "oracle.jdbc.driver.OracleDriver";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String user = "admin";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        String pwd = "password";
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        Connection conn = null;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        CallableStatement cs = null;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        ResultSet rs = null;
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        try {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            Class.forName(driver);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            conn = DriverManager.getConnection(url, user, pwd);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs = conn.prepareCall("{ call DBO.PRO_3(?) }");
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            cs.execute();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            rs = (ResultSet) cs.getObject(1);
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            while (rs.next()) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                System.out.println("\t" + rs.getString(1) + "\t"
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                        + rs.getString(2) + "\t");
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }
 catch (SQLException e) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            e.printStackTrace();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }
 catch (Exception e) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            e.printStackTrace();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }
 finally {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            try {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                if (rs != null{
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    rs.close();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    if (cs != null{
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                        cs.close();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    if (conn != null{
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                        conn.close();
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                    }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)                }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            }
 catch (SQLException e) {
Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)            }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)        }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)    }

Java(CallableStatement)调用Oracle存储过程返回结果集(ResultSet)}
转:http://www.blogjava.net/icewee/archive/2011/04/18/353198.html