public class TestBlob {
//存储二进制
@Test
public void cunchu()throws Exception{
//获取连接对象
Connection conn = JdbcUtils2.getConn();
//获取执行对象 alt+shift+L
PreparedStatement pst = conn.prepareStatement("insert into tb1(image) values (?)");
//设置参数
FileInputStream in = new FileInputStream("C:\\Users\\sunqingchen\\Desktop\\新建文件夹\\b.jpg");
pst.setBlob(1, in);
//执行更新
int count = pst.executeUpdate();
System.out.println(count+"--->行受影响");
//关流
JdbcUtils2.close(null, pst, conn);
}
//读取二进制
@Test
public void duqu()throws Exception{
//获取连接对象
Connection conn = JdbcUtils2.getConn();
//获取执行对象 alt+shift+L
PreparedStatement pst = conn.prepareStatement("select image from tb1 where book is null");
//执行更新
ResultSet rs = pst.executeQuery();
rs.next();
Blob blob = rs.getBlob(1);
InputStream bs = blob.getBinaryStream();
//写到某个文件中即可
FileOutputStream fos = new FileOutputStream("C:\\Users\\sunqingchen\\Desktop\\新建文件夹\\c.jpg");
int n = -1;
while((n = bs.read()) != -1) {
//调用输出流写出去
fos.write(n);
}
//关流
fos.flush();
fos.close();
bs.close();
JdbcUtils2.close(null, pst, conn);
}
}
jdbc操作二进制文件
最新推荐文章于 2022-02-09 11:37:21 发布