相关疑难解决方法(0)

正确的重用PreparedStatement实例的方法?

什么是创建PreparedStatement的正确方法,重复使用几次,然后清理它?我使用以下模式:

Connection conn = null;
PreparedStatement stmt = null;

try {
    conn = getConnection(...);

    // first use
    stmt = conn.prepareStatement("some statement ?");
    stmt.setString(1, "maybe some param");
    if (stmt.execute()) {
        ...
    }

    // second use
    stmt = conn.prepareStatement("some statement ?");
    stmt.setString(1, "maybe some param");
    if (stmt.execute()) {
        ...
    }

    // third use.
    stmt = conn.prepareStatement("some statement");
    stmt.execute();
}
finally {
    if (stmt != null) {
        try {
            stmt.close();
        } catch (Exception sqlex) {
            sqlex.printStackTrace();
        }

        stmt = null;
    }

    if (conn …
Run Code Online (Sandbox Code Playgroud)

java jdbc prepared-statement

5
推荐指数
1
解决办法
1万
查看次数

重用ResultSet

我需要连续运行几个查询

Statement st = cnx.createStatement();
ResultSet rs = st.executeQuery( "SELECT [good stuff]");
// do something smart with rs
rs = st.execute( "SELECT [better stuff]");
// do something smarter with rs
rs = st.execute( "SELECT [best stuff]");
// you got it
try{ rs.close();} catch( SQLException ignore){};
try{ st.close();} catch( SQLException ignore){};
Run Code Online (Sandbox Code Playgroud)

这是一个问题,前两个ResultSet没有正确关闭,还是在垃圾收集过程中隐含地完成了?

java sql jdbc resultset

3
推荐指数
1
解决办法
7825
查看次数

标签 统计

java ×2

jdbc ×2

prepared-statement ×1

resultset ×1

sql ×1