Reputation: 542
I am trying to save Image at SQL-SERVER Database using procedures .I have a PROCEDURE name with input parameters but I don't have procedure syntax.
BufferedImage imm = ImageIO.read(new File("C:\\MY DATA\\Release 2\\18.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(imm, "jpg", baos );
baos.flush();
byte[] immAsBytes = baos.toByteArray();
baos.close();
con.setAutoCommit(true);
CallableStatement statement = con.prepareCall(query);
ByteArrayInputStream baisForIndex2 = new ByteArrayInputStream(immAsBytes);
ByteArrayInputStream baisForIndex3 = new ByteArrayInputStream(immAsBytes);
statement.setDate(1, sqlDate);
statement.setBinaryStream(2, baisForIndex2, immAsBytes.length);
statement.setBinaryStream(3, baisForIndex3, immAsBytes.length);
statement.executeUpdate();
Error msg: Operand type clash: nvarchar is incompatible with image. SQLSTATE: S0002 Error code: 206 Error code: 0
What I want is compatible type of Java with IMAGE type of DB of sql-server.
Upvotes: 2
Views: 4528
Reputation: 312289
When inserting a stream into a blob, the JDBC driver will read the specified length from it and will not reset the the stream when done. In your example, you use this stream for placeholder 2, and then again for placeholder 3 and specify the entire length of the underlying byte[] each time. This way, when the driver gets to placeholder 3, the stream is exhausted, and cannot be read.
One solution could be to use two stream objects:
CallableStatement statement = con.prepareCall(query);
ByteArrayInputStream baisForIndex2 = new ByteArrayInputStream(immAsBytes);
ByteArrayInputStream baisForIndex3 = new ByteArrayInputStream(immAsBytes);
statement.setDate(1, sqlDate);
statement.setBinaryStream(2, baisForIndex2, immAsBytes.length);
statement.setBinaryStream(3, baisForIndex3, immAsBytes.length);
statement.executeUpdate();
Upvotes: 2