IBM Support

Error -4461 : Invalid Data conversion in Type 4 JCC connection when using setObject() function

Technote (FAQ)


Question

Using Java:setObject function for character, string data types returns an error -4461 - Invalid data conversion, when using JCC Type 4 connection while the same succeeds when using a JCC Type 2 connection.

Cause

The reason this error is returned in Type 4 implementation of jdbc
driver and not in Type 2 is because, Type 4 JDBC driver is a pure Java
native driver that strictly follows the Java JDBC specifications,
whereas Type2 JDBC driver is a CLI based driver which is less strict
with data type conversions.

Here is a small test program to check and rectify the error :

<test program snip>
String url = "jdbc:db2://aaaaabbbbb.xxx.com:50000/Sample";
Connection con = DriverManager.getConnection(url,"userid","mypwd");
System.out.println("DB Sample Connected");
.
PreparedStatement pstmt =
con.prepareStatement("INSERT INTO table_name VALUES (?)");
// pstmt.setString (2, "P"); ===> SUCCESS
pstmt.setObject (2, new Character('P'), Types.CHAR ); ===> FAILs
pstmt.execute ();
}
catch (Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}

Execution of this program throws the following error :

Exception: com.ibm.db2.jcc.c.SqlException:
[jcc][1091][10417][3.64.96] Invalid data conversion: Parameter instance
P is invalid for the
requested conversion. ERRORCODE=-4461, SQLSTATE=42815
at com.ibm.db2.jcc.am.bd.a(bd.java:679)
at com.ibm.db2.jcc.am.bd.a(bd.java:60)
at com.ibm.db2.jcc.am.bd.a(bd.java:103)
at com.ibm.db2.jcc.am.ec.a(ec.java:1360)
at com.ibm.db2.jcc.am.ec.a(ec.java:1306)
at com.ibm.db2.jcc.am.jo.a(jo.java:2411)
at com.ibm.db2.jcc.am.jo.setObject(jo.java:2301)
at MAINCLASS.main(settest.java:18)

When pstmt.setObject is changed to pstmt.setString (commented line above the failing one), it inserts "P" successfully in the table.


Answer

The solution is to use the function setString in place of setObject wherever you are aware that input data is going to be a character or string.

Related information

Driver support for JDBC APIs

Document information

More support for: DB2 for Linux, UNIX and Windows
Programming Interface - JCC

Software version: 9.1, 9.5, 9.7, 9.8, 10.1

Operating system(s): AIX, HP-UX, Linux, Solaris, Windows

Reference #: 1622381

Modified date: 29 July 2013