简体   繁体   English

每次调用execute()后,我应该关闭并创建一个新语句吗?

[英]Should I close and create a new statement after every time I call execute()?

If I create a statement with JDBC and execute a query, do I need to close said statement and create a new one before executing again? 如果我使用JDBC创建一个语句并执行查询,我是否需要在再次执行之前关闭所述语句并创建一个新语句? Eclipse doesn't complain about the second case. Eclipse没有抱怨第二种情况。

try {
        connection = dataSource.getConnection();

        try {
            statement = connection.createStatement();
            statement.execute("set search_path to '...'");
        } finally {
            Utils.tryClose(statement);
        }

        try {
            statement = connection.createStatement();
            statement.execute("SET statement_timeout TO " + (QUERY_TIMEOUT_SECONDS * 1000));
        } finally {
            Utils.tryClose(statement);
        }

        try {
            statement = connection.createStatement();
            statement.execute(query);
        } finally {
            Utils.tryClose(statement);
        }
} finally {
    Utils.tryClose(connection);
}

As opposed to: 相反:

try {
    connection = dataSource.getConnection();

    statement = connection.createStatement();
    statement.execute("set search_path to '...'");
    statement.execute("SET statement_timeout TO " + (QUERY_TIMEOUT_SECONDS * 1000));
    statement.execute(query);
} finally {
    Utils.tryClose(statement);
    Utils.tryClose(connection);
}

That is not required you can use the same statement to query the DB multiple times, the only thing to remember is that each resultset returned with a statement execution will be closed after creating a new statemnet. 这不是必需的,您可以使用相同的语句多次查询数据库,唯一要记住的是,在创建新的statemnet之后,将关闭每个返回语句执行的结果集。 Quoting from java docs :- 引用java文档 : -

By default, only one ResultSet object per Statement object can be open at the same time. 默认情况下,每个Statement对象只能同时打开一个ResultSet对象。 Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. 因此,如果读取一个ResultSet对象与另一个ResultSet对象的读取交错,则每个ResultSet对象必须由不同的Statement对象生成。 All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists. Statement接口中的所有执行方法都隐式关闭一个statment的当前ResultSet对象(如果存在一个open对象)。

Hence you can do something like this:- 因此你可以这样做: -

try {
     connection = dataSource.getConnection();

     statement = connection.createStatement();
     ResultSet rs1=statement.execute("....");

     //parse rs1
     //close rs1

     ResultSet rs2= statement.execute(....);
     //parse rs1
     //close rs1

  } finally {
    Utils.tryClose(statement);
    Utils.tryClose(connection);
  }

I am not sure why eclipse is complaining in case of PreparedStatements , the whole purpose of PreparedStatements is to define a query structure and execute the query multiple times by only changing the parameters. 我不知道为什么日食的情况下抱怨PreparedStatements ,整个目的PreparedStatements是定义查询结构,仅通过改变参数多次执行查询。 For example when you want to parse and insert a large text file into DB. 例如,当您要解析大型文本文件并将其插入数据库时​​。 Quoting from javadocs 引用javadocs

If you want to execute a Statement object many times, it usually reduces execution time to use a PreparedStatement object instead. 如果要多次执行Statement对象,通常会减少使用PreparedStatement对象的执行时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM