云数据库 GaussDB-执行SQL语句:执行预编译SQL语句

时间:2023-11-01 16:22:10

执行预编译SQL语句

预编译语句是只编译和优化一次,然后可以通过设置不同的参数值多次使用。由于已经预先编译好,后续使用会减少执行时间。因此,如果多次执行一条语句,请选择使用预编译语句。可以按以下步骤执行:

  1. 调用Connection的prepareStatement方法创建预编译语句对象。

    1
    PreparedStatement pstmt = con.prepareStatement("UPDATE customer_t1 SET c_customer_name = ? WHERE c_customer_sk = 1");

  2. 调用PreparedStatement的setShort设置参数。

    1
    pstmt.setShort(1, (short)2);

  3. 调用PreparedStatement的executeUpdate方法执行预编译SQL语句。

    1
    int rowcount = pstmt.executeUpdate();

  4. 调用PreparedStatement的close方法关闭预编译语句对象。

    1
    pstmt.close();

support.huaweicloud.com/centralized-devg-v2-opengauss/devg_03_0095.html