java 里 存储过程的使用

package poceduretest;

import java.sql.*;

class MainTest
{
public static void main(String[] args)
{
java.sql.Connection con = ConnectionManager.getConnection(); //先获得连接
/**
* 基本存储过程调用
*/
try
{
java.sql.CallableStatement cs = con.prepareCall("{call sp_num}"); //传入的参数是call 和 存储过程名
cs.execute();
if(cs.execute())
{

}else
{
java.sql.ResultSet r = cs.getResultSet();
// 输出记录集里的数据
while (r.next())
{
System.out.print(r.getString(1));
System.out.print("\t" + r.getString(2));
System.out.print("\t" + r.getInt(3));
System.out.println("\t" + r.getInt(4));
}
}
}
catch (SQLException ex)
{
}



/**
* 调用有参数的存储过程1
*/
try
{
int x = 15;
java.sql.CallableStatement cs = con.prepareCall("{call sp_num1(?)}"); //此内的?与存储过程里的输出和输入参数一致
cs.setInt(1, x);

// 执行存储过程
cs.execute();

java.sql.ResultSet r = cs.getResultSet();
// 输出记录集里的数据
while (r.next())
{
System.out.print(r.getString(1));
System.out.println("\t" + r.getString(2));
}
}
catch (SQLException ex)
{
}

/**
* 调用有参数的存储过程2
*/




try
{
String name = "business";
java.sql.CallableStatement cs = con.prepareCall("{call sp_num2(?)}");
cs.setString(1, name);

// 执行存储过程
cs.execute();

java.sql.ResultSet r = cs.getResultSet();
// 输出记录集里的数据
while (r.next())
{
System.out.print(r.getString(1));
System.out.println("\t" + r.getString(2));
}
}
catch (SQLException ex)
{
}



/**
* 带有输出参数的存储过程
*/
try
{
String name = "business";
java.sql.CallableStatement cs = con.prepareCall("{call sp_num5(?,?)}");

cs.registerOutParameter(1, java.sql.Types.INTEGER);
cs.setInt(2, 5);


cs.execute();


System.out.println(cs.getInt(1));
}
catch (SQLException ex)
{
}

/**
* 返回多个结果集的存储过程
*/




try
{
java.sql.CallableStatement cs = con.prepareCall("{call sp_num6}");
cs.execute();

do
{
java.sql.ResultSet t = cs.getResultSet();
while (t.next())
{
System.out.print(t.getString(1));
System.out.println("\t" +t.getString(2));
}

}while(cs.getMoreResults());
}
catch (SQLException ex)
{
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值