I've seen examples here and elsewhere that say this should work.
With '1' parameter in the IN CLAUSE I have success:
SELECT AVG( ( High_Temperature + Low_Temperature ) / 2.0 )
FROM obs_masterAll
WHERE Observation_Valid_Time = ? AND
Location_ID IN ( ? );
Microsoft JDBC Driver 4.2 for SQL Server 4.2.6420.100
Parameter Count = 2
2010, 36.5
But, with more than one parameter in the IN CLAUSE, I get this:
SELECT AVG( ( High_Temperature + Low_Temperature ) / 2.0 )
FROM obs_masterAll
WHERE Observation_Valid_Time = ?
AND Location_ID IN ( ?, ? ); <--- PROBLEM HERE
Microsoft JDBC Driver 4.2 for SQL Server 4.2.6420.100
Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ','.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:191)
at com.microsoft.sqlserver.jdbc.SQLServerParameterMetaData.<init>(SQLServerParameterMetaData.java:423)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.getParameterMetaData(SQLServerPreparedStatement.java:1659)
at CreateAMultiStationDailyLoadCtrAvgHistogram.main(CreateAMultiStationDailyLoadCtrAvgHistogram.java:68)
Java Result: 1
Here's my code:
prepStmt = buildPreparedStatement( conn.getConnectionObject() ); //function below
conn.printDriverInfo();
sqlDate = new java.sql.Date( beginDate.getTimeInMillis() );
ParameterMetaData pmData = prepStmt.getParameterMetaData();
System.out.println( "Parameter Count = " + pmData.getParameterCount() );
prepStmt.setDate( 1, sqlDate );
for( i = 0; i < locID.size(); i++ )
{
prepStmt.setString( ( i + 2 ), locID.get(i) );
}
rslt = prepStmt.executeQuery();
private PreparedStatement buildPreparedStatement(java.sql.Connection con)
throws SQLException
{
int i;
StringBuilder sb = new StringBuilder();
sb.append("SELECT AVG( ( High_Temperature + Low_Temperature ) / 2.0 ) ");
sb.append("FROM obs_masterAll ");
sb.append("WHERE Observation_Valid_Time = ? AND " );
sb.append("Location_ID IN ( " );
for( i = 0; i < ( locID.size() - 1 ); i++ )
{
sb.append( "?" ).append(", ");
}
sb.append( "?" ).append(" );");
System.out.println( sb.toString() );
return( con.prepareStatement( sb.toString() ) );
}
Is the problem my code, or the JDBC Driver?