一:Java如何實現(xiàn)對存儲過程的調(diào)用: A:不帶輸出參數(shù)的 ---------------不帶輸出參數(shù)的---------------------------------- create procedure getsum @n int =0<--此處為參數(shù)--> 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中執(zhí)行:-------------------- exec getsum 100 ------------在JAVA中調(diào)用:--------------------- JAVA可以調(diào)用 但是在JAVA程序卻不能去顯示該存儲過程的結(jié)果 因為上面的存儲 過程的參數(shù)類型int 傳遞方式是in(按值)方式 import java.sql.*; public class ProcedureTest { public static void main(String args[]) throws Exception { //加載驅(qū)動 DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver()); //獲得連接 Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa",""); //創(chuàng)建存儲過程的對象 CallableStatement c=conn.prepareCall("{call getsum(?)}"); //給存儲過程的參數(shù)設(shè)置值 c.setInt(1,100); //將第一個參數(shù)的值設(shè)置成100 //執(zhí)行存儲過程 c.execute(); conn.close(); } } B:帶輸出參數(shù)的 1:返回int -------------------------帶輸出參數(shù)的---------------- 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 -------------------在查詢分析器中執(zhí)行------------ declare @myResult int exec getsum 100,@myResult output print @myResult ------------在JAVA中調(diào)用--------------------- import java.sql.*; public class ProcedureTest { public static void main(String args[]) throws Exception { //加載驅(qū)動 DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver()); //獲得連接 Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa",""); //創(chuàng)建存儲過程的對象 CallableStatement c=conn.prepareCall("{call getsum(?,?)}"); //給存儲過程的第一個參數(shù)設(shè)置值 c.setInt(1,100); //注冊存儲過程的第二個參數(shù) c.registerOutParameter(2,java.sql.Types.INTEGER); //執(zhí)行存儲過程 c.execute(); //得到存儲過程的輸出參數(shù)值 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 --關(guān)閉游標 close cur --銷毀游標 deallocate cur ------------執(zhí)行存儲過程-------------- exec CursorIntoProcedure '' --------------JAVA調(diào)用------------------ import java.sql.*; public class ProcedureTest { public static void main(String args[]) throws Exception { //加載驅(qū)動 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:刪除數(shù)據(jù)的存儲過程 ------------------存儲過程-------------------------- drop table 學(xué)生基本信息表 create table 學(xué)生基本信息表 ( StuID int primary key, StuName varchar(10), StuAddress varchar(20) ) insert into 學(xué)生基本信息表 values(1,'三毛','wuhan') insert into 學(xué)生基本信息表 values(2,'三毛','wuhan') create table 學(xué)生成績表 ( StuID int, Chinese int, PyhSics int foreign key(StuID) references 學(xué)生基本信息表(StuID) on delete cascade on update cascade ) insert into 學(xué)生成績表 values(1,99,100) insert into 學(xué)生成績表 values(2,99,100) --創(chuàng)建存儲過程 create procedure delePro @StuID int as delete from 學(xué)生基本信息表 where StuID=@StuID --創(chuàng)建完畢 exec delePro 1 --執(zhí)行存儲過程 --創(chuàng)建存儲過程 create procedure selePro as select * from 學(xué)生基本信息表 --創(chuàng)建完畢 exec selePro --執(zhí)行存儲過程 ------------------在JAVA中調(diào)用---------------- import java.sql.*; public class ProcedureTest { public static void main(String args[]) throws Exception { //加載驅(qū)動 DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver()); //獲得連接 Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa",""); //創(chuàng)建存儲過程的對象 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 ("學(xué)號:"+" "+"姓名:"+" "+"地址"); System.out.println (Stu+" "+name+" "+add); } c.close(); } } D:修改數(shù)據(jù)的存儲過程 ---------------------創(chuàng)建存儲過程--------------------- create procedure ModPro @StuID int, @StuName varchar(10) as update 學(xué)生基本信息表 set StuName=@StuName where StuID=@StuID -------------執(zhí)行存儲過程------------------------- exec ModPro 2,'四毛' ---------------JAVA調(diào)用存儲過程-------------------- import java.sql.*; public class ProcedureTest { public static void main(String args[]) throws Exception { //加載驅(qū)動 DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver()); //獲得連接 Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa",""); //創(chuàng)建存儲過程的對象 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 ("學(xué)號:"+" "+"姓名:"+" "+"地址"); System.out.println (Stu+" "+name+" "+add); } c.close(); } } E:查詢數(shù)據(jù)的存儲過程(模糊查詢) -----------------存儲過程--------------------- create procedure FindCusts @cust varchar(10) as select customerid from orders where customerid like '%'+@cust+'%' ---------------執(zhí)行--------------------------- execute FindCusts 'alfki' -------------在JAVA中調(diào)用-------------------------- import java.sql.*; public class ProcedureTest { public static void main(String args[]) throws Exception { //加載驅(qū)動 DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver()); //獲得連接 Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa",""); //創(chuàng)建存儲過程的對象 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:增加數(shù)據(jù)的存儲過程 ------------存儲過程-------------------- create procedure InsertPro @StuID int, @StuName varchar(10), @StuAddress varchar(20) as insert into 學(xué)生基本信息表 values(@StuID,@StuName,@StuAddress) -----------調(diào)用存儲過程--------------- exec InsertPro 5,'555','555' -----------在JAVA中執(zhí)行------------- import java.sql.*; public class ProcedureTest { public static void main(String args[]) throws Exception { //加載驅(qū)動 DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver()); //獲得連接 Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa",""); //創(chuàng)建存儲過程的對象 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中創(chuàng)建存儲過程 并且在JAVA中直接調(diào)用 import java.sql.*; public class ProcedureTest { public static void main(String args[]) throws Exception { //加載驅(qū)動 DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver()); //獲得連接 Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa",""); Statement stmt=conn.createStatement(); //在JAVA中創(chuàng)建存儲過程 stmt.executeUpdate("create procedure OOP as select * from 學(xué)生成績表"); CallableStatement c=conn.prepareCall("{call OOP}"); ResultSet rs=c.executeQuery(); while(rs.next()) { String chinese=rs.getString("Chinese"); System.out.println (chinese); } conn.close(); } }
|
|