Search in sources :

Example 46 with CallableStatement

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;
}
Also used : CallableStatement(java.sql.CallableStatement) KeyGenerator(org.apache.ibatis.executor.keygen.KeyGenerator)

Example 47 with CallableStatement

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;
}
Also used : CallableStatement(java.sql.CallableStatement)

Example 48 with CallableStatement

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;
}
Also used : CallableStatement(java.sql.CallableStatement)

Example 49 with CallableStatement

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);
    }
}
Also used : CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 50 with CallableStatement

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();
    }
}
Also used : HashMap(java.util.HashMap) ValueAdaptor(org.nutz.dao.jdbc.ValueAdaptor) VarIndex(org.nutz.dao.sql.VarIndex) PreparedStatement(java.sql.PreparedStatement) Sql(org.nutz.dao.sql.Sql) CallableStatement(java.sql.CallableStatement) VarSet(org.nutz.dao.sql.VarSet) ResultSet(java.sql.ResultSet) Record(org.nutz.dao.entity.Record)

Aggregations

CallableStatement (java.sql.CallableStatement)273 SQLException (java.sql.SQLException)138 Connection (java.sql.Connection)125 ResultSet (java.sql.ResultSet)60 DatabaseAccessException (com.axway.ats.log.autodb.exceptions.DatabaseAccessException)45 DbConnection (com.axway.ats.core.dbaccess.DbConnection)28 Checkpoint (com.axway.ats.log.autodb.entities.Checkpoint)22 ArrayList (java.util.ArrayList)22 PreparedStatement (java.sql.PreparedStatement)21 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)20 Timestamp (java.sql.Timestamp)18 Test (org.junit.Test)16 CouldntDeleteException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)15 Statement (java.sql.Statement)14 HashMap (java.util.HashMap)10 CConnection (com.google.security.zynamics.binnavi.Database.CConnection)8 MockCallableStatement (com.alibaba.druid.mock.MockCallableStatement)7 MaybeNullException (com.google.security.zynamics.binnavi.Exceptions.MaybeNullException)6 BigInteger (java.math.BigInteger)6 OracleCallableStatement (oracle.jdbc.OracleCallableStatement)6