JDBC的储存过程 剖解

1.1.在 java 中使用 jdbc--cst 就是 为了 大大 简化每一步单独的 操作,快速 便捷。

 数据库,首先 先看 存储过程的 编写,

         带有 参数的,没有 参数的,带返回值, 不带

    --  CREATE proc proc_name  ---然后就是 调用

--编写带有返回值的存储过程
if exists(select * from sysobjects where name='proc_getRows')  储存过程的 名字 需要 打上 引号
drop proc proc_getRows
go


create proc proc_getRows
as
begin
declare @cnt int
select @cnt=COUNT(1) from goods 总共 有多少列
return @cnt                  返回的是 一个 数值
end

go

》》 这是在 数据库 中的执行过程
declare @cnt int
exec @cnt=proc_getRows
print @cnt
go

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

         如果是在 java中 调用这个 东西,就是 用到 cst

1.Class.forName(com.........);

2.Connection sqlcon=DriverM.getConnection(名字,密码,地址);

                                                           地址 包含着  数据库的 数据库类型,IP地址,端口 数据库名字

3.  String sql=" {?=call proc proc_getcows()}";

4.Callab.. cst=sqlcpon.Preparecall.(sql);

带有 返回值和 输出参数的 都需要 先 注册参数的 类型

  5. cst.registerOutParameter(1, java.sql.Types.INTEGER);
6. 执行 语句
cst.execute();
7.获得 返回值
int count=cst.getInt(1);

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

带有 输出 和输入参数的  调用

--编写带有输入输出参数的存储过程
--根据商品编号获取商品的单价和数量
if exists(select * from sysobjects where name='proc_getGoodsInfo')
drop proc proc_getGoodsInfo
go
create proc proc_getGoodsInfo
(
                @id int,   两个 输出 和一个 输入
@price money output,
@count int output

)
as
begin
select @price=gprice,@count=icount  from goods where id=@id
end
go

--调用带有输入输出参数的存储过程
declare @num1 int =10
declare @num2 int 
declare @res int 
exec proc_Add @num1,@num2 output,@res output
print ' 为:'+convert(varchar(20),@res)
go

________________________________________________________________________

java中调用 就是 

String sql="{call pro_name(?,?,?)}";

cst=sqlcon.prepareCall(sql);

                 cst.registerOutParameter(2, java.sql.Types.DOUBLE);
cst.registerOutParameter(3, java.sql.Types.INTEGER);
cst.setInt(1, 11);第一个 需要 赋值


cst.execute();
执行之后 所有的 值  都会 储存在 CST 之中 ,
double price=cst.getDouble(2);
int count=cst.getInt(3);

System.out.println("商品单价为:"+price);
System.out.println("商品数量为:"+count);

————————————————————————————————

只带有 输入参数的, 很可能就是  调用的  一个

                        插入 或者 删除 或者 更改 的 操作

String sql="{call proc_insertGoods(?,?,?,?)}";

try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

Connection sqlcon = DriverManager.getConnection(url, user, password);
CallableStatement cst = sqlcon.prepareCall(sql);
cst.setString(1, "海飞丝");
cst.setString(2, "瓶");
cst.setDouble(3, 21.5);
cst.setInt(4, 100);


int line = cst.executeUpdate();
if(line>0){
System.out.println("执行成功!");
}else{
System.out.println("执行失败!");
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

8. 如果 储存 过程 执行之后 

cst 之中的 是一个 结果集 

那么 还是需要 用到  RST

》》》》》》》》》》》》》》》》》》》》》》》

  9.  不管  储存 过程 有没有 参数

java 中的 String sql ="{ call proc_name()}";

create proc proc_selectGoods
as
begin
select * from goods
end
go

>>

 java 中 就是 

   cst.execute();

   rst=cst.getReuslt();

  然后就是 对 rst 进行 操作



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值