JDBC读取BLOB类型
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();
}
}