use of java.sql.CallableStatement in project mybatis-3 by mybatis.
the class CallableStatementHandler method update.
@Override
public int update(Statement statement) throws SQLException {
CallableStatement cs = (CallableStatement) statement;
cs.execute();
int rows = cs.getUpdateCount();
Object parameterObject = boundSql.getParameterObject();
KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
keyGenerator.processAfter(executor, mappedStatement, cs, parameterObject);
resultSetHandler.handleOutputParameters(cs);
return rows;
}
use of java.sql.CallableStatement in project mybatis-3 by mybatis.
the class CallableStatementHandler method queryCursor.
@Override
public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
CallableStatement cs = (CallableStatement) statement;
cs.execute();
Cursor<E> resultList = resultSetHandler.<E>handleCursorResultSets(cs);
resultSetHandler.handleOutputParameters(cs);
return resultList;
}
use of java.sql.CallableStatement in project mybatis-3 by mybatis.
the class CallableStatementHandler method query.
@Override
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
CallableStatement cs = (CallableStatement) statement;
cs.execute();
List<E> resultList = resultSetHandler.<E>handleResultSets(cs);
resultSetHandler.handleOutputParameters(cs);
return resultList;
}
use of java.sql.CallableStatement in project pinpoint by naver.
the class MariaDB_IT_Base method executeCallableStatement.
protected final void executeCallableStatement() throws Exception {
final int expectedResultSize = 1;
final int expectedTotalCount = 3;
final int expectedMatchingId = 2;
final String outputParamCountName = "outputParamCount";
Connection conn = null;
try {
conn = DriverManager.getConnection(JDBC_URL, "root", null);
CallableStatement cs = null;
try {
cs = conn.prepareCall(CALLABLE_STATEMENT_QUERY);
cs.setString(1, CALLABLE_STATEMENT_INPUT_PARAM);
cs.registerOutParameter(2, CALLABLE_STATMENT_OUTPUT_PARAM_TYPE);
ResultSet rs = null;
try {
rs = cs.executeQuery();
int resultCount = 0;
while (rs.next()) {
++resultCount;
if (resultCount > expectedResultSize) {
fail();
}
assertEquals(expectedMatchingId, rs.getInt(1));
assertEquals(CALLABLE_STATEMENT_INPUT_PARAM, rs.getString(2));
}
assertEquals(expectedResultSize, resultCount);
} finally {
closeResultSet(rs);
}
final int totalCount = cs.getInt(outputParamCountName);
assertEquals(expectedTotalCount, totalCount);
} finally {
closeStatement(cs);
}
} finally {
closeConnection(conn);
}
}
use of java.sql.CallableStatement in project nutz by nutzam.
the class NutDaoExecutor method _runExec.
// 执行存储过程,简单实现
protected void _runExec(Connection conn, DaoStatement st) throws SQLException {
if (st.getContext().getPager() != null) {
throw Lang.makeThrow(DaoException.class, "NOT Pageable : " + st);
}
// 打印调试信息
String sql = st.toPreparedStatement();
if (log.isDebugEnabled())
log.debug(sql);
Object[][] paramMatrix = st.getParamMatrix();
CallableStatement stmt = null;
ResultSet rs = null;
try {
stmt = conn.prepareCall(sql);
ValueAdaptor[] adaptors = st.getAdaptors();
HashMap<Integer, OutParam> outParams = new HashMap<Integer, OutParam>();
if (st instanceof Sql) {
VarIndex varIndex = ((Sql) st).paramIndex();
VarSet varSet = ((Sql) st).params();
for (int i = 0; i < varIndex.size(); i++) {
String name = varIndex.getOrderName(i);
if (name.startsWith("OUT") && varSet.get(name).getClass() == Integer.class) {
Integer t = (Integer) varSet.get(name);
outParams.put(i, new OutParam(name, t));
}
}
}
// 创建语句并设置参数
if (paramMatrix != null && paramMatrix.length > 0) {
PreparedStatement pst = (PreparedStatement) stmt;
Object[] pm = paramMatrix[0];
for (int i = 0; i < pm.length; i++) {
OutParam outParam = outParams.get(i);
if (outParam == null)
adaptors[i].set(pst, pm[i], i + 1);
else
stmt.registerOutParameter(i + 1, outParam.jdbcType);
}
}
stmt.execute();
if (outParams.size() > 0) {
Record r = Record.create();
for (Entry<Integer, OutParam> en : outParams.entrySet()) {
OutParam outParam = en.getValue();
int jdbcIndex = en.getKey() + 1;
Object value;
switch(outParam.jdbcType) {
case Types.INTEGER:
value = stmt.getInt(jdbcIndex);
break;
case Types.TIMESTAMP:
value = stmt.getTimestamp(jdbcIndex);
break;
case Types.CLOB:
value = stmt.getString(jdbcIndex);
break;
case Types.DATE:
value = stmt.getDate(jdbcIndex);
break;
default:
value = stmt.getObject(jdbcIndex);
break;
}
r.set(outParam.name.substring(3), value);
}
st.getContext().attr("OUT", r);
}
//先尝试读取第一个,并调用一次回调
rs = stmt.getResultSet();
try {
st.onAfter(conn, rs, null);
} finally {
if (rs != null)
rs.close();
}
while (true) {
if (stmt.getMoreResults()) {
rs = stmt.getResultSet();
try {
if (rs != null)
st.onAfter(conn, rs, null);
} finally {
if (rs != null)
rs.close();
}
}
break;
}
} finally {
if (stmt != null)
stmt.close();
}
}
Aggregations