Me and my team faced a problem. We are trying to retrieve some data from Sybase IQ database and are using where
clause to filter out and get specific data.
The SQL is tested and works fine but it fails when using Prepared Statement.
Tests done:
- If we run the query (with or without where clause parameters), it works fine.
- If we run the query with parameters hard coded in the Prepared Statement, it also works fine.
- If we set the parameters of prepared statement programmatically, it does not work.
The above tests confirm the JDBC connection is working fine.
The same error appears when PreparedStatement, JdbcTemplate or NamedParameterJdbcTemplate is used, so I suspect there might be an issue between PreparedStatement and Sybase IQ.
Could anyone help to investigate that? We have found a workaround for that, but it would be really useful to know why this was not working.
I found very similar thread (How do I execute PreparedStatement(select object_id()) in sybase iq?) about the same issue, but nobody provided an accepted and correct answer there, so I decided to create a new question for this.
The code used is:
Class.forName("com.sybase.jdbc4.jdbc.SybDriver");
PreparedStatement stmt = con.prepareStatement("select * from myView where off = ? and acc = ?");
stmt.setString(1, "260");
stmt.setString(2, "9050V");
ResultSet set = stmt.executeQuery();
The error message is:
Exception in thread "main" java.sql.SQLException: JZ0SA: Prepared Statement: Input parameter not set, index: 0.
at com.sybase.jdbc4.jdbc.SybConnection.getAllExceptions(Unknown Source)
at com.sybase.jdbc4.jdbc.SybStatement.handleSQLE(Unknown Source)
at com.sybase.jdbc4.jdbc.SybStatement.sendQuery(Unknown Source)
at com.sybase.jdbc4.jdbc.SybPreparedStatement.sendQuery(Unknown Source)
at com.sybase.jdbc4.jdbc.SybStatement.executeQuery(Unknown Source)
at com.sybase.jdbc4.jdbc.SybPreparedStatement.executeQuery(Unknown Source)
JDBC Driver used (Maven dependency):
<dependency>
<groupId>com.sybase</groupId>
<artifactId>jconn4</artifactId>
<version>7.0</version>
</dependency>