【问题标题】:Correct way of reusing PreparedStatement instance?重用 PreparedStatement 实例的正确方法?
【发布时间】:2011-06-24 22:16:13
【问题描述】:

创建 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 != null) {
        try {
            conn.close();
        } catch (Exception sqlex) {
        sqlex.printStackTrace();
        }

        conn = null;
    }
}

我们可以像那样重用“stmt”对象,还是我们必须在每个查询之间调用 stmt.close()?

谢谢

------------ 更新------------

啊,好的,我明白了,我的每个陈述都会有所不同。那么这是一个更正确的模式吗?:

Connection conn = null;
PreparedStatement stmt = null;

try {
    conn = getConnection(...);

    // first use
    PreparedStatement stmt1 = null;
    try {
        stmt1 = conn.prepareStatement("some statement ?");
        stmt1.setString(1, "maybe some param");
        if (stmt1.execute()) {
            ...
        }
    }
    finally {
        if (stmt1 != null) {
            try {
                stmt1.close();
            } catch (Exception ex) {}
        }
    }

    // second use
    PreparedStatement stmt2 = null;
    try {
        stmt2 = conn.prepareStatement("some different statement ?");
        stmt2.setString(1, "maybe some param");
        if (stmt2.execute()) {
            ...
        }
    }
    finally {
        if (stmt2 != null) {
            try {
                stmt2.close();
            } catch (Exception ex) {}
        }
    }

    // third use
    PreparedStatement stmt3 = null;
    try {
        stmt3 = conn.prepareStatement("yet another statement ?");
        stmt3.setString(1, "maybe some param");
        if (stmt3.execute()) {
            ...
        }
    }
    finally {
        if (stmt3 != null) {
            try {
                stmt3.close();
            } catch (Exception ex) {}
        }
    }
}
finally {
    if (conn != null) {
        try {
            conn.close();
        } catch (Exception sqlex) {
        sqlex.printStackTrace();
        }

        conn = null;
    }
}

因此每个不同的语句将在下一个执行之前单独关闭。

【问题讨论】:

标签: java jdbc prepared-statement


【解决方案1】:

反之亦然——您只需准备一次,然后重复使用。

I.E.这个:

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

应该变成这样:

// second use
    stmt.setString(1, "maybe some param");
    if (stmt.execute()) {
        ...
    }

您的第三次使用,即不同的语句,应该是一个新变量,或者先关闭您准备好的语句。 (虽然通常使用 PreparedStatements,但您可以保留它们并重复使用它们)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多