使用 JDBC 驱动程序,如何在不不断打开和关闭连接的情况下使用来自不同查询的多个结果集,因为它正在提取我需要的内容并将其传递给另一个方法。每次打开一个新的conn、statement和resultset
public static void OppQuery() {
Connection conn = null;
Statement stmt = null;
ResultSet result_set = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL);
stmt = conn.createStatement();
String emailInfoQuery = "FROM WHERE";
}
public static void addQuery(String latestRenewalId) {
Connection conn = null;
Statement stmt = null;
ResultSet result_set = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL);
stmt = conn.createStatement();
String new_AddressLine1, new_AddressLine2;
new_AddressLine1=new_AddressLine2="";
int new_ChannelOption= 0;
}
我尝试在一种方法中使用多个结果集,但它不断抛出异常,表示结果集已关闭。我对 SqlServver 没有太多经验,因此任何指导都会有帮助:-)
最佳答案
在 JDBC 中,当连接处于自动提交模式(默认)时,任何语句的执行都会提交前一个事务并关闭同一连接上任何打开的结果集(假设可保持性为 ResultSet.CLOSE_CURSORS_AT_COMMIT
,实际上是 isn't the default for SQL Server )。当您需要打开多个结果集时,您需要禁用自动提交(或使用ResultSet.HOLD_CURSORS_OVER_COMMIT
)并且您需要使用多个Statement
对象。 JDBC 要求对同一 Statement
对象执行新操作会关闭同一语句中任何其他打开的结果集。
所以
- 禁用自动提交 (
Connection.setAutoCommit(false)
) - 使用多个
Statement
对象打开ResultSet
关于java - 多个结果集、不同查询、同一数据库 SQLSERVER,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21428961/