一、Java如何實現對存儲過程的調用:
A:不帶輸出參數的
- create procedure getsum
- @n int =0<--此處為參數-->
- as
- declare @sum int<--定義變量-->
- declare @i int
- set @sum=0
- set @i=0
- while @i<=@n begin
- set @sum=@sum+@i
- set @i=@i+1
- end
- print 'the sum is '+ltrim(rtrim(str(@sum)))
在SQL中執行:
exec getsum 100
在Java中調用:
JAVA可以調用 但是在Java程序卻不能去顯示該存儲過程的結果 因為上面的存儲過程的參數類型int 傳遞方式是in(按值)方式
- import Java.sql.*;
- public class ProcedureTest
- {
- public static void main(String args[]) throws Exception
- {
- //加載驅動
- DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
- //獲得連接
- Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
- //創建存儲過程的對象
- CallableStatement c=conn.prepareCall("{call getsum(?)}");
- //給存儲過程的參數設置值
- c.setInt(1,100); //將第一個參數的值設置成100
- //執行存儲過程
- c.execute();
- conn.close();
- }
- }
B:帶輸出參數的
1:返回int
- alter procedure getsum
- @n int =0,
- @result int output
- as
- declare @sum int
- declare @i int
- set @sum=0
- set @i=0
- while @i<=@n begin
- set @sum=@sum+@i
- set @i=@i+1
- end
- set @result=@sum
在查詢分析器中執行:
- declare @myResult int
- exec getsum 100,@myResult output
- print @myResult
在Java中調用:
- import Java.sql.*;
- public class ProcedureTest
- {
- public static void main(String args[]) throws Exception
- {
- //加載驅動
- DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
- //獲得連接
- Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
- //創建存儲過程的對象
- CallableStatement c=conn.prepareCall("{call getsum(?,?)}");
- //給存儲過程的第一個參數設置值
- c.setInt(1,100);
- //注冊存儲過程的第二個參數
- c.registerOutParameter(2,Java.sql.Types.INTEGER);
- //執行存儲過程
- c.execute();
- //得到存儲過程的輸出參數值
- System.out.println (c.getInt(2));
- conn.close();
- }
- }
2:返回varchar
存儲過程帶游標:
在存儲過程中帶游標 使用游標不停的遍歷orderid
- create procedure CursorIntoProcedure
- @pname varchar(8000) output
- as
- --定義游標
- declare cur cursor for select orderid from orders
- --定義一個變量來接收游標的值
- declare @v varchar(5)
- --打開游標
- open cur
- set @pname=''--給@pname初值
- --提取游標的值
- fetch next from cur into @v
- while @@fetch_status=0
- begin
- set @pname=@pname+';'+@v
- fetch next from cur into @v
- end
- print @pname
- --關閉游標
- close cur
- --銷毀游標
- deallocate cur
執行存儲過程:
- exec CursorIntoProcedure ''
Java調用:
- import Java.sql.*;
- public class ProcedureTest
- {
- public static void main(String args[]) throws Exception
- {
- //加載驅動
- DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
- //獲得連接
- Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
- CallableStatement c=conn.prepareCall("{call CursorIntoProcedure(?)}");
- c.registerOutParameter(1,Java.sql.Types.VARCHAR);
- c.execute();
- System.out.println (c.getString(1));
- conn.close();
- }
- }
C:刪除數據的存儲過程
存儲過程:
- drop table 學生基本信息表
- create table 學生基本信息表
- (
- StuID int primary key,
- StuName varchar(10),
- StuAddress varchar(20)
- )
- insert into 學生基本信息表 values(1,'三毛','wuhan')
- insert into 學生基本信息表 values(2,'三毛','wuhan')
- create table 學生成績表
- (
- StuID int,
- Chinese int,
- PyhSics int
- foreign key(StuID) references 學生基本信息表(StuID)
- on delete cascade
- on update cascade
- )
- insert into 學生成績表 values(1,99,100)
- insert into 學生成績表 values(2,99,100)
創建存儲過程:
- create procedure delePro
- @StuID int
- as
- delete from 學生基本信息表 where StuID=@StuID
- --創建完畢
- exec delePro 1 --執行存儲過程
- --創建存儲過程
- create procedure selePro
- as
- select * from 學生基本信息表
- --創建完畢
- exec selePro --執行存儲過程
在Java中調用:
- import Java.sql.*;
- public class ProcedureTest
- {
- public static void main(String args[]) throws Exception
- {
- //加載驅動
- DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
- //獲得連接
- Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
- //創建存儲過程的對象
- CallableStatement c=conn.prepareCall("{call delePro(?)}");
- c.setInt(1,1);
- c.execute();
- c=conn.prepareCall("{call selePro}");
- ResultSet rs=c.executeQuery();
- while(rs.next())
- {
- String Stu=rs.getString("StuID");
- String name=rs.getString("StuName");
- String add=rs.getString("StuAddress");
- System.out.println ("學號:"+" "+"姓名:"+" "+"地址");
- System.out.println (Stu+" "+name+" "+add);
- }
- c.close();
- }
- }
D:修改數據的存儲過程
創建存儲過程:
- create procedure ModPro
- @StuID int,
- @StuName varchar(10)
- as
- update 學生基本信息表 set StuName=@StuName where StuID=@StuID
執行存儲過程:
- exec ModPro 2,'四毛'
Java調用存儲過程:
- import Java.sql.*;
- public class ProcedureTest
- {
- public static void main(String args[]) throws Exception
- {
- //加載驅動
- DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
- //獲得連接
- Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
- //創建存儲過程的對象
- CallableStatement c=conn.prepareCall("{call ModPro(?,?)}");
- c.setInt(1,2);
- c.setString(2,"美女");
- c.execute();
- c=conn.prepareCall("{call selePro}");
- ResultSet rs=c.executeQuery();
- while(rs.next())
- {
- String Stu=rs.getString("StuID");
- String name=rs.getString("StuName");
- String add=rs.getString("StuAddress");
- System.out.println ("學號:"+" "+"姓名:"+" "+"地址");
- System.out.println (Stu+" "+name+" "+add);
- }
- c.close();
- }
- }
E:查詢數據的存儲過程(模糊查詢)
存儲過程:
- create procedure FindCusts
- @cust varchar(10)
- as
- select customerid from orders where customerid
- like '%'+@cust+'%'
執行:
- execute FindCusts 'alfki'
在Java中調用:
- import Java.sql.*;
- public class ProcedureTest
- {
- public static void main(String args[]) throws Exception
- {
- //加載驅動
- DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
- //獲得連接
- Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
- //創建存儲過程的對象
- CallableStatement c=conn.prepareCall("{call FindCusts(?)}");
- c.setString(1,"Tom");
- ResultSet rs=c.executeQuery();
- while(rs.next())
- {
- String cust=rs.getString("customerid");
- System.out.println (cust);
- }
- c.close();
- }
- }
F:增加數據的存儲過程
存儲過程:
- create procedure InsertPro
- @StuID int,
- @StuName varchar(10),
- @StuAddress varchar(20)
- as
- insert into 學生基本信息表 values(@StuID,@StuName,@StuAddress)
調用存儲過程:
- exec InsertPro 5,'555','555'
在Java中執行:
- import Java.sql.*;
- public class ProcedureTest
- {
- public static void main(String args[]) throws Exception
- {
- //加載驅動
- DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
- //獲得連接
- Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
- //創建存儲過程的對象
- CallableStatement c=conn.prepareCall("{call InsertPro(?,?,?)}");
- c.setInt(1,6);
- c.setString(2,"Liu");
- c.setString(3,"wuhan");
- c.execute();
- c=conn.prepareCall("{call selePro}");
- ResultSet rs=c.executeQuery();
- while(rs.next())
- {
- String stuid=rs.getString("StuID");
- String name=rs.getString("StuName");
- String address=rs.getString("StuAddress");
- System.out.println (stuid+" "+name+" "+address);
- }
- c.close();
- }
- }
G:在JAVA中創建存儲過程 並且在Java中直接調用
- import Java.sql.*;
- public class ProcedureTest
- {
- public static void main(String args[]) throws Exception
- {
- //加載驅動
- DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
- //獲得連接
- Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
- Statement stmt=conn.createStatement();
- //在Java中創建存儲過程
- stmt.executeUpdate("create procedure OOP as select * from 學生成績表");
- CallableStatement c=conn.prepareCall("{call OOP}");
- ResultSet rs=c.executeQuery();
- while(rs.next())
- {
- String chinese=rs.getString("Chinese");
- System.out.println (chinese);
- }
- conn.close();
- }
- }