- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 DbUtils 从 SQL Server 中的存储过程检索结果时遇到问题。
在 SQL Server Management Studio 中执行时,存储过程在针对特定输入值执行时返回两个单独的结果集,但对于其他值,它仅返回一个结果集。下图说明了该问题:
我在这里面临的问题是我使用 DbUtils BeanListHandler
将结果转换为 UserInfo
beans 列表。
List<UserInfo> userList = (List<UserInfo>) run.query(STORED_PROC, new BeanListHandler(UserInfo.class), refId);
当存储过程仅返回一个结果集时,它工作正常。但是,在返回两个结果集的情况下,仅给出第一个结果集的列表。
我认为通过使用 JDBC,我们可以使用多个 ResultSet
,但我不确定如何处理这个 DbUtils。
有人可以提供见解吗?如果需要任何其他信息,请更新我,我将提供。
最佳答案
将 QueryRunner
对象进行子类化,然后调整适当的 query
方法来处理多个结果集是非常简单的。通过以下代码,我能够使用
UserInfo
对象的完整列表
ResultSetHandler<List<UserInfo>> h = new BeanListHandler<UserInfo>(UserInfo.class);
MyQueryRunner run = new MyQueryRunner(ds);
String sql =
"EXEC dbo.Gain_Web_GetCompanyRepByIndRefID @RefID=?";
List<UserInfo> result = run.query(sql, h, 2);
其中 MyQueryRunner
位于
package com.example.so36623732;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
public class MyQueryRunner extends QueryRunner {
public MyQueryRunner(DataSource ds) {
super(ds);
}
/**
* Executes the given SELECT or EXEC SQL query and returns a result object.
* The <code>Connection</code> is retrieved from the
* <code>DataSource</code> set in the constructor.
* @param <T> The type of object that the handler returns
* @param sql The SQL statement to execute.
* @param rsh The handler used to create the result object from
* the <code>ResultSet</code>.
* @param params Initialize the PreparedStatement's IN parameters with
* this array.
* @return An object generated by the handler.
* @throws SQLException if a database access error occurs
*/
public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
Connection conn = this.prepareConnection();
return this.<T>query(conn, true, sql, rsh, params);
}
/**
* Calls query after checking the parameters to ensure nothing is null.
* @param conn The connection to use for the query call.
* @param closeConn True if the connection should be closed, false otherwise.
* @param sql The SQL statement to execute.
* @param params An array of query replacement parameters. Each row in
* this array is one set of batch replacement values.
* @return The results of the query.
* @throws SQLException If there are database or parameter errors.
*/
@SuppressWarnings("unchecked")
private <T> T query(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params)
throws SQLException {
if (conn == null) {
throw new SQLException("Null connection");
}
if (sql == null) {
if (closeConn) {
close(conn);
}
throw new SQLException("Null SQL statement");
}
if (rsh == null) {
if (closeConn) {
close(conn);
}
throw new SQLException("Null ResultSetHandler");
}
PreparedStatement stmt = null;
ResultSet rs = null;
T result = null;
List<T> allResults = null;
try {
stmt = this.prepareStatement(conn, sql);
this.fillStatement(stmt, params);
rs = this.wrap(stmt.executeQuery());
allResults = (List<T>)rsh.handle(rs);
while (stmt.getMoreResults()) {
rs = stmt.getResultSet();
result = rsh.handle(rs);
allResults.addAll((List<T>)result);
}
} catch (SQLException e) {
this.rethrow(e, sql, params);
} finally {
try {
close(rs);
} finally {
close(stmt);
if (closeConn) {
close(conn);
}
}
}
return (T) allResults;
}
}
关于java - Apache DbUtils : Handling multiple result sets returned from Stored Procedure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36623732/
我按照此处的文档将 Azure Blob 存储容器安装到 Databricks: https://docs.databricks.com/_static/notebooks/data-sources/
我正在使用 apache dbutils。我有一个带有 java.util.Date 属性的对象。当我执行以下代码时,sql 失败 从 myTable 中选择名称、开始日期 try {
我是 Python 新手,需要 Databricks 方面的帮助。 我需要使用 Python 将文件从 Azure Blob 简单复制到 ADLS。 我需要 Python 文件中的代码,并且需要从 D
我有一个实例化单个 QueryRunner 的网络服务在初始化时使用数据源。它通过将这个 QueryRunner 对象作为 servlet 上下文属性传递给 webapp 使用的多个不同 servle
我在一个容器下有三个文件夹 文件夹结构 folder1 |_ file1.json |_ file2.json folder2 |_ sub-folder1 |_
我正在尝试使用 Jupyter Notebook Python 脚本(在 Docker 上运行)中的 DBUtils 和 Pyspark 来访问 Azure Data Lake Blob。但是,我似乎
我们需要使用第三方库来执行 SQL 语句,在开始实现之前,我们希望确保该库可以执行我们想要的任何操作。我们目前正在评估 Apache DBUtils。 我们尝试执行类似的查询 INSERT INTO
我有一个用于 jdbc 的 DBUtil 类,有一些方法,如查询、插入和更新,当发生一些错误时,我们现在抛出 RuntimeException,但我有一些问题。 当错误发生时,如果我不使用 try c
我正在尝试使用 ResultSetHandler 将学生列表传递给 servlet,但出现以下错误 java.lang.NumberFormatException: For input string:
我开始使用 DBUtils 库,但我的大多数查询都是“INSERT ON DUPLICATE KEY UPDATE”,我进行了研究,但没有找到有关如何使用 QueryRunner 对象实现该操作的信息
我发誓我以前做过这个并且效果很好,但似乎在我的查询(下面)中我无法映射同名的两列(在本例中为“电子邮件”),即使使用我查询中的“as”。我也尝试过不使用“as”——只使用“u.email assess
apache dbutils库是否可以调用oracle存储过程? 最佳答案 是的: QueryRunner run = JDBCConnectionFactory.getQueryRunner(dat
我需要使用 DBUtils 获取标量值,有人知道如何获取吗?目前我这样做: String sql = "SELECT count(*) FROM t1 WHERE cod = ?";
有人可以让我知道如何使用 databricks dbutils 从文件夹中删除所有文件。 我尝试了以下但不幸的是,Databricks 不支持通配符。 dbutils.fs.rm('adl://azu
我们如何尝试使用它? 将依赖项“com.databricks”%%“dbutils-api”%“0.0.1”添加到build.sbt 用代码编写: 导入 com.databricks.dbutils_
我找不到如何使用 DBUtils 来调用可调用语句 我们可以如何指定哪些所有字段都是输入以及哪些所有字段都是输出? 最佳答案 您只需要使用CallableStatement Connection co
Apache DBUtils 的 BeanHandler 在使用 SELECT 查询时可以方便地将数据映射到 Bean 中。但是,在写入数据时似乎不能做到同样的事情,所以我需要使用一种容易出错的方式:
我正在尝试使用 SQL Server 数据库上的 queryrunner 接口(interface)从列表中检索多条记录。似乎正常方法有效时接口(interface)会抛出错误。 private vo
我正在 Spring Framework 中创建 MVC Web 应用程序,我需要将 Apache DBUtils 结果集中的行转换为由嵌套对象组成的 JavaBeans。 关于我发现的极少数示例,我
我一直在尝试了解如何使用结果集中的数据填充 jtable,并发现使用 DBUtils 可能是最简单的方法,但不幸的是它根本不适合我。程序运行,但 jtable 仍然是空的。我不明白我哪里出了问题。我有
我是一名优秀的程序员,十分优秀!