利用blob类型存储图片
来源:互联网 发布:ftp服务器默认端口 编辑:程序博客网 时间:2024/05/14 21:27
SavePicture.java :
public class SavePicture {public static void main(String[] args) {Connection conn=null;PreparedStatement pstmt=null;String sql="insert into student(id,name,photo) values(?,?,?)";try {Class.forName("oracle.jdbc.driver.OracleDriver");conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");pstmt=conn.prepareStatement(sql);pstmt.setInt(1,1);pstmt.setString(2,"tom");//以下为重点部分File f=new File("src\\coder.jpg");FileInputStream fis=new FileInputStream(f);pstmt.setBinaryStream(3,fis,(int)f.length());int n=pstmt.executeUpdate();System.out.println(n+"条记录被插入");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();}finally{//关闭资源}}}
GetPicture.java :
public class GetPicture {public static void main(String[] args) {Connection conn=null;PreparedStatement pstmt=null;ResultSet rs=null;String sql="select id,name,photo from student where id=?";try {Class.forName("oracle.jdbc.driver.OracleDriver");conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");pstmt=conn.prepareStatement(sql);pstmt.setInt(1,1);rs=pstmt.executeQuery();if(rs.next()){//重点开始InputStream is=rs.getBinaryStream("photo");FileOutputStream fos=new FileOutputStream(new File("abc.jpg"));byte[] buffer=new byte[1024];int len=0;while((len=is.read(buffer))!=-1){fos.write(buffer,0,len);}fos.close();is.close();}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{//关闭资源}}}运行程序,刷新工程,得到:
可以看到abc.jpg和coder.jpg是相同的图片。
- 利用blob类型存储图片
- Oracle中读取&&存储blob类型存储的图片(待用)
- 利用Sqlite数据库的blob类型存储自定义的数据类型
- 利用Sqlite数据库的blob类型存储自定义的数据类型
- 利用ORACLE JAVA存储过程实现BLOB图片导出
- php 操作 MySQL 中的Blob类型 存储图片
- php 操作 MySQL 中的Blob类型 存储图片
- php 操作 MySQL 中的Blob类型 存储图片
- Blob存储读取图片
- MySQL存储 BLOB 类型数据
- 利用Sqlite数据库的blob类型存储自定义的数据类型(小寿转载)
- 利用sql将存储在 oracle blob中的图片等文件写出
- MySql Blob图片类型存储Bug解决:索引超出了数组界限错误
- Oracle 11g PL/SQL程序块,存储图片等BLOB字段类型的列PL/
- 利用Base64在XML中存储BLOB
- 存储过程中使用blob 类型
- oralce 中 blob类型的存储
- MySQL的BLOB类型存储大小调整
- uvc摄像头代码解析1
- android Application类
- oracle的导入导出遇到的问题
- Mac OS下不产生.DS_Store 隐藏文件
- 14.从零开始学习C语言--指针
- 利用blob类型存储图片
- mantis+testlink bug系统的整合配置
- 中联通陈一昕:运营商要把握大数据的仓库
- 解决root 用户无法切换到oracle用户
- iPhone开源项目汇总(更新版)
- PHP基础02-在页面中打印当前的文件名
- 6.对象的序列化以及对于多态,IO,异常等问题的复习
- struts2+hibernate+spring整合(annotation版)
- 开放-封闭原则