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

use*_*701 5 java jdbc prepared-statement

什么是创建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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我们可以像这样重用"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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

jse*_*gal 5

反之亦然 - 您只需要准备一次,然后重复使用它.

IE这个:

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

应该成为这样的:

// second use
    stmt.setString(1, "maybe some param");
    if (stmt.execute()) {
        ...
    }
Run Code Online (Sandbox Code Playgroud)

您的第三次使用,这是一个不同的声明,应该是一个新变量,或者首先关闭您准备好的声明.(尽管通常使用PreparedStatements,您可以保留它们并重复使用它们).