JDBC公共操作方法(四):【查询】、【增、删、改】和【调用存储过程】的公共方法 03...

 

/**
	 * @Title: callProcedure
	 * @Description: 执行存储过程的公共方法
	 * @param sql
	 *            : 调用存储过程的语句
	 * @param params
	 *            : 条件
	 * @return
	 * @author
	 * @date 2011-12-29
	 */
	public String[] callProcedure(String sql, String[][] params) {

		String[] result = null;

		if (null == sql || "".equals(sql.trim())) {
			LOG.error("The sql is null, return.");
			return result;
		}

		// 获取数据库连接
		Connection connection = JDBCUtil.getInstance().getConnection();

		if (null == connection) {
			LOG.error("The database connction is null, return.");
			return result;
		}

		CallableStatement cs = null;
		int len = params == null ? 0 : params.length;
		int[] index = new int[len];

		try {
			cs = connection.prepareCall(sql);
			int position = 0;

			// 设置入参/出参
			for (int i = 0; i < len; i++) {
				if ("IN".equalsIgnoreCase(params[i][0])) {
					cs.setString(i + 1, params[i][1]);
				} else if ("OUT".equalsIgnoreCase(params[i][0])) {
					cs.registerOutParameter(i + 1,
							Integer.valueOf(params[i][1]));
					index[position] = i + 1;
					position++;
				}
			}
			cs.execute();

			result = new String[position];

			// 处理返回值
			for (int i = 0; i < index.length; i++) {
				if (0 != index[i]) {
					result[i] = String.valueOf(cs.getObject(index[i]));
				}
			}

		} catch (SQLException e) {
			LOG.error("JDBCCore:=>callProcedure : SQLException!", e);
		} catch (Exception e) {
			LOG.error("JDBCCore:=>callProcedure : Exception!", e);
		} finally {
			JDBCUtil.close(connection, null, cs);
		}
		return result;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值