Java 在关闭同一连接上的不同语句之前,在连接上执行语句的行为是什么?
我试图理解在连接上执行语句的含义,同时在同一连接上处理不同语句执行的结果 举个例子:Java 在关闭同一连接上的不同语句之前,在连接上执行语句的行为是什么?,java,database,database-connection,Java,Database,Database Connection,我试图理解在连接上执行语句的含义,同时在同一连接上处理不同语句执行的结果 举个例子: public void doSomething(){ Connection dbConnection = null; PreparedStatement preparedStatement = null; try { dbConnection = getDBConnection(); preparedStatement = dbConnection.pre
public void doSomething(){
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement("some sql");
ResultSet rs = preparedStatement.executeQuery();
while (rs.next()) {
//...do some stuff with the results
/*
pass the db connection so we don't create
too many connections to the database
*/
doSomethingElse(dbConnection);
}
} catch (SQLException e) {
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
public void doSomethingElse(Connection dbConnection){
PreparedStatement preparedStatement2 = null;
try {
//using the same connection from "doSomething"
preparedStatement2 = dbConnection.prepareStatement("some more sql");
ResultSet rs2 = preparedStatement2.executeQuery();
while (rs2.next()) {
//...do some stuff with the results
}
} catch (SQLException e) {
} finally {
if (preparedStatement2 != null) {
preparedStatement2.close();
}
}
}
我的主要问题是:在使用同一连接关闭语句之前,在连接上执行语句的行为是什么?在同一个连接上执行另一个语句是否会损坏两个结果?我知道这样做是不允许的,但具体的行为是什么?还是行为不可预测
我一直在寻找,但在任何地方都找不到答案或例子。任何洞察或指导都会很好。JDBC 3.0规范规定: 13.1.1创建报表 。。。每个连接对象可以创建多个语句对象,这些语句对象可能 由程序同时使用
哇,我真的读了这个文档,但没有看到这个。我觉得自己很愚蠢。谢谢@wero,你是救命恩人