您的位置:首页 > 其它

JDBC读取BLOB类型

2015-07-20 14:39 302 查看
public
String getBlob(String SQL){


Connection conn =
null
;


PreparedStatement stmt =
null
;


ResultSet rs =
null
;


try
{


conn = dataSource.getConnection();
//c3p0连接池


stmt = conn.prepareStatement(SQL);
//SQL: select info from table1 (其中info字段是blob类型)


rs = stmt.executeQuery();




InputStream in = rs.getBinaryStream(
1
);


ByteArrayOutputStream outStream =
new
ByteArrayOutputStream();


byte
[] data =
new
byte
[
4096
];


int
count = -
1
;


while
((count = in.read(data,
0
,
4096
)) != -
1
)


outStream.write(data,
0
, count);




data =
null
;


String result =
new
String(outStream.toByteArray(),
"utf-8"
);


}
catch
(SQLException sqle) {


log.warn(
"Exception XXX"
, sqle);


}
finally
{


conn.close();


stmt.close();


}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: