'DB2/JAVA: dbjcc doesn't detect parameter marker at my SQL
I'm using Java against a DB2 database and I'm having this error message:
Error: com.ibm.db2.jcc.am.SqlSyntaxErrorException: [jcc][10448][12695][4.19.66] Invalid argument: Parameter marker ':machineIdsParam' was not present in the SQL String ERRORCODE=-4461, SQLSTATE=42815
with the code here below:
String ENABLED_NAMED_PARAMS_YES = "1";
int DB2_DRIVER_TYPE = 4;
Properties properties = new Properties(); // Create Properties object
properties.put("user", config.getDbUser()); // Set user ID for connection
properties.put("password", config.getDbPassword()); // Set password for connection
properties.put("enableNamedParameterMarkers", ENABLED_NAMED_PARAMS_YES);
properties.put("driverType", DB2_DRIVER_TYPE);
// Load the driver
Class.forName("com.ibm.db2.jcc.DB2Driver");
//Establish connection
DB2Connection currentConnection = (DB2Connection) DriverManager.getConnection(config.getDbUrl(), properties);
StringBuilder sb = new StringBuilder();
sb.append("WITH mymachines AS (SELECT machineId");
sb.append(" FROM XMLTABLE('for $id in tokenize($s, \",\") return <i>{string($id)}</i>' ");
sb.append(" PASSING CAST (:machineIdsParam AS VARCHAR (100)) AS \"s\" COLUMNS");
sb.append(" machineId INT PATH '.')) ");
sb.append(" SELECT machineId from mymachines ");
String sqlString = sb.toString();
String machineIds = "930,603"; // This is an input I receive as a parameter;
DB2PreparedStatement ps = (DB2PreparedStatement) currentConnection.prepareStatement(sqlString);
ps.setJccStringAtName(":machineIdsParam", machineIds);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
System.out.println(rs.toString());
}
The strange thing is that this is code working in another Java class and I can't find the difference other than the very SQL string, which is somehow different, except for the XML table part. I suspect something is going on with the db2 driver for Java.
Additional data:
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>db2jcc</artifactId>
<version>4.19.66</version>
</dependency>
IBM Data Server Driver for JDBC and SQLJ: 4.19.66
SELECT VERSIONNUMBER FROM SYSIBM.SYSVERSIONS
| VERSIONNUMBER |
|---|
| -10050900 |
| 9070900 |
| 10050800 |
| 10051000 |
| 11010405 |
| 11010406 |
| 11050700 |
Solution 1:[1]
Wrong parameter name reference in the setJccXXXAtName method.
Try:ps.setJccStringAtName("machineIdsParam", machineIds);
instead of:ps.setJccStringAtName(":machineIdsParam", machineIds);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mark Barinstein |
