package com.syj.jdbc.core;
import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* Title:存储过程回调类
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright: Copyright (c) 2007
* </p>
*
* @author 孙钰佳
* @main sunyujia@yahoo.cn
* @date May 17, 2008 6:23:50 PM
*/
public abstract class ProcedureCallBack {
/**
* jdbc模版 包可见
*/
SimpleJdbcTemplate jt;
/**
* 执行过程的语句 包可见
*/
CallableStatement cstm;
/**
* 0表示过程无返回值,1表示过程有返回值,声明的参数下标需加1 包可见
*/
int startParametersIdx = 0;
/**
* 参数个数(排除返回值参数) 包可见
*/
int parametersCount;
/**
* 注册的out参数(包括返回值参数)
*/
private Map outParameters = new HashMap();
/**
* 参数计数器加1
*/
private void addParametersCount() {
parametersCount++;
};
/**
* 根据用户声明下标取参数实际下标
*
* @param parameterIndex
* @return
*/
private int getParameterIndex(int parameterIndex) {
return parameterIndex + startParametersIdx;
}
/**
* Registers the OUT parameter in ordinal position
* <code>parameterIndex</code> to the JDBC type <code>sqlType</code>.
* All OUT parameters must be registered before a stored procedure is
* executed.
* <p>
* The JDBC type specified by <code>sqlType</code> for an OUT parameter
* determines the Java type that must be used in the <code>get</code>
* method to read the value of that parameter.
* <p>
* If the JDBC type expected to be returned to this output parameter is
* specific to this particular database, <code>sqlType</code> should be
* <code>java.sql.Types.OTHER</code>. The method {@link #getObject}
* retrieves the value.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, and so on
* @param sqlType
* the JDBC type code defined by <code>java.sql.Types</code>.
* If the parameter is of JDBC type <code>NUMERIC</code> or
* <code>DECIMAL</code>, the version of
* <code>registerOutParameter</code> that accepts a scale value
* should be used.
*
* @exception SQLException
* if a database access error occurs
* @see Types
*/
public void registerOutParameter(int parameterIndex, int sqlType)
throws SQLException {
if (cstm != null) {
parameterIndex = getParameterIndex(parameterIndex);
cstm.registerOutParameter(parameterIndex, sqlType);
outParameters.put(String.valueOf(parameterIndex), new Integer(
sqlType));
}
addParametersCount();
};
/**
* 添加一个oracle游标,mssql环境下执行无影响相当于空调用
*
* @param parameterIndex
* @throws SQLException
*/
public void addOracleCursor(int parameterIndex) throws SQLException {
if (!jt.isOracle())
return;
if (cstm != null) {
parameterIndex = getParameterIndex(parameterIndex);
cstm.registerOutParameter(parameterIndex,
oracle.jdbc.OracleTypes.CURSOR);
outParameters.put(String.valueOf(parameterIndex), new Integer(
oracle.jdbc.OracleTypes.CURSOR));
}
addParametersCount();
};
/**
* Registers the parameter in ordinal position <code>parameterIndex</code>
* to be of JDBC type <code>sqlType</code>. This method must be called
* before a stored procedure is executed.
* <p>
* The JDBC type specified by <code>sqlType</code> for an OUT parameter
* determines the Java type that must be used in the <code>get</code>
* method to read the value of that parameter.
* <p>
* This version of <code>registerOutParameter</code> should be used when
* the parameter is of JDBC type <code>NUMERIC</code> or
* <code>DECIMAL</code>.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, and so on
* @param sqlType
* the SQL type code defined by <code>java.sql.Types</code>.
* @param scale
* the desired number of digits to the right of the decimal
* point. It must be greater than or equal to zero.
* @exception SQLException
* if a database access error occurs
* @see Types
*/
public void registerOutParameter(int parameterIndex, int sqlType, int scale)
throws SQLException {
if (cstm != null)
cstm.registerOutParameter(getParameterIndex(parameterIndex),
sqlType, scale);
outParameters.put(new Integer(parameterIndex), new Integer(sqlType));
addParametersCount();
};
/**
* 回调方法,内部类实现
*
* @throws SQLException
*/
public void registerParameter() throws SQLException {
}
/**
* 回调方法,内部类实现 结果集行对象的封装
*
* @param rs
* @param rsIndex
* @return
* @throws SQLException
*/
public Object mapRow(ResultSet rs, int rsIndex) throws SQLException {
return null;
}
/**
* 取声明的所有out参数
*
* @return
*/
public Map getOutParameters() {
return outParameters;
}
/**
* Sets the designated parameter to SQL <code>NULL</code>.
*
* <P>
* <B>Note:</B> You must specify the parameter's SQL type.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, ...
* @param sqlType
* the SQL type code defined in <code>java.sql.Types</code>
* @exception SQLException
* if a database access error occurs
*/
public void setNull(int parameterIndex, int sqlType) throws SQLException {
if (cstm != null)
cstm.setNull(getParameterIndex(parameterIndex), sqlType);
addParametersCount();
}
/**
* Sets the designated parameter to the given Java <code>boolean</code>
* value. The driver converts this to an SQL <code>BIT</code> value when
* it sends it to the database.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, ...
* @param x
* the parameter value
* @exception SQLException
* if a database access error occurs
*/
public void setBoolean(int parameterIndex, boolean x) throws SQLException {
if (cstm != null)
cstm.setBoolean(getParameterIndex(parameterIndex), x);
addParametersCount();
};
/**
* Sets the designated parameter to the given Java <code>byte</code>
* value. The driver converts this to an SQL <code>TINYINT</code> value
* when it sends it to the database.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, ...
* @param x
* the parameter value
* @exception SQLException
* if a database access error occurs
*/
public void setByte(int parameterIndex, byte x) throws SQLException {
if (cstm != null)
cstm.setByte(getParameterIndex(parameterIndex), x);
addParametersCount();
};
/**
* Sets the designated parameter to the given Java <code>short</code>
* value. The driver converts this to an SQL <code>SMALLINT</code> value
* when it sends it to the database.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, ...
* @param x
* the parameter value
* @exception SQLException
* if a database access error occurs
*/
public void setShort(int parameterIndex, short x) throws SQLException {
if (cstm != null)
cstm.setShort(getParameterIndex(parameterIndex), x);
addParametersCount();
};
/**
* Sets the designated parameter to the given Java <code>int</code> value.
* The driver converts this to an SQL <code>INTEGER</code> value when it
* sends it to the database.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, ...
*
没有合适的资源?快使用搜索试试~ 我知道了~
针对存储过程通用的jdbc封装
共43个文件
class:19个
java:12个
jar:5个
4星 · 超过85%的资源 需积分: 17 316 下载量 184 浏览量
2008-05-18
18:47:29
上传
评论 4
收藏 2.57MB RAR 举报
温馨提示
针对java调用存储过程写的通用封装,oracle,mssql测试通过,附带测试脚本.以前写过一个,感觉不是很好,这个周末就重写了一个,大家多多提意见,是eclipse工程直接跑junit就行了,所有的东西都在里面.除了存储过程也可以执行简单的sql.
资源推荐
资源详情
资源评论
收起资源包目录
jdbc.rar (43个子文件)
jdbc
.project 380B
bin
com
syj
jdbc
oracle
SqlTest$1.class 775B
ProcedureTest.class 498B
ProcedureTest.sql 1KB
SqlTest.class 954B
CommonProcedureTest$1.class 1012B
TestUtil.class 2KB
CommonProcedureTest$3.class 1004B
CommonProcedureTest$2.class 1003B
CommonProcedureTest.class 2KB
core
ProcedureResult.class 1KB
SimpleJdbcTemplate$1.class 1KB
SimpleJdbcTemplate.class 9KB
RowMapper.class 228B
ProcedureCallBack.class 7KB
DataAccessException.class 750B
mssql
SqlTest$1.class 771B
ProcedureTest.class 495B
ProcedureTest.sql 434B
SqlTest.class 928B
CommonProcedureTest$4.class 493B
util
JdbcUtils.class 8KB
.settings
org.eclipse.jdt.core.prefs 634B
src
main
test
com
syj
jdbc
TestUtil.java 2KB
CommonProcedureTest.java 2KB
oracle
ProcedureTest.java 530B
ProcedureTest.sql 1KB
SqlTest.java 852B
mssql
ProcedureTest.java 528B
ProcedureTest.sql 434B
SqlTest.java 830B
java
com
syj
jdbc
core
ProcedureResult.java 792B
SimpleJdbcTemplate.java 8KB
ProcedureCallBack.java 30KB
RowMapper.java 1KB
util
JdbcUtils.java 11KB
DataAccessException.java 600B
.classpath 830B
lib
commons-logging-1.1.jar 52KB
ojdbc14_g.jar 1.84MB
xstream-1.2.1.jar 341KB
mssql_All.jar 403KB
junit-3.8.1.jar 118KB
共 43 条
- 1
sunyujia
- 粉丝: 1480
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页