2

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.

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, This Worked. But I am still facing -"Error msg: Operand type clash: nvarchar is incompatible with image SQLSTATE: S0002 Error code: 206 Error code: 0 " Issue.
Is the column data type in the underlying table nvarchar instead of varbinary? If so, the column needs to be changed to varbinary.
Only I have is access of DB, I am not able to see db structure. In this DB coloumn type is IMAGE.So what is compatible type for IMAGE in java code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.