0

I'm trying to convert a byte[] into a java.lang.Object[]. Basically it's the reverse problem discussed here, and is related to (but different from) my question on callableStatements in Scala.

Bottom line: I have a byte[] that represents a binary file. I need to pass it to a JDBC callableStatement.setObject() using createArrayOf("byte", objectArray) but I can't figure out how to transform my byte[] into an Object[].

This is what I have now... the getBytes() function returns the byte[] but this generates a compiler error, of course:

callableStatement.setObject(index, callableStatement.getConnection().createArrayOf("byte", getBytes()));

Unfortunately, this generates a compiler error:

SentimentDao.java:111: error: incompatible types: byte[] cannot be converted to Object[]

Also I'm not entirely sure that the first argument to createArrayOf() should be byte (if you know, please add that to your answer too).

Thank you – the help is much appreciated!

Community
  • 1
  • 1
Zaphod
  • 1,387
  • 2
  • 17
  • 33
  • 1
    Why do you need to convert it into an `Object[]`? Why can't you just call `callableStatement.setObject(index, getBytes())`? The second parameter of `setObject` is just of type `Object`, so it's not clear why you want to wrap it in an array. – Jon Skeet Jun 03 '15 at 05:56
  • Well, the main reason is the compiler error: `SentimentDao.java:111: error: incompatible types: byte[] cannot be converted to Object[]` – Zaphod Jun 03 '15 at 05:58
  • Well that *should* work fine - looking at the Javadocs for `CallableStatement`, there are *no* parameters of type `Object[]`... Is `callableStatement` really a variable of type `java.sql.CallableStatement`? – Jon Skeet Jun 03 '15 at 05:59
  • Well, it depends how you achieved this `byte[]`. If it is a serialized form of an `object[]`, then you can use the deserialize() method shown in http://stackoverflow.com/questions/3736058/java-object-to-byte-and-byte-to-object-converter-for-tokyo-cabinet, otherwise, I don't think you will be able to do it, unless you write your own deserializer. – Aakash Jun 03 '15 at 06:00
  • 1
    What's wrong with `PreparedStatement.setBytes()`? http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setBytes%28int,%20byte[]%29 –  Jun 03 '15 at 06:01
  • It is... however, we are using Postgresql/EDB, not Oracle. So there are bound to be odd little idiosyncrasies. This must be one of them. :( – Zaphod Jun 03 '15 at 06:01
  • I was using `setBytes()` originally, but sometimes it throws a database exception if the `byte[]` is empty. So trying to find a method that works. My guess was that it couldn't figure out what to do with an empty `byte[]` so I wanted to give it more "context" (by using `createArrayOf()`, etc). – Zaphod Jun 03 '15 at 06:03
  • Just do a length check on byte[]. if length = 0, fill the first slot of byte with 0 or 1. – Tschallacka Jun 03 '15 at 06:07
  • The compiler error you mention is because you try to create an (SQL) `ARRAY`: you shouldn't. You should be able to just call `setObject` with the original `byte[]`, without using `createArrayOf``. – Mark Rotteveel Jun 03 '15 at 06:26
  • To deal with an empty (or null array) you could use something like `if (bytes == null || bytes.length == 0) pstmt.setNull(1, Types.ARRAY); else pstmt.setBytes(1, theArray);` You need to adjust the `Types.ARRAY` to whatever the real JDBC type for that column is. –  Jun 03 '15 at 06:33
  • Unfortunately, it seems that the standard ("Oracle") way of doing things won't work with Postgres/EDB. Any attempt to set the `byte[]` parameter to NULL results in an exception. Passing a value with `setBytes()` results in an exception (as does `setObject()` by itself). The only way I've been able to get it to work is by using the rather convoluted approach above, using `createArrayOf()`... which is why I am trying to convert a Java `byte[]` into a Java `Object[]` (and having no luck thus far...) – Postgres/EDB is really frustrating me on this. – Zaphod Jun 03 '15 at 09:24

0 Answers0