【www.gdgbn.com--js教程】

java 调用存储过程
1、使用不带参数的存储过程
          使用 jdbc 驱动程序调用不带参数的存储过程时,必须使用 call sql 转义序列。不带参数的 call 转义序列的语法如下所示:

 {call procedure-name} 作为实例,在 sql server 2005 adventureworks 示例数据库教程中创建以下存储过程:

以下是引用片段:

create procedure getcontactformalnames
  as
  begin
   select top 10 title + " " + firstname + " " + lastname as formalname
   from person.contact
  end


此存储过程返回单个结果集,其中包含一列数据(由 person.contact 表中前十个联系人的称呼、名称和姓氏组成)。

  在下面的实例中,将向此函数传递 adventureworks 示例数据库的打开连接,然后使用 executequery 方法调用 getcontactformalnames 存储过程。

以下是引用片段:
  

public static void executesprocnoparams(connection con) ...{
   try ...{
   statement stmt = con.createstatement();
  resultset rs = stmt.executequery("{call dbo.getcontactformalnames}");
  
   while (rs.next()) ...{
 system.out.println(rs.getstring("formalname"));
  }
  rs.close();
  stmt.close();
  }
  catch (exception e) ...{
  e.printstacktrace();
  }
  }

2、使用带有输入参数的存储过程

     使用 jdbc 驱动程序调用带参数的存储过程时,必须结合 sqlserverconnection 类的 preparecall 方法使用 call sql 转义序列。带有 in 参数的 call 转义序列的语法如下所示:{call procedure-name[([parameter][,[parameter]]...)]} 构造 call 转义序列时,请使用 ?(问号)字符来指定 in 参数。此字符充当要传递给该存储过程的参数值的占位符。可以使用 sqlserverpreparedstatement 类的 setter 方法之一为参数指定值。可使用的 setter 方法由 in 参数的数据类型决定。

  向 setter 方法传递值时,不仅需要指定要在参数中使用的实际值,还必须指定参数在存储过程中的序数位置。例如,如果存储过程包含单个 in 参数,则其序数值为 1。如果存储过程包含两个参数,则第一个序数值为 1,第二个序数值为 2。作为如何调用包含 in 参数的存储过程的实例,使用 sql server 2005 adventureworks 示例数据库中的 uspgetemployeemanagers 存储过程。此存储过程接受名为 employeeid 的单个输入参数(它是一个整数值),然后基于指定的 employeeid 返回雇员及其经理的递归列表。下面是调用此存储过程的 java 代码:

 

     public static void executesprocinparams(connection con) ...{
   try ...{
   preparedstatement ps教程tmt = con.preparestatement("{call dbo.uspgetemployeemanagers(?)}");
   pstmt.setint(1, 50);
   resultset rs = pstmt.executequery();
   while (rs.next()) ...{
   system.out.println("employee:");
   system.out.println(rs.getstring("lastname") + ", " + rs.getstring("firstname"));
   system.out.println("manager:");
   system.out.println(rs.getstring("managerlastname") + ", " + rs.getstring("managerfirstname"));
   system.out.println();
   }
   rs.close();
   pstmt.close();
   }
   catch (exception e) ...{
   e.printstacktrace();
   }
  } 

 

3、使用带有输出参数的存储过程

     使用 jdbc 驱动程序调用此类存储过程时,必须结合 sqlserverconnection 类的 preparecall 方法使用 call sql 转义序列。带有 out 参数的 call 转义序列的语法如下所示:{call procedure-name[([parameter][,[parameter]]...)]} 构造 call 转义序列时,请使用 ?(问号)字符来指定 out 参数。此字符充当要从该存储过程返回的参数值的占位符。要为 out 参数指定值,必须在运行存储过程前使用 sqlservercallablestatement 类的 registeroutparameter 方法指定各参数的数据类型。

  使用 registeroutparameter 方法为 out 参数指定的值必须是 java.sql.types 所包含的 jdbc 数据类型之一,而它又被映射成本地 sql server 数据类型之一。有关 jdbc 和 sql server 数据类型的详细信息,请参阅了解 jdbc 驱动程序数据类型。

  当您对于 out 参数向 registeroutparameter 方法传递一个值时,不仅必须指定要用于此参数的数据类型,而且必须在存储过程中指定此参数的序号位置或此参数的名称。例如,如果存储过程包含单个 out 参数,则其序数值为 1;如果存储过程包含两个参数,则第一个序数值为 1,第二个序数值为 2。

  作为实例,在 sql server 2005 adventureworks 示例数据库中创建以下存储过程: 根据指定的整数 in 参数 (employeeid),该存储过程也返回单个整数 out 参数 (managerid)。根据 humanresources.employee 表中包含的 employeeid,out 参数中返回的值为 managerid。

  在下面的实例中,将向此函数传递 adventureworks 示例数据库的打开连接,然后使用 execute 方法调用 getimmediatemanager 存储过程:

 

      public static void executestoredprocedure(connection con) ...{
   try ...{
   callablestatement cstmt = con.preparecall("{call dbo.getimmediatemanager(?, ?)}");
   cstmt.setint(1, 5);
   cstmt.registeroutparameter(2, java.sql.types.integer);
   cstmt.execute();
   system.out.println("manager id: " + cstmt.getint(2));
   }
   catch (exception e) ...{
   e.printstacktrace();
   }
  } 

本示例使用序号位置来标识参数。或者,也可以使用参数的名称(而非其序号位置)来标识此参数。下面的代码示例修改了上一个示例,以说明如何在 java 应用程序中使用命名参数。请注意,这些参数名称对应于存储过程的定义中的参数名称: create procedure getimmediatemanager

以下是引用片段:

 @employeeid int,
   @managerid int output
  as
  begin
   select @managerid = managerid
   from humanresources.employee
   where employeeid = @employeeid
  end

  存储过程可能返回更新计数和多个结果集。microsoft sql server 2005 jdbc driver 遵循 jdbc 3.0 规范,此规范规定在检索 out 参数之前应检索多个结果集和更新计数。也就是说,应用程序应先检索所有 resultset 对象和更新计数,然后使用 callablestatement.getter 方法检索 out 参数。否则,当检索 out 参数时,尚未检索的 resultset 对象和更新计数将丢失。

4、使用带有返回状态的存储过程

     使用 jdbc 驱动程序调用这种存储过程时,必须结合 sqlserverconnection 类的 preparecall 方法使用 call sql 转义序列。返回状态参数的 call 转义序列的语法如下所示:
{[?=]call procedure-name[([parameter][,[parameter]]...)]} 构造 call 转义序列时,请使用 ?(问号)字符来指定返回状态参数。此字符充当要从该存储过程返回的参数值的占位符。要为返回状态参数指定值,必须在执行存储过程前使用 sqlservercallablestatement 类的 registeroutparameter 方法指定参数的数据类型。

    此外,向 registeroutparameter 方法传递返回状态参数值时,不仅需要指定要使用的参数的数据类型,还必须指定参数在存储过程中的序数位置。对于返回状态参数,其序数位置始终为 1,这是因为它始终是调用存储过程时的第一个参数。尽管 sqlservercallablestatement 类支持使用参数的名称来指示特定参数,但您只能对返回状态参数使用参数的序号位置编号。

create procedure checkcontactcity
   (@cityname char(50))
  as
  begin
   if ((select count(*)
   from person.address
   where city = @cityname) > 1)
   return 1
  else
   return 0
  end

  该存储过程返回状态值 1 或 0,这取决于是否能在表 person.address 中找到 cityname 参数指定的城市。

在下面的实例中,将向此函数传递 adventureworks 示例数据库的打开连接,然后使用 execute 方法调用 checkcontactcity 存储过程:

public static void executestoredprocedure(connection con) ...{
   try ...{
   callablestatement cstmt = con.preparecall("{? = call dbo.checkcontactcity(?)}");
   cstmt.registeroutparameter(1, java.sql.types.integer);
   cstmt.setstring(2, "atlanta");
   cstmt.execute();
   system.out.println("return status: " + cstmt.getint(1));
   }
   cstmt.close();
   catch (exception e) ...{
   e.printstacktrace();
   }
  }

5、使用带有更新计数的存储过程

    
  使用 sqlservercallablestatement 类构建对存储过程的调用之后,可以使用 execute 或 executeupdate 方法中的任意一个来调用此存储过程。executeupdate 方法将返回一个 int 值,该值包含受此存储过程影响的行数,但 execute 方法不返回此值。如果使用 execute 方法,并且希望获得受影响的行数计数,则可以在运行存储过程后调用

getupdatecount 方法。
create table testtable
   (col1 int identity,
   col2 varchar(50),
   col3 int);
  
  create procedure updatetesttable
   @col2 varchar(50),
   @col3 int
  as
  begin
   update testtable
   set col2 = @col2, col3 = @col3
  end;

在下面的实例中,将向此函数传递 adventureworks 示例数据库的打开连接,并使用 execute 方法调用

updatetesttable 存储过程,然后使用 getupdatecount 方法返回受存储过程影响的行计数。 以下是引用片段:
public static void executeupdatestoredprocedure(connection con) ...{
   try ...{
   callablestatement cstmt = con.preparecall("{call dbo.updatetesttable(?, ?)}");
   cstmt.setstring(1, "a");
   cstmt.setint(2, 100);
   cstmt.execute();
   int count = cstmt.getupdatecount();
   cstmt.close();
  
   system.out.println("rows affected: " + count);
   }
   catch (exception e) ...{
   e.printstacktrace();
   }
  }

本文来源:http://www.gdgbn.com/wangyezhizuo/29512/