I have come across the following codes i feel it is doing the wrong thing:
(Note that this is JDK 1.4.2, therefore the list is not typed)
StringBuffe
This didn't help me. Below is the query built after appending the bind variables. select ACC_NO from ACC_TABLE where ACC_NAME='java.lang.String';
It is trying to convert to java.lang.String type and which results in the following exception java.sql.SQLException: Could not execute sql command - Original message: null
Where as my ACC_NAME is 'user01'. So actually the query should be some thing like this, select ACC_NO from ACC_TABLE where ACC_NAME='user01';
So if my understanding is not wrong, preparedStatement.setObject(index, object) is converting the data to its respective data type and setting it.
preparedStatement.setObject(index, object) in MySQL is working perfectly alright with no issues. Only problem is while using Oracle. Oracle DB version i am working with is
Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production PL/SQL Release 11.2.0.2.0 - Production "CORE 11.2.0.2.0 Production" TNS for 32-bit Windows: Version 11.2.0.2.0 - Production NLSRTL Version 11.2.0.2.0 - Production
Consider using the PreparedStatement setObject() method instead of setString().
The PreparedStatement setObject() will attempt to convert any of the java.lang types for you if the type is unknown at compile time.
so with an updated for loop (assuming you have java 5.0) and generic null handling:
int i = 0;
for(Object value : temp) {
if (value == null) {
// set null parameter if value type is null and type is unknown
pstmt.setNull(++i, Integer.MIN_VALUE);
} else {
pstmt.setObject(++i, value);
}
}
Note that setNull() can accept a type as the 2nd parameter if it is known.