[转载]JDBC调用存储过程_Andy_pany_新浪博客

原文地址:JDBC调用存储过程 作者:樊华春

参考网站

(http://blog.csdn.net/gaohuanjie/article/details/34422903)

JDBC调用存储过程

JDBC API中提供了调用存储过程的方法,通过CallableStatement对象进行操作。CallableStatement对象位于java.sql包中,它继承于Statement对象,主要用于执行数据库中定义的存储过程

JDBC 中的 CallableStatement 对象为所有的关系数据库管理系统 (RDBMS: Relational Database Management System) 提供了一种标准形式调用存储过程的方法。对存储过程的调用有两种形式:带结果参数和不带结果参数。结果参数是一种输出参数,是存储过程的返回值。两种形式都可带有数量可变的输入(IN 参数)、输出(OUT 参数)或输入和输出(INOUT 参数)的参数。

JDBC 中调用存储过程的语法为:{call procedure_name[(? ?...)]};返回结果参数的存储过程的语法为:{? = call procedure_name[(? ?...)]};不带参数的存储过程的语法为:{call procedure_name}。其中,问号代表参数,方括号表示其间的内容是可选

 

使用CallableStatement 对象调用存储过程的过程如下:

使用Connection.prepareCall 方法创建一个CallableStatement 对象。 
使用 CallableStatement.setXXX 方法给输入参数(IN)赋值。 
使用 CallableStatement.registerOutParameter 方法来指明哪些参数只做输出参数(OUT),哪些是输入输出参数(INOUT 

 

实例

1、没有任何输入和输出参数的存储过程

1.  package com.ghj.packageoftest;  

2.    

3.  import java.sql.CallableStatement;  

4.  import java.sql.Connection;  

5.  import java.sql.ResultSet;  

6.  import java.sql.SQLException;  

7.    

8.  import com.ghj.packageoftool.LinkDB;  

9.    

10.  

19.  

20.  

25.public class NoParam {  

26.    public static void main(String args[]) throws SQLException {  

27.        Connection connection = LinkDB.getMySqlConnection();  

28.        String proStr = "{call noParam}";  

29.        CallableStatement callableStatement = connection.prepareCall(proStr);  

30.        callableStatement.execute();  

31.        ResultSet resultSet = callableStatement.getResultSet();  

32.        while (resultSet.next()) {  

33.            System.out.println("产品的平均价格是:" + resultSet.getDouble("priceAvg") + "");  

34.        }  

35.        LinkDB.close(connection, callableStatement, resultSet);  

36.    }  

37.}  

 2、只有两个输入参数的存储过程

[java] view plain copy

1.  package com.ghj.packageoftest;  

2.    

3.  import java.sql.CallableStatement;  

4.  import java.sql.Connection;  

5.  import java.sql.ResultSet;  

6.  import java.sql.SQLException;  

7.    

8.  import com.ghj.packageoftool.LinkDB;  

9.    

10.  

19.  

20.  

25.public class InTwoParam {  

26.    public static void main(String args[]) throws SQLException {  

27.        Connection connection = LinkDB.getMySqlConnection();  

28.        String procStr = "{call inTwoParam(?,?)}";  

29.        CallableStatement callableStatement = connection.prepareCall(procStr);  

30.        callableStatement.setString(1"");  

31.        callableStatement.setDouble(288.88);//DECIMAL类型的属性设值要使用setDouble方法。  

32.        callableStatement.execute();  

33.        ResultSet resultSet = callableStatement.getResultSet();  

34.        System.out.println("名称包含字且价格小于88.88元的水果有:");  

35.        while (resultSet.next()) {  

36.            System.err.println("名称:" + resultSet.getString("name") +"、价格:" + resultSet.getDouble("price") + ""+"、产地:" + resultSet.getString("address"));  

37.        }  

38.        LinkDB.close(connection, callableStatement, resultSet);  

39.    }  

40.}  

 

3.只有两个输出参数的存储过程

[java] view plain copy

1.  package com.ghj.packageoftest;  

2.    

3.  import java.sql.CallableStatement;  

4.  import java.sql.Connection;  

5.  import java.sql.SQLException;  

6.  import java.sql.Types;  

7.    

8.  import com.ghj.packageoftool.LinkDB;  

9.    

10.  

21.  

22.  

27.public class OutTwoParam {  

28.    public static void main(String args[]) throws SQLException {  

29.        Connection connection = LinkDB.getMySqlConnection();  

30.        String proStr = "{call outTwoParam(?,?)}";  

31.        CallableStatement callableStatement = connection.prepareCall(proStr);  

32.        callableStatement.registerOutParameter(1, Types.VARCHAR);  

33.        callableStatement.registerOutParameter(2, Types.DECIMAL);  

34.        callableStatement.execute();  

35.        String fruitName = callableStatement.getString(1);  

36.        double fruitPrice = callableStatement.getDouble(2);// 获取DECIMAL类型的属性要使用getDouble方法。  

37.        System.out.println("水果名称:" + fruitName +"、水果价格:" + fruitPrice + "");  

38.        LinkDB.close(connection, callableStatement, null);  

39.    }  

40.}  

 

 4、含有一个输入参数和一个输出参数的存储过程

[java] view plain copy

1.  package com.ghj.packageoftest;  

2.    

3.  import java.sql.CallableStatement;  

4.  import java.sql.Connection;  

5.  import java.sql.SQLException;  

6.  import java.sql.Types;  

7.    

8.  import com.ghj.packageoftool.LinkDB;  

9.    

10.  

19.   

20.  

25.public class InOneParamAndOutOneParam {  

26.    public static  void main(String args[]) throws SQLException {  

27.        Connection connection=LinkDB.getMySqlConnection();  

28.        CallableStatement callableStatement=null;  

29.        String procStr="{call inOneParamAndOutOneParam(?,?)}";  

30.        callableStatement=connection.prepareCall(procStr);  

31.        String fruitName = "莲雾";  

32.        callableStatement.setString(1, fruitName);  

33.        callableStatement.registerOutParameter(2, Types.DECIMAL);  

34.        callableStatement.execute();  

35.        double fruitPrice=callableStatement.getDouble(2);//获取DECIMAL类型的属性要使用getDouble方法。  

36.        System.out.println(fruitName+"的价格为:"+fruitPrice+"");  

37.        LinkDB.close(connection, callableStatement, null);  

38.    }  

39.}  

 

5、输入参数即输出参数的存储过程

[java] view plain copy

1.  package com.ghj.packageoftest;  

2.    

3.  import java.sql.*;  

4.    

5.  import com.ghj.packageoftool.LinkDB;  

6.    

7.    

26.  

27.  

32.public class InOneParamISOutOneParam {  

33.    public static void main(String args[]) throws SQLException {  

34.        Connection con = LinkDB.getMySqlConnection();  

35.        CallableStatement callableStatement = null;  

36.        String procStr = "{call inOneParamISOutOneParam(?)}";  

37.        callableStatement = con.prepareCall(procStr);  

38.        callableStatement.setString(1"");  

39.        callableStatement.registerOutParameter(1, Types.VARCHAR);  

40.        callableStatement.execute();  

41.        String fruitName = callableStatement.getString(1);  

42.        System.out.println("表中水果名称含有字的一中水果的名称是:" + fruitName);  

43.        LinkDB.close(con, callableStatement, null);  

44.    }  

45.}  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值