I don't manage to make AWS Athena JDBC driver working with PreparedStatement and binded variables. If I put the desired value of a column directly in the SQL string, it works. But if I use placeholders '?' and I bind variables with setters of PreparedStatement, it does not work. Of course, we know we have to use the second way of doing (for caching, avoid SQL injection and so on).
I use JDBC Driver AthenaJDBC42_2.0.2.jar. I get the following error when trying to use placeholders '?' in the SQL String. The error is thrown when I get the PreparedStatement from the JDBC Connection. It complains about parameters not being found. But I set them after in the code. How can I set the parameters before getting the PreparedStatement :-) ?
java.sql.SQLException: [Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client. SYNTAX_ERROR: line 1:1: Incorrect number of parameters: expected 1 but found 0
at com.simba.athena.athena.api.AJClient.executeQuery(Unknown Source)
at com.simba.athena.athena.dataengine.AJQueryExecutor.<init>(Unknown Source)
at com.simba.athena.athena.dataengine.AJDataEngine.prepare(Unknown Source)
at com.simba.athena.jdbc.common.SPreparedStatement.<init>(Unknown Source)
at com.simba.athena.jdbc.jdbc41.S41PreparedStatement.<init>(Unknown Source)
at com.simba.athena.jdbc.jdbc42.S42PreparedStatement.<init>(Unknown Source)
at com.simba.athena.jdbc.jdbc42.JDBC42ObjectFactory.createPreparedStatement(Unknown Source)
at com.simba.athena.athena.jdbc42.AJJDBC42ObjectFactory.createPreparedStatement(Unknown Source)
at com.simba.athena.jdbc.common.SConnection.prepareStatement(Unknown Source)
at com.simba.athena.jdbc.common.SConnection.prepareStatement(Unknown Source)
at ****************************************************
Caused by: com.simba.athena.support.exceptions.GeneralException: [Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client. SYNTAX_ERROR: line 1:1: Incorrect number of parameters: expected 1 but found 0
... 37 more
Am I doing something wrong ? Here is the code
@Test
public void testWhichFails() throws SQLException {
try (Connection connection = athenaConnexion()) {
String sql = "select * from my_table where col = ? limit 10";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setInt(1, 30);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
System.out.println("rs.getString(1) = " + rs.getString(1));
}
}
}
}
}
@Test
public void testWhichWorks() throws SQLException {
try (Connection connection = athenaConnexion()) {
String sql = "select * from my_table where col = 30 limit 10";
try (PreparedStatement ps = connection.prepareStatement(sql)) {
//ps.setInt(1, 30);
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
System.out.println("rs.getString(1) = " + rs.getString(1));
}
}
}
}
}