JDBC学习笔记(四)二进制数据读写

一、插入二进制数据

File f = new File("D:\\BaiduNetdiskDownload\\JDBC\\111.png");
InputStream in = new BufferedInputStream(new FileInputStream(f));
ps.setBinaryStream(1, in, f.length());  -- ps为PreparedStatement对象
int i = ps.executeUpdate();


二、读取二进制数据

// 获取输入流还有一种方式

// Blob blob = rs.getBlob("big_blob");
// InputStream in = blob.getBinaryStream();
InputStream in = rs.getBinaryStream("big_blob"); File f = new File("D:\\BaiduNetdiskDownload\\JDBC\\111.bak.png");
FileOutputStream out
= new FileOutputStream(f);
byte[] buff = new byte[1024];
int len = 0;
while ((len = in.read(buff)) != -1)
{ out.write(buff,
0, len);
}

 

posted @ 2017-02-16 20:16  Java_xb  阅读(560)  评论(0编辑  收藏  举报