1
-----------------存儲過程---------------------
2
create procedure FindCusts
3
@cust varchar(10)
4
as
5
select customerid from orders where customerid
6
like '%'+@cust+'%'
7
---------------執行---------------------------
8
execute FindCusts 'alfki'
--------------在Java中調用--------------------

2

3

4

5

6

7

8

1
import java.sql.*;
2
3
public class ProcedureTest {
4
public static void main(String args[]) throws Exception {
5
//加載驅動
6
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
7
//獲得連接
8
Connection conn = DriverManager.getConnection("jdbc:odbc:mydata", "sa",
9
"");
10
//創建存儲過程的對象
11
CallableStatement c = conn.prepareCall("{call FindCusts(?)}");
12
c.setString(1, "Tom");
13
ResultSet rs = c.executeQuery();
14
while (rs.next()) {
15
String cust = rs.getString("customerid");
16
System.out.println(cust);
17
}
18
c.close();
19
}
20
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
