Java result set getblob and getobject
I am having problem writing / reading BufferedImage
java dataType here . When writing, it seems to have no problem with the inserted data, but when reading, I have a problem using the getObject method from the java ResultSet (for some reason there must be a method getObject
, so I can read any data types and convert them after). When I insert BufferedImage dataType into mysql, I use inputStream and it is 42559 in size by the length of the byte array, but I read it using the getObject
length is not 42559, but its size turned to 42586, so I cannot read it back using the " ImageIO.read "(it returns null)
Here is my embed code:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write((BufferedImage) vals[i], "jpg", baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
ps.setBinaryStream(i + 1, is);
Here is my code:
Object set = rs.getObject(i + 1);
And here is my envelope to BufferedImage code:
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(b);
o.writeObject(set);
o.flush();
byte res[] = b.toByteArray();
o.close();
b.close();
System.out.println("Cek variabel: " + set.getClass() + "-" + res.toString() + "-" + res.length);
Blob blob = KubraLib.Conn.ConnSelect.getMySQLConn().createBlob();
blob.setBytes(1, res);
System.out.println("Cek variabel: " + blob.toString());
InputStream is = blob.getBinaryStream();
System.out.println("Cek variabel: " + is.toString());
bi = ImageIO.read(is);
System.out.println("Cek hasil: " + bi.toString()); <-- Returns java.lang.NullPointerException
is.close();
Any help would be appreciated :). And im relatively new with stack overflow, if there is any mistake in this post i am sorry :))
source to share
No one has answered this question yet
Check out similar questions: