public static void createPhoto(Connection conn,String sql) throws SQLException, IOException{
OutputStream outputStream=null;
ResultSet rs = null;
PreparedStatement ps = null;
try{
CreatPhoto cp = new CreatPhoto();
// 加载驱动程序
cp.loadJdbcDriver();
cp.connect();
sql = "select name,id,content,type from A";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
String s1 = "";
//将图片存放到本地的路径
String s = "E:\\photo";
while (rs.next()) {
System.out.println("名: " + rs.getString("name")+"."+rs.getString("type"));
String temp=rs.getString("name")+"_"+rs.getString("id")+"."+rs.getString("type");
Blob blob = rs.getBlob("content");
if(blob == null){
continue;
}
//验证图片路径是否存在,不存在就创建
File f = new File(s);
if(!f.exists()){
f.mkdirs();
}
s1 = s + "\\"+temp;
File file2 = new File(s1);
outputStream = new FileOutputStream(file2);
try {
//blob.getBytes的第一个参数是从第几个字节开始提取数据,第二个是提取字节的长度
outputStream.write(blob.getBytes(1, (int) blob.length()));
} catch (IOException e) {
e.printStackTrace();
}
}
outputStream.close();
cp.disConnect();
}catch(Exception e){
try{
if(outputStream!=null) outputStream.close();
if(ps!=null) ps.close();
if(rs!=null) rs.close();
if(conn!=null) conn.close();
}catch(Exception e1){
}
e.printStackTrace();
throw new IOException(e.getMessage());
}
}