Oracle存入图片和读取图片教程
程序员文章站
2024-01-20 10:55:58
Oracle存入图片和读取图片教程
package com.kevinb.image;
import java.io.File;
import java.io.Fil...
Oracle存入图片和读取图片教程
package com.kevinb.image; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import oracle.jdbc.driver.OracleBlobInputStream; import com.kevinb.sys.db.ConnectOracleBase; public class ProImage { /** database user*/ private static final String DBUSER = "scott"; /** database password*/ private static final String DBPSW = "ffffff"; /** database server*/ private static final String DBSERVER = "orcl"; /** * 向数据库中存入图片信息; * @param path */ @SuppressWarnings({ "static-access", "deprecation" }) public void insertImage(String path){ System.out.println("----insertImage"); Connection conn = null; PreparedStatement ps = null; conn = ConnectOracleBase.getConnection(this.DBUSER, this.DBPSW, this.DBSERVER); String sql = "insert into user_manager u (u.user_id,u.user_name,u.user_photo) " + "values(scott.hibernate_sequence.nextval,?,?)"; try { ps = conn.prepareStatement(sql); ps.setString(1, "Hello"); File file = new File(path); FileInputStream fStream = new FileInputStream(file); ps.setBinaryStream(2, fStream, (int) file.length()); ps.execute(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } finally{ try { ConnectOracleBase.closeAllConnection(ps, conn); } catch (SQLException e) { e.printStackTrace(); } } System.out.println("图片保存成功!"); } /** * 通过员工的empId取得图片信息; * @param userId */ @SuppressWarnings("static-access") public void getPhoto(int userId,String path){ System.out.println("----getPhoto"); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; conn = ConnectOracleBase.getConnection(this.DBUSER, this.DBPSW, this.DBSERVER); String sql = "select u.user_id,u.user_name,u.user_photo " + "from user_manager u " + "where u.user_id = ?"; try { ps = conn.prepareStatement(sql); ps.setInt(1, userId); rs = ps.executeQuery(); OracleBlobInputStream fStream = null; while(rs.next()){ fStream = (OracleBlobInputStream)rs.getBinaryStream("user_photo"); } FileOutputStream fOutputStream = new FileOutputStream(path); byte[] buffer = new byte[1024]; int byteRead = 0; while((byteRead = fStream.read(buffer)) != -1){ fOutputStream.write(buffer, 0, byteRead); } fOutputStream.close(); fStream.close(); } catch (SQLException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { ConnectOracleBase.closeAllConnection(rs, ps, conn); } catch (SQLException e) { e.printStackTrace(); } } System.out.println("get photo is success!"); } }
说明:ConnectOracleBase,是我自己封装的连库操作类;
推荐阅读
-
php读取和保存base64编码的图片
-
java读取xslx内容,内容转换成docx和pdf,包括图片
-
php ob_start()实现图片存入变量程序_PHP教程
-
两个由于php.ini配置错误导致的报错:ajax图片上传报错和exec报错,_PHP教程
-
ajax实现上传图片保存到后台并读取的实例(图文教程)
-
C#操作图片读取和存储SQLserver实现代码
-
Linux上安装Python的PIL和Pillow库处理图片的实例教程
-
C#操作图片读取和存储SQLserver实现代码
-
Linux上安装Python的PIL和Pillow库处理图片的实例教程
-
使用HTML5 Canvas为图片填充颜色和纹理的教程