Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PreparedStatement setObject Woes

 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to use the setObject() method in a PreparedStatement with SQL Server 2000. But I get a variety of errors:
1)
con.prepareStatement("INSERT INTO TESTTABLE (IMAGECOL) VALUES (?)");
stmt.setObject(1, "test"); //SQLException: nvarchar is incompatible with image
2) con.prepareStatement("INSERT INTO TESTTABLE (IMAGECOL) VALUES (?)");
stmt.setObject(1, new Serializable() {String t="test";} ); //SQLException: Driver does not support this function. setObject(1,class test.jdbc.Test$1)
Imagecol has a data type of image.
I've tried these tests with i-net gate and Microsoft's JDBC driver.
Incidentally, the reason I'm concerned about this is that I'm trying to attach SQL Server 2000 to JBoss 3.2.1 and these are the errors I have to deal with.
Is it because the drivers don't support the setObject method very well?
Any thoughts?
Yoo-Jin
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yoo-Jin
It looks like there is a bit of confusion what setObject() is for. The setObject() method cannot handle arbitrary Java objects, and does not support serialization. Broadly speaking, you can use it for two categories of input:
  • It can take anything the type-specific setter methods can take. The JDBC driver will do its best to infer the SQL type from the object type. For example, setObject(1, timestamp) is equivalent to setTimestamp(1, timestamp) assuming that timestamp is a java.sql.Timestamp. You might want to use setObject() in this way if you have some generic database-access code where you don't know what the precise type of your arguments is.
  • More interestingly, you can define custom mappings from your Java objects to database object types, so you don't have to use Struct. You would need to create an SQLData in the connection's type map to achieve this.
  • However, I suspect this is not what you want to do. I am not sure what the SQL Server IMAGE type maps to, but I will assume it maps to the JDBC BINARY type (you'd need to check this). What you probably want to do is turn your image into a GIF or JPG and useTo insert it into the database. You can also serialize your object, but in that case you should probably use a BINARY database field rather than an IMAGE field:Hope this helps,
    - Peter
    [ June 26, 2003: Message edited by: Peter den Haan ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic