在 H2 中通过 Prepared语句进行查询会引发异常:预准备语句不允许使用此方法;请改用常规语句



Java 11 中使用 JDBC 4 访问 H2 数据库时,通过预准备语句运行简单查询失败。

运行此行时:

try ( ResultSet rs = pstmt.executeQuery( sql ) ; ) {

。我收到此错误:

org.h2.jdbc.JdbcSQLException:预准备语句不允许使用此方法;请改用常规语句。[90130-197]

我尝试使用 H2 错误分析器,但没有帮助。

下面是单个.java文件中的完整示例应用。您可以复制粘贴并自行运行。

package com.basilbourque.example.work.basil.example.h2.pstmt_query;
import org.h2.jdbcx.JdbcDataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class App {
    public static void main ( String[] args ) {
        App app = new App();
        app.doIt();
    }
    private void doIt ( ) {
        // Create database.
        try {
            Class.forName( "org.h2.Driver" );
        } catch ( ClassNotFoundException e ) {
            e.printStackTrace();
        }
        JdbcDataSource dataSource = new JdbcDataSource();
        dataSource.setURL( "jdbc:h2:mem:pstmt_query_example_db;DB_CLOSE_DELAY=-1" ); // Set `DB_CLOSE_DELAY` to `-1` to keep in-memory database in existence after connection closes.
        dataSource.setUser( "scott" );
        dataSource.setPassword( "tiger" );
        // Create table.
        try (
                Connection conn = dataSource.getConnection() ;
                Statement stmt = conn.createStatement() ;
        ) {
            String sql = "CREATE TABLE person_ ( n" +
                    " pkey_ UUID NOT NULL DEFAULT RANDOM_UUID() PRIMARY KEY , n" +
                    " name_ VARCHAR NOT NULL n" +
                    ");";
            System.out.println( sql );
            stmt.execute( sql );
        } catch ( SQLException e ) {
            e.printStackTrace();
        }
        // Query table.
        List < UUID > list = new ArrayList <>();
        String sql = "SELECT * FROM person_ WHERE name_ = ? ;";
        try (
                Connection conn = dataSource.getConnection() ;
                PreparedStatement pstmt = conn.prepareStatement( sql ) ;
        ) {
            String name = "Wendy Melvoin";
            pstmt.setString( 1 , name );
            try ( ResultSet rs = pstmt.executeQuery( sql ) ; ) {  // org.h2.jdbc.JdbcSQLException: This method is not allowed for a prepared statement; use a regular statement instead. [90130-197]
                while ( rs.next() ) {
                    UUID pkey = rs.getObject( "pkey_" , UUID.class );
                    list.add( pkey );
                }
            }
        } catch ( SQLException e ) {
            e.printStackTrace();
        }
    }
}

报告异常:

org.h2.jdbc.JdbcSQLException: This method is not allowed for a prepared statement; use a regular statement instead. [90130-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.message.DbException.get(DbException.java:144)
    at org.h2.jdbc.JdbcPreparedStatement.executeQuery(JdbcPreparedStatement.java:302)
    at com.basilbourque.example.work.basil.example.h2.pstmt_query.App.doIt(App.java:53)
    at com.basilbourque.example.work.basil.example.h2.pstmt_query.App.main(App.java:13)

传递 SQL 字符串两次

PreparedStatement,你永远不会将SQL字符串传递给executeQuery方法。你这样做是在毫无准备的Statement,但不是PreparedStatement。请注意 JavaDoc for PreparedStatement::executeQuery 如何不进行任何争论。

所以你的行:

try ( ResultSet rs = pstmt.executeQuery( sql ) ; ) {

。应该是:

try ( ResultSet rs = pstmt.executeQuery() ; ) {

当您准备语句时,您已经在该行上方传递了名为 sql 的 SQL 字符串:

PreparedStatement pstmt = conn.prepareStatement( sql ) ;

由于名为 pstmtPreparedStatement已经包含您的 SQL 语句,因此无需传递到 executeQuery

此错误可能是使用 Statement 复制粘贴某些代码以使用 PreparedStatement 在其他代码中重用的结果。

最新更新