Hive 2 JDBC PreparedStatement throws an error cannot recognize input near '?' '<EOF>' '<EOF>' in expression specification

梦想的初衷 提交于 2019-12-12 06:51:13

问题


try {
        Class.forName("org.apache.hive.jdbc.HiveDriver");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    }
    String query = "SELECT spd_field_label_id FROM RAL WHERE SUBJECT_USER_ID = ?";

    PreparedStatement stmt = null;
    Connection con = null;
    boolean testCasePassed = false;

    try {

        con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
        stmt = con.prepareStatement(query);

        stmt.setString(1, "USR-44");
        ResultSet resultSet = stmt.executeQuery(query);

        Assert.assertNotNull(resultSet);

        while (resultSet.next()) {
            testCasePassed = true;
            System.out.println("=======Test =========" + resultSet.getString("spd_field_label_id"));
        }
    } finally {

        if (stmt != null) {
            stmt.close();
        }

        if (con != null) {
            con.close();
        }

    }

    return testCasePassed;

RAL is a simple Hive table with String type columns spd_field_label_id and SUBJECT_USER_ID.

Simple PreparedStatement using Hive2 throwing an Error stacktrace below. Any pointers on what could be wrong? Same query works fine when using Statement instead of PreparedStatement and without using ? for parameter binding.

org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: ParseException line 1:62 cannot recognize input near '?' '<EOF>' '<EOF>' in expression specification
at org.apache.hive.jdbc.Utils.verifySuccess(Utils.java:264)
at org.apache.hive.jdbc.Utils.verifySuccessWithInfo(Utils.java:250)
at org.apache.hive.jdbc.HiveStatement.runAsyncOnServer(HiveStatement.java:309)
at org.apache.hive.jdbc.HiveStatement.execute(HiveStatement.java:250)
at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:434)

回答1:


stmt.executeQuery(query);

You're using the wrong method. You've already prepared the statement. It is ready to execute. It should be:

stmt.execute();


来源:https://stackoverflow.com/questions/46859270/hive-2-jdbc-preparedstatement-throws-an-error-cannot-recognize-input-near

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!