开发者

JDBC 4.0 - MS SQL 2005 - how to insert image from Java app?

开发者 https://www.devze.com 2023-03-14 10:48 出处:网络
I just downloaded the microsoft jdbc mssql driver to try connect my test app... The lib contains so many methods so I have confused开发者_运维问答 ... There is a BLOB and InputStream can be used but I

I just downloaded the microsoft jdbc mssql driver to try connect my test app... The lib contains so many methods so I have confused开发者_运维问答 ... There is a BLOB and InputStream can be used but I am not sure which one I need to insert FileInputStream for example? I couldn't find any tutorial showing that, moreover, I want to insert image with t-sql procedure... So my question is... How to insert FileInputStream to MS SQL Server 2005 table with Java client app as...

A) With no procedure B) With t-sql procedure

Any useful tutorial will be much appreciated


There's an example in the MSDN documentation for the JDBC driver, and a similar example here (found by Googling for "mssql jdbc insert image"). Both of these examples seem to rely on using setBinaryStream() on a PreparedStatement to pass a FileInputStream into a BLOB-type field field.

Third example below for reference:

PreparedStatement pstmt = con.prepareStatement("INSERT INTO BigTable (Col1) VALUES(?)");
FileInputStream inStream = new FileInputStream(new File("myImg.gif"));
pstmt.setBinaryStream(1, inStream);
pstmt.executeUpdate();
inStream.close();

In response to question B, I assume that it's possible to create a stored procedure with a BLOB parameter and then pass in an input stream using CallableStatement#setBinaryStream very similar to the above, e.g.,

CallableStatement cstmt = con.prepareCall("{AddImage(?)}");
FileInputStream inStream = new FileInputStream(new File("myImg.gif"));
cstmt.setBinaryStream(1, inStream);
cstmt.executeQuery();
inStream.close();
0

精彩评论

暂无评论...
验证码 换一张
取 消