Java对存储过程的调用方法

一、Java如何实现对存储过程的调用:

A:不带输出参数的

  1. createprocedure getsum
  2. @n int =0<--此处为参数-->
  3. as
  4. declare @sumint<--定义变量-->
  5. declare @i int
  6. set @sum=0
  7. set @i=0
  8. while @i<=@n begin
  9. set @sum=@sum+@i
  10. set @i=@i+1
  11. end
  12. print 'the sum is '+ltrim(rtrim(str(@sum)))

在SQL中执行:

exec getsum 100

在JAVA中调用:

JAVA可以调用 但是在JAVA程序却不能去显示该存储过程的结果 因为上面的存储过程的参数类型int 传递方式是in(按值)方式

  1. import java.sql.*;
  2. publicclass ProcedureTest
  3. {
  4. publicstaticvoid main(String args[]) throws Exception
  5. {
  6. //加载驱动
  7. DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
  8. //获得连接
  9. Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
  10. //创建存储过程的对象
  11. CallableStatement c=conn.prepareCall("{call getsum(?)}");
  12. //给存储过程的参数设置值
  13. c.setInt(1,100); //将第一个参数的值设置成100
  14. //执行存储过程
  15. c.execute();
  16. conn.close();
  17. }
  18. }

B:带输出参数的

1:返回int

  1. alterprocedure getsum
  2. @n int =0,
  3. @result intoutput
  4. as
  5. declare @sumint
  6. declare @i int
  7. set @sum=0
  8. set @i=0
  9. while @i<=@n begin
  10. set @sum=@sum+@i
  11. set @i=@i+1
  12. end
  13. set @result=@sum

在查询分析器中执行:

  1. declare @myResult int
  2. exec getsum 100,@myResult output
  3. print @myResult

在JAVA中调用:

  1. import java.sql.*;
  2. publicclass ProcedureTest
  3. {
  4. publicstaticvoid main(String args[]) throws Exception
  5. {
  6. //加载驱动
  7. DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
  8. //获得连接
  9. Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
  10. //创建存储过程的对象
  11. CallableStatement c=conn.prepareCall("{call getsum(?,?)}");
  12. //给存储过程的第一个参数设置值
  13. c.setInt(1,100);
  14. //注册存储过程的第二个参数
  15. c.registerOutParameter(2,java.sql.Types.INTEGER);
  16. //执行存储过程
  17. c.execute();
  18. //得到存储过程的输出参数值
  19. System.out.println (c.getInt(2));
  20. conn.close();
  21. }
  22. }

2:返回varchar

存储过程带游标:

在存储过程中带游标 使用游标不停的遍历orderid

  1. createprocedure CursorIntoProcedure
  2. @pname varchar(8000) output
  3. as
  4. --定义游标
  5. declare cur cursorforselect orderid from orders
  6. --定义一个变量来接收游标的值
  7. declare @v varchar(5)
  8. --打开游标
  9. open cur
  10. set @pname=''--给@pname初值
  11. --提取游标的值
  12. fetchnextfrom cur into @v
  13. while @@fetch_status=0
  14. begin
  15. set @pname=@pname+';'+@v
  16. fetchnextfrom cur into @v
  17. end
  18. print @pname
  19. --关闭游标
  20. close cur
  21. --销毁游标
  22. deallocate cur

执行存储过程:

  1. exec CursorIntoProcedure ''

JAVA调用:

  1. import java.sql.*;
  2. publicclass ProcedureTest
  3. {
  4. publicstaticvoid main(String args[]) throws Exception
  5. {
  6. //加载驱动
  7. DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
  8. //获得连接
  9. Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
  10. CallableStatement c=conn.prepareCall("{call CursorIntoProcedure(?)}");
  11. c.registerOutParameter(1,java.sql.Types.VARCHAR);
  12. c.execute();
  13. System.out.println (c.getString(1));
  14. conn.close();
  15. }
  16. }

C:删除数据的存储过程

存储过程:

  1. drop table 学生基本信息表
  2. create table 学生基本信息表
  3. (
  4. StuID int primary key,
  5. StuName varchar(10),
  6. StuAddress varchar(20)
  7. )
  8. insert into 学生基本信息表 values(1,'三毛','wuhan')
  9. insert into 学生基本信息表 values(2,'三毛','wuhan')
  10. create table 学生成绩表
  11. (
  12. StuID int,
  13. Chinese int,
  14. PyhSics int
  15. foreign key(StuID) references 学生基本信息表(StuID)
  16. on delete cascade
  17. on update cascade
  18. )
  19. insert into 学生成绩表 values(1,99,100)
  20. insert into 学生成绩表 values(2,99,100)

创建存储过程:

  1. createprocedure delePro
  2. @StuID int
  3. as
  4. deletefrom 学生基本信息表 where StuID=@StuID
  5. --创建完毕
  6. exec delePro 1 --执行存储过程
  7. --创建存储过程
  8. createprocedure selePro
  9. as
  10. select * from 学生基本信息表
  11. --创建完毕
  12. exec selePro --执行存储过程

在JAVA中调用:

  1. import java.sql.*;
  2. publicclass ProcedureTest
  3. {
  4. publicstaticvoid main(String args[]) throws Exception
  5. {
  6. //加载驱动
  7. DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
  8. //获得连接
  9. Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
  10. //创建存储过程的对象
  11. CallableStatement c=conn.prepareCall("{call delePro(?)}");
  12. c.setInt(1,1);
  13. c.execute();
  14. c=conn.prepareCall("{call selePro}");
  15. ResultSet rs=c.executeQuery();
  16. while(rs.next())
  17. {
  18. String Stu=rs.getString("StuID");
  19. String name=rs.getString("StuName");
  20. String add=rs.getString("StuAddress");
  21. System.out.println ("学号:"+" "+"姓名:"+" "+"地址");
  22. System.out.println (Stu+" "+name+" "+add);
  23. }
  24. c.close();
  25. }
  26. }

D:修改数据的存储过程

创建存储过程:

  1. createprocedure ModPro
  2. @StuID int,
  3. @StuName varchar(10)
  4. as
  5. update 学生基本信息表 set StuName=@StuName where StuID=@StuID

执行存储过程:

  1. exec ModPro 2,'四毛'

JAVA调用存储过程:

  1. import java.sql.*;
  2. publicclass ProcedureTest
  3. {
  4. publicstaticvoid main(String args[]) throws Exception
  5. {
  6. //加载驱动
  7. DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
  8. //获得连接
  9. Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
  10. //创建存储过程的对象
  11. CallableStatement c=conn.prepareCall("{call ModPro(?,?)}");
  12. c.setInt(1,2);
  13. c.setString(2,"美女");
  14. c.execute();
  15. c=conn.prepareCall("{call selePro}");
  16. ResultSet rs=c.executeQuery();
  17. while(rs.next())
  18. {
  19. String Stu=rs.getString("StuID");
  20. String name=rs.getString("StuName");
  21. String add=rs.getString("StuAddress");
  22. System.out.println ("学号:"+" "+"姓名:"+" "+"地址");
  23. System.out.println (Stu+" "+name+" "+add);
  24. }
  25. c.close();
  26. }
  27. }

E:查询数据的存储过程(模糊查询)

存储过程:

  1. createprocedure FindCusts
  2. @cust varchar(10)
  3. as
  4. select customerid from orders where customerid
  5. like'%'+@cust+'%'

执行:

  1. execute FindCusts 'alfki'

在JAVA中调用:

  1. import java.sql.*;
  2. publicclass ProcedureTest
  3. {
  4. publicstaticvoid main(String args[]) throws Exception
  5. {
  6. //加载驱动
  7. DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
  8. //获得连接
  9. Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
  10. //创建存储过程的对象
  11. CallableStatement c=conn.prepareCall("{call FindCusts(?)}");
  12. c.setString(1,"Tom");
  13. ResultSet rs=c.executeQuery();
  14. while(rs.next())
  15. {
  16. String cust=rs.getString("customerid");
  17. System.out.println (cust);
  18. }
  19. c.close();
  20. }
  21. }

F:增加数据的存储过程

存储过程:

  1. createprocedure InsertPro
  2. @StuID int,
  3. @StuName varchar(10),
  4. @StuAddress varchar(20)
  5. as
  6. insertinto 学生基本信息表 values(@StuID,@StuName,@StuAddress)

调用存储过程:

  1. exec InsertPro 5,'555','555'

在JAVA中执行:

  1. import java.sql.*;
  2. public class ProcedureTest
  3. {
  4. publicstatic void main(String args[]) throws Exception
  5. {
  6. //加载驱动
  7. DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
  8. //获得连接
  9. Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
  10. //创建存储过程的对象
  11. CallableStatement c=conn.prepareCall("{call InsertPro(?,?,?)}");
  12. c.setInt(1,6);
  13. c.setString(2,"Liu");
  14. c.setString(3,"wuhan");
  15. c.execute();
  16. c=conn.prepareCall("{call selePro}");
  17. ResultSet rs=c.executeQuery();
  18. while(rs.next())
  19. {
  20. String stuid=rs.getString("StuID");
  21. String name=rs.getString("StuName");
  22. String address=rs.getString("StuAddress");
  23. System.out.println (stuid+" "+name+" "+address);
  24. }
  25. c.close();
  26. }
  27. }

G:在JAVA中创建存储过程 并且在JAVA中直接调用

  1. import java.sql.*;
  2. publicclass ProcedureTest
  3. {
  4. publicstaticvoid main(String args[]) throws Exception
  5. {
  6. //加载驱动
  7. DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
  8. //获得连接
  9. Connection conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
  10. Statement stmt=conn.createStatement();
  11. //在JAVA中创建存储过程
  12. stmt.executeUpdate("create procedure OOP as select * from 学生成绩表");
  13. CallableStatement c=conn.prepareCall("{call OOP}");
  14. ResultSet rs=c.executeQuery();
  15. while(rs.next())
  16. {
  17. String chinese=rs.getString("Chinese");
  18. System.out.println (chinese);
  19. }
  20. conn.close();
  21. }
  22. }
posted on 2013-02-19 16:08  Eleven_Niu  阅读(242)  评论(0编辑  收藏  举报