源代码: //读取mysql中的image
@Test
public void testImageOutput(){
Connection con=null;
PreparedStatement ppstat=null;
ResultSet rs=null;
String sql="SELECT * FROM test where id=9";

try {
//链接数据库
con=JdbcUtil.getConnection();
//执行预编译
ppstat=con.prepareStatement(sql);
//获取结果集合
rs=ppstat.executeQuery(); //遍历结果集合
if(rs.next()){
//获取图片资源
InputStream in=rs.getBinaryStream("image");
//图片输出流
FileOutputStream out=new FileOutputStream(new File("h://240.jpg"));

int len=-1;
//定义一个缓冲区
byte b[]=new byte[1024];
len=in.read();
//把流写出到缓冲区
while(len!=-1){//读完返回-1
//写到磁盘
out.write(b, 0, len);
}
if(out!=null){
out.close();
out=null;
}
if(in!=null){
in.close();
in=null;
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JdbcUtil.close(con, ppstat, rs);
}
}