zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java读取oracle数据库中blob字段详解编程语言

JAVAOracle数据库编程语言 详解 读取 字段 blob
2023-06-13 09:20:41 时间
public static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"; public static final String USERNAME = "test"; public static final String PASSWORD = "test"; static { try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); /** * @param args public static void main(String[] args) { if (download()) { System.out.println("图片下载成功"); } else { System.out.println("图片下载失败"); public static boolean download() { Connection conn = getConnection(); Statement sta = null; ResultSet rs = null; try { sta = conn.createStatement(); // image字段为BLOG字段 String sql = "Select name, image from test_img where id=1"; rs = sta.executeQuery(sql); while (rs.next()) { // 输出文件名 String name = rs.getString("name"); oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("image"); String filepath = "D:/" + name + ".jpg"; System.out.println("输出文件路径为:" + filepath); InputStream input = blob.getBinaryStream(); FileOutputStream out = new FileOutputStream(filepath); int len = (int) blob.length(); byte buffer[] = new byte[len]; while ((len = input.read(buffer)) != -1) { out.write(buffer, 0, len); out.close(); input.close(); } catch (SQLException e) { e.printStackTrace(); return false; } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } finally { try { rs.close(); sta.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); return false; return true; /** * 获得Connection public static Connection getConnection() { Connection conn = null; try { conn = DriverManager.getConnection(URL, USERNAME, PASSWORD); } catch (SQLException e) { e.printStackTrace(); return conn; }

存入图片Blob参见:Oracle JDBC存入图片Blob

作者:blog.ytso.com

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/14309.html

cjavaoracle