org.apache.commons.dbutils.QueryRunner.execute()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(10.8k)|赞(0)|评价(0)|浏览(224)

本文整理了Java中org.apache.commons.dbutils.QueryRunner.execute方法的一些代码示例,展示了QueryRunner.execute的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。QueryRunner.execute方法的具体详情如下:
包路径:org.apache.commons.dbutils.QueryRunner
类名称:QueryRunner
方法名:execute

QueryRunner.execute介绍

[英]Execute an SQL statement, including a stored procedure call, which returns one or more result sets. Any parameters which are instances of OutParameter will be registered as OUT parameters.

Use this method when: a) running SQL statements that return multiple result sets; b) invoking a stored procedure that return result sets and OUT parameters. Otherwise you may wish to use #query(java.lang.String,org.apache.commons.dbutils.ResultSetHandler,java.lang.Object...)(if there are no OUT parameters) or #execute(java.lang.String,java.lang.Object...)(if there are no result sets).
[中]执行一条SQL语句,包括一个存储过程调用,该语句返回一个或多个结果集。任何作为OutParameter实例的参数都将注册为OUT参数。
在以下情况下使用此方法:a)运行返回多个结果集的SQL语句;b) 调用返回结果集并输出参数的存储过程。否则,您可能希望使用#query(java.lang.String、org.apache.commons.dbutils.ResultSetHandler、java.lang.Object…)(如果没有OUT参数)或#execute(java.lang.String、java.lang.Object…)(如果没有结果集)。

代码示例

代码示例来源:origin: codingapi/tx-lcn

@Override
public void createLockTable() {
  try {
    queryRunner.execute(txcSql.lockTableSql());
  } catch (SQLException e) {
    log.error("txc > sql executor > create lock table error.", e);
  }
}

代码示例来源:origin: codingapi/tx-lcn

@Override
public void createUndoLogTable() {
  try {
    queryRunner.execute(txcSql.undoLogTableSql());
  } catch (SQLException e) {
    log.error("txc > sql executor > create undo_log table error.", e);
  }
}

代码示例来源:origin: commons-dbutils/commons-dbutils

/**
 * Execute an SQL statement, including a stored procedure call, which does
 * not return any result sets.
 * Any parameters which are instances of {@link OutParameter} will be
 * registered as OUT parameters.
 * <p>
 * Use this method when invoking a stored procedure with OUT parameters
 * that does not return any result sets.  If you are not invoking a stored
 * procedure, or the stored procedure has no OUT parameters, consider using
 * {@link #update(java.sql.Connection, java.lang.String, java.lang.Object...) }.
 * If the stored procedure returns result sets, use
 * {@link #execute(java.sql.Connection, java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }.
 *
 * @param conn The connection to use to run the query.
 * @param sql The SQL to execute.
 * @param params The query replacement parameters.
 * @return The number of rows updated.
 * @throws SQLException if a database access error occurs
 */
public int execute(Connection conn, String sql, Object... params) throws SQLException {
  return this.execute(conn, false, sql, params);
}

代码示例来源:origin: commons-dbutils/commons-dbutils

/**
 * Execute an SQL statement, including a stored procedure call, which
 * returns one or more result sets.
 * Any parameters which are instances of {@link OutParameter} will be
 * registered as OUT parameters.
 * <p>
 * Use this method when: a) running SQL statements that return multiple
 * result sets; b) invoking a stored procedure that return result
 * sets and OUT parameters.  Otherwise you may wish to use
 * {@link #query(java.sql.Connection, java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }
 * (if there are no OUT parameters) or
 * {@link #execute(java.sql.Connection, java.lang.String, java.lang.Object...) }
 * (if there are no result sets).
 *
 * @param <T> The type of object that the handler returns
 * @param conn The connection to use to run the query.
 * @param sql The SQL to execute.
 * @param rsh The result set handler
 * @param params The query replacement parameters.
 * @return A list of objects generated by the handler
 * @throws SQLException if a database access error occurs
 */
public <T> List<T> execute(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
  return this.execute(conn, false, sql, rsh, params);
}

代码示例来源:origin: commons-dbutils/commons-dbutils

/**
 * Execute an SQL statement, including a stored procedure call, which does
 * not return any result sets.
 * Any parameters which are instances of {@link OutParameter} will be
 * registered as OUT parameters.
 * <p>
 * Use this method when invoking a stored procedure with OUT parameters
 * that does not return any result sets.  If you are not invoking a stored
 * procedure, or the stored procedure has no OUT parameters, consider using
 * {@link #update(java.lang.String, java.lang.Object...) }.
 * If the stored procedure returns result sets, use
 * {@link #execute(java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }.
 * <p>
 * The <code>Connection</code> is retrieved from the <code>DataSource</code>
 * set in the constructor.  This <code>Connection</code> must be in
 * auto-commit mode or the update will not be saved.
 *
 * @param sql The SQL statement to execute.
 * @param params Initializes the CallableStatement's parameters (i.e. '?').
 * @throws SQLException if a database access error occurs
 * @return The number of rows updated.
 */
public int execute(String sql, Object... params) throws SQLException {
  Connection conn = this.prepareConnection();
  return this.execute(conn, true, sql, params);
}

代码示例来源:origin: commons-dbutils/commons-dbutils

/**
 * Execute an SQL statement, including a stored procedure call, which
 * returns one or more result sets.
 * Any parameters which are instances of {@link OutParameter} will be
 * registered as OUT parameters.
 * <p>
 * Use this method when: a) running SQL statements that return multiple
 * result sets; b) invoking a stored procedure that return result
 * sets and OUT parameters.  Otherwise you may wish to use
 * {@link #query(java.lang.String, org.apache.commons.dbutils.ResultSetHandler, java.lang.Object...) }
 * (if there are no OUT parameters) or
 * {@link #execute(java.lang.String, java.lang.Object...) }
 * (if there are no result sets).
 *
 * @param <T> The type of object that the handler returns
 * @param sql The SQL to execute.
 * @param rsh The result set handler
 * @param params The query replacement parameters.
 * @return A list of objects generated by the handler
 * @throws SQLException if a database access error occurs
 */
public <T> List<T> execute(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
  Connection conn = this.prepareConnection();
  return this.execute(conn, true, sql, rsh, params);
}

代码示例来源:origin: commons-dbutils/commons-dbutils

@Test(expected=SQLException.class)
public void testNullConnectionExecute() throws Exception {
  when(meta.getParameterCount()).thenReturn(2);
  when(dataSource.getConnection()).thenReturn(null);
  runner.execute("{call my_proc(?, ?)}", "unit", "test");
}

代码示例来源:origin: commons-dbutils/commons-dbutils

@Test(expected=SQLException.class)
public void testNullSqlExecute() throws Exception {
  when(meta.getParameterCount()).thenReturn(2);
  runner.execute(null);
}

代码示例来源:origin: commons-dbutils/commons-dbutils

@Test(expected=SQLException.class)
public void testNullSqlExecuteWithResultSet() throws Exception {
  when(meta.getParameterCount()).thenReturn(2);
  runner.execute(null, handler);
}

代码示例来源:origin: commons-dbutils/commons-dbutils

@Test(expected=SQLException.class)
public void testNullConnectionExecuteWithResultSet() throws Exception {
  when(meta.getParameterCount()).thenReturn(2);
  when(dataSource.getConnection()).thenReturn(null);
  runner.execute("{call my_proc(?, ?)}", handler, "unit", "test");
}

代码示例来源:origin: commons-dbutils/commons-dbutils

@Test(expected=SQLException.class)
public void testNullHandlerExecute() throws Exception {
  when(meta.getParameterCount()).thenReturn(2);
  runner.execute("{call my_proc(?, ?)}");
}

代码示例来源:origin: commons-dbutils/commons-dbutils

@Test(expected=SQLException.class)
public void testNullHandlerExecuteWithResultSet() throws Exception {
  when(meta.getParameterCount()).thenReturn(2);
  runner.execute("{call my_proc(?, ?)}", (ResultSetHandler)null);
}

代码示例来源:origin: commons-dbutils/commons-dbutils

@Test
public void testExecuteWithMultipleResultSets() throws Exception {
  when(call.execute()).thenReturn(true);
  when(call.getMoreResults()).thenAnswer(new Answer<Boolean>()
  {
    int count = 1;
    @Override
    public Boolean answer(InvocationOnMock invocation)
    {
      return ++count <= 3;
    }
  });
  when(meta.getParameterCount()).thenReturn(0);
  List<Object[]> objects = runner.execute("{call my_proc()}", handler);
  Assert.assertEquals(3, objects.size());
  verify(call, times(1)).execute();
  verify(results, times(3)).close();
  verify(call, times(1)).close();    // make sure we closed the statement
  verify(conn, times(1)).close();    // make sure we close the connection
}

代码示例来源:origin: commons-dbutils/commons-dbutils

int result = runner.execute(conn, "{call my_proc(?, ?)}", "unit", "test");
result = runner.execute(conn, "{call my_proc()}");
OutParameter<Integer> intParam =
  new OutParameter<Integer>(Types.INTEGER, Integer.class);
result = runner.execute(conn, "{?= call my_proc()}", intParam);
when(call.getObject(1)).thenReturn(4242);
intParam.setValue(null);
result = runner.execute(conn, "{?= call my_proc(?, ?)}", intParam, "unit", "test");
OutParameter<String> stringParam =
  new OutParameter<String>(Types.VARCHAR, String.class, "in");
result = runner.execute(conn, "{?= call my_proc(?, ?)}", intParam, "test", stringParam);

代码示例来源:origin: commons-dbutils/commons-dbutils

runner.execute(conn, "{call my_proc(?, ?)}", handler, "unit", "test");
runner.execute(conn, "{call my_proc()}", handler);
OutParameter<Integer> intParam =
  new OutParameter<Integer>(Types.INTEGER, Integer.class);
runner.execute(conn, "{?= call my_proc()}", handler, intParam);
when(call.getObject(1)).thenReturn(4242);
intParam.setValue(null);
runner.execute(conn, "{?= call my_proc(?, ?)}", handler, intParam, "unit", "test");
OutParameter<String> stringParam =
  new OutParameter<String>(Types.VARCHAR, String.class, "in");
runner.execute(conn, "{?= call my_proc(?, ?)}", handler, intParam, "test", stringParam);

代码示例来源:origin: commons-dbutils/commons-dbutils

runner.execute("{call my_proc(?, ?)}", handler, "unit", "test");
runner.execute("{call my_proc()}", handler);
OutParameter<Integer> intParam =
  new OutParameter<Integer>(Types.INTEGER, Integer.class);
runner.execute("{?= call my_proc()}", handler, intParam);
when(call.getObject(1)).thenReturn(4242);
intParam.setValue(null);
runner.execute("{?= call my_proc(?, ?)}", handler, intParam, "unit", "test");
OutParameter<String> stringParam =
  new OutParameter<String>(Types.VARCHAR, String.class, "in");
runner.execute("{?= call my_proc(?, ?)}", handler, intParam, "test", stringParam);

代码示例来源:origin: commons-dbutils/commons-dbutils

int result = runner.execute("{call my_proc(?, ?)}", "unit", "test");
result = runner.execute("{call my_proc()}");
OutParameter<Integer> intParam =
  new OutParameter<Integer>(Types.INTEGER, Integer.class);
result = runner.execute("{?= call my_proc()}", intParam);
when(call.getObject(1)).thenReturn(4242);
intParam.setValue(null);
result = runner.execute("{?= call my_proc(?, ?)}", intParam, "unit", "test");
OutParameter<String> stringParam =
  new OutParameter<String>(Types.VARCHAR, String.class, "in");
result = runner.execute("{?= call my_proc(?, ?)}", intParam, "test", stringParam);

相关文章