posts - 28,  comments - 15,  trackbacks - 0
   在说明之前,先做一下准备工作.下面的说明主要针对SQL Server数据库,另外要保证jtds驱动程序已经测试通过.

  1. 调用带有输入和输出参数的存储过程

         

假设已经存在一个存储过程user_registry_stat,其代码如下:

               
CREATE PROCEDURE user_registry_stat(@stylebook int,@result int output  )AS
                    BEGIN
                    /*不返回计数*/
                    SET NOCOUNT ON

                    DECLARE @total_amount int
                    SET @total_amount = (SELECT count(id) as amount from CP_USER)

                    IF(@total_amount>@stylebook)
                    BEGIN
                          @result = 1
                    END
                    ELSE
                    BEGIN
                          @result =2
                    END
               
               JAVA执行代码如下:
               public static void executeStoredProcedure(Connection con) {
                     try {
                           CallableStatement cstmt = con.prepareCall("{call user_registry_s(?,?)}");
                            cstmt.seInt(1, 50);
                           cstmt.execute();
                           System.out.println("RETURN STATUS: " + cstmt.getInt(2));
                           cstmt.close();
                      }catch (Exception e) {
                           e.printStackTrace();
                     }
                  }
2.   调用带有返回状态的存储过程
            假设已经存在一个存储过程user_registry_stat,其代码如下:
                  CREATE PROCEDURE user_registry_stat(@stylebook int)AS
                    BEGIN
                    /*不返回计数*/
                    SET NOCOUNT ON

                    DECLARE @total_amount int
                    SET @total_amount = (SELECT count(id) as amount from CP_USER)

                    IF(@total_amount>@stylebook)
                        return 1
                    ELSE
                        return 2
            
            java执行代码如下:
            public static void executeStoredProcedure(Connection con) {
               try {
                  CallableStatement cstmt = con.prepareCall("{? = call user_registry_stat(?)}");
                  cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
                  cstmt.seInt(2, 50);
                  cstmt.execute();
                  System.out.println("RETURN STATUS: " + cstmt.getInt(1));
            
                  cstmt.close();
            }catch (Exception e) {
               e.printStackTrace();
            }
         }
参考: http://msdn2.microsoft.com/zh-cn/library/ms378371.aspx

posted on 2006-12-11 18:42 zhangxl 阅读(467) 评论(0)  编辑  收藏 所属分类: JDBC
<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(1)

随笔分类(17)

随笔档案(28)

文章分类(30)

文章档案(30)

相册

收藏夹(2)

hibernate

java基础

mysql

xml

关注

压力测试

算法

最新随笔

搜索

  •  

积分与排名

  • 积分 - 95498
  • 排名 - 602

最新评论

阅读排行榜

评论排行榜