Java调用存储过程
【转自】http://www.blogjava.net/TrampEagle/archive/2005/12/13/23605.aspx
一:无返回值的存储过程
存储过程为:
1 CREATE OR REPLACE PROCEDURE TESTA(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2) AS
2
3 BEGIN
4
5 INSERT INTO HYQ.B_ID (I_ID,I_NAME) VALUES (PARA1, PARA2);
6
7 END TESTA;
8
2
3 BEGIN
4
5 INSERT INTO HYQ.B_ID (I_ID,I_NAME) VALUES (PARA1, PARA2);
6
7 END TESTA;
8
然后呢,在java里调用时就用下面的代码:
package com.hyq.src;
import java.sql.*;
import java.sql.ResultSet;
public class TestProcedureOne {
public TestProcedureOne() {
}
public static void main(String[] args ){
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521: hyq ";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
CallableStatement cstmt = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl, " hyq ", " hyq ");
CallableStatement proc = null;
proc = conn.prepareCall("{ call HYQ.TESTA(?,?) }");
proc.setString(1, "100");
proc.setString(2, "TestOne");
proc.execute();
}
catch (SQLException ex2) {
ex2.printStackTrace();
}
catch (Exception ex2) {
ex2.printStackTrace();
}
finally{
try {
if(rs != null){
rs.close();
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
catch (SQLException ex1) {
}
}
}
}
import java.sql.*;
import java.sql.ResultSet;
public class TestProcedureOne {
public TestProcedureOne() {
}
public static void main(String[] args ){
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521: hyq ";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
CallableStatement cstmt = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl, " hyq ", " hyq ");
CallableStatement proc = null;
proc = conn.prepareCall("{ call HYQ.TESTA(?,?) }");
proc.setString(1, "100");
proc.setString(2, "TestOne");
proc.execute();
}
catch (SQLException ex2) {
ex2.printStackTrace();
}
catch (Exception ex2) {
ex2.printStackTrace();
}
finally{
try {
if(rs != null){
rs.close();
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
catch (SQLException ex1) {
}
}
}
}
当然了,这就先要求要建张表TESTTB,里面两个字段(I_ID,I_NAME)。
二:有返回值的存储过程(非列表)
存储过程为:
1 CREATE OR REPLACE PROCEDURE TESTB(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2) AS
2
3 BEGIN
4
5 SELECT INTO PARA2 FROM TESTTB WHERE I_ID= PARA1;
6
7 END TESTB;
8
2
3 BEGIN
4
5 SELECT INTO PARA2 FROM TESTTB WHERE I_ID= PARA1;
6
7 END TESTB;
8
在java里调用时就用下面的代码:
1 package com.hyq.src;
2
3
4
5 public class TestProcedureTWO {
6
7 public TestProcedureTWO() {
8
9 }
10
11 public static void main(String[] args ){
12
13 String driver = "oracle.jdbc.driver.OracleDriver";
14
15 String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";
16
17 Statement stmt = null;
18
19 ResultSet rs = null;
20
21 Connection conn = null;
22
23 try {
24
25 Class.forName(driver);
26
27 conn = DriverManager.getConnection(strUrl, " hyq ", " hyq ");
28
29 CallableStatement proc = null;
30
31 proc = conn.prepareCall("{ call HYQ.TESTB(?,?) }");
32
33 proc.setString(1, "100");
34
35 proc.registerOutParameter(2, Types.VARCHAR);
36
37 proc.execute();
38
39 String testPrint = proc.getString(2);
40
41 System.out.println("=testPrint=is="+testPrint);
42
43 }
44
45 catch (SQLException ex2) {
46
47 ex2.printStackTrace();
48
49 }
50
51 catch (Exception ex2) {
52
53 ex2.printStackTrace();
54
55 }
56
57 finally{
58
59 try {
60
61 if(rs != null){
62
63 rs.close();
64
65 if(stmt!=null){
66
67 stmt.close();
68
69 }
70
71 if(conn!=null){
72
73 conn.close();
74
75 }
76
77 }
78
79 }
80
81 catch (SQLException ex1) {
82
83 }
84
85 }
86
87 }
88
89 }
90
91
92
93 }
94
95
2
3
4
5 public class TestProcedureTWO {
6
7 public TestProcedureTWO() {
8
9 }
10
11 public static void main(String[] args ){
12
13 String driver = "oracle.jdbc.driver.OracleDriver";
14
15 String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";
16
17 Statement stmt = null;
18
19 ResultSet rs = null;
20
21 Connection conn = null;
22
23 try {
24
25 Class.forName(driver);
26
27 conn = DriverManager.getConnection(strUrl, " hyq ", " hyq ");
28
29 CallableStatement proc = null;
30
31 proc = conn.prepareCall("{ call HYQ.TESTB(?,?) }");
32
33 proc.setString(1, "100");
34
35 proc.registerOutParameter(2, Types.VARCHAR);
36
37 proc.execute();
38
39 String testPrint = proc.getString(2);
40
41 System.out.println("=testPrint=is="+testPrint);
42
43 }
44
45 catch (SQLException ex2) {
46
47 ex2.printStackTrace();
48
49 }
50
51 catch (Exception ex2) {
52
53 ex2.printStackTrace();
54
55 }
56
57 finally{
58
59 try {
60
61 if(rs != null){
62
63 rs.close();
64
65 if(stmt!=null){
66
67 stmt.close();
68
69 }
70
71 if(conn!=null){
72
73 conn.close();
74
75 }
76
77 }
78
79 }
80
81 catch (SQLException ex1) {
82
83 }
84
85 }
86
87 }
88
89 }
90
91
92
93 }
94
95
注意,这里的proc.getString(2)中的数值2并非任意的,而是和存储过程中的out列对应的,如果out是在第一个位置,那就是proc.getString(1),如果是第三个位置,就是proc.getString(3),当然也可以同时有多个返回值,那就是再多加几个out参数了。
三:返回列表
由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用pagkage了.所以要分两部分,
1, 建一个程序包。如下:
1 CREATE OR REPLACE PACKAGE TESTPACKAGE AS
2
3 TYPE Test_CURSOR IS REF CURSOR;
4
5 end TESTPACKAGE;
6
7
2
3 TYPE Test_CURSOR IS REF CURSOR;
4
5 end TESTPACKAGE;
6
7
2,建立存储过程,存储过程为:
1 CREATE OR REPLACE PROCEDURE TESTC(p_CURSOR out TESTPACKAGE.Test_CURSOR) IS
2
3 BEGIN
4
5 OPEN p_CURSOR FOR SELECT * FROM HYQ.TESTTB;
6
7 END TESTC;
8
9
2
3 BEGIN
4
5 OPEN p_CURSOR FOR SELECT * FROM HYQ.TESTTB;
6
7 END TESTC;
8
9
可以看到,它是把游标(可以理解为一个指针),作为一个out 参数来返回值的。
在java里调用时就用下面的代码:
1 package com.hyq.src;
2
3 import java.sql.*;
4
5 import java.io.OutputStream;
6
7 import java.io.Writer;
8
9 import java.sql.PreparedStatement;
10
11 import java.sql.ResultSet;
12
13 import oracle.jdbc.driver.*;
14
15
16
17
18
19 public class TestProcedureTHREE {
20
21 public TestProcedureTHREE() {
22
23 }
24
25 public static void main(String[] args ){
26
27 String driver = "oracle.jdbc.driver.OracleDriver";
28
29 String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";
30
31 Statement stmt = null;
32
33 ResultSet rs = null;
34
35 Connection conn = null;
36
37
38
39 try {
40
41 Class.forName(driver);
42
43 conn = DriverManager.getConnection(strUrl, "hyq", "hyq");
44
45
46
47 CallableStatement proc = null;
48
49 proc = conn.prepareCall("{ call hyq.testc(?) }");
50
51 proc.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);
52
53 proc.execute();
54
55 rs = (ResultSet)proc.getObject(1);
56
57
58
59 while(rs.next())
60
61 {
62
63 System.out.println("<tr><td>" + rs.getString(1) + "</td><td>"+rs.getString(2)+"</td></tr>");
64
65 }
66
67 }
68
69 catch (SQLException ex2) {
70
71 ex2.printStackTrace();
72
73 }
74
75 catch (Exception ex2) {
76
77 ex2.printStackTrace();
78
79 }
80
81 finally{
82
83 try {
84
85 if(rs != null){
86
87 rs.close();
88
89 if(stmt!=null){
90
91 stmt.close();
92
93 }
94
95 if(conn!=null){
96
97 conn.close();
98
99 }
100
101 }
102
103 }
104
105 catch (SQLException ex1) {
106
107 }
108
109 }
110
111 }
112
113 }
114
115
2
3 import java.sql.*;
4
5 import java.io.OutputStream;
6
7 import java.io.Writer;
8
9 import java.sql.PreparedStatement;
10
11 import java.sql.ResultSet;
12
13 import oracle.jdbc.driver.*;
14
15
16
17
18
19 public class TestProcedureTHREE {
20
21 public TestProcedureTHREE() {
22
23 }
24
25 public static void main(String[] args ){
26
27 String driver = "oracle.jdbc.driver.OracleDriver";
28
29 String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";
30
31 Statement stmt = null;
32
33 ResultSet rs = null;
34
35 Connection conn = null;
36
37
38
39 try {
40
41 Class.forName(driver);
42
43 conn = DriverManager.getConnection(strUrl, "hyq", "hyq");
44
45
46
47 CallableStatement proc = null;
48
49 proc = conn.prepareCall("{ call hyq.testc(?) }");
50
51 proc.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);
52
53 proc.execute();
54
55 rs = (ResultSet)proc.getObject(1);
56
57
58
59 while(rs.next())
60
61 {
62
63 System.out.println("<tr><td>" + rs.getString(1) + "</td><td>"+rs.getString(2)+"</td></tr>");
64
65 }
66
67 }
68
69 catch (SQLException ex2) {
70
71 ex2.printStackTrace();
72
73 }
74
75 catch (Exception ex2) {
76
77 ex2.printStackTrace();
78
79 }
80
81 finally{
82
83 try {
84
85 if(rs != null){
86
87 rs.close();
88
89 if(stmt!=null){
90
91 stmt.close();
92
93 }
94
95 if(conn!=null){
96
97 conn.close();
98
99 }
100
101 }
102
103 }
104
105 catch (SQLException ex1) {
106
107 }
108
109 }
110
111 }
112
113 }
114
115
在这里要注意,在执行前一定要先把oracle的驱动包放到class路径里,否则会报错的。