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是一个简单的Hive表,其字符串类型为spd_field_label_id和SUBJECT_USER_ID。
使用Hive2简单的PreparedStatement抛出下面的错误堆栈跟踪。什么可能是错的任何指针?使用Statement而不是PreparedStatement并且不使用时,相同的查询工作正常?用于参数绑定。
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)
答案 0 :(得分:0)
stmt.executeQuery(query);
您使用的是错误的方法。你已经准备好了这份声明。它已准备好执行。它应该是:
stmt.execute();