mysql 图片的存取
程序员文章站
2022-03-29 10:03:06
...
http://www.cnblogs.com/xudong-bupt/p/3758529.html
import java.io.*; import java.sql.*; public class LoadStoreBLOB { public static void main(String[] args) { DBConnection DB = new DBConnection(); //负责连接MySQl数据库的类 Connection con = null; PreparedStatement ps = null; ResultSet rs = null; InputStream in = null; try { //从本地硬盘硬盘读取一张图片保存到数据库 con=DB.getConn(); in=new FileInputStream("sdf.png"); ps=con.prepareStatement("insert into test.phototest values(?,?)"); ps.setInt(1,2); ps.setBinaryStream(2, in, in.available()); ps.executeUpdate(); in.close(); DB.closeConn(con); //从数据库读取图片保存到本地硬盘 con=DB.getConn(); ps=con.prepareStatement("select * from test.phototest where id=?"); ps.setInt(1,2); rs=ps.executeQuery(); rs.next(); //将光标指向第一行 in=rs.getBinaryStream("photo"); byte[] b=new byte[in.available()]; //新建保存图片数据的byte数组 in.read(b); OutputStream out=new FileOutputStream("222.jpg"); out.write(b); out.flush(); out.close(); DB.closeConn(con); } catch (Exception e) { System.out.println("Error::"+e); } } } http://blog.sina.com.cn/s/blog_6a9d14ec0100l8y1.html