首页 > 代码库 > PostgreSQL java读取bytes字段
PostgreSQL java读取bytes字段
写入bytea:
File img = new File("/tmp/eclipse.png"); fin = new FileInputStream(img); con = DriverManager.getConnection(url, user, password); pst = con.prepareStatement("INSERT INTO images(data) VALUES(?)"); pst.setBinaryStream(1, fin, (int) img.length()); pst.executeUpdate(); System.out.println("WriteImage.main() success.");
读取bytea:
String query = "SELECT data, LENGTH(data) FROM images WHERE id = 1"; pst = con.prepareStatement(query); ResultSet result = pst.executeQuery(); result.next(); //offset to the first row fos = new FileOutputStream("//home//apple//Pictures//eclipse.png"); int len = result.getInt(2); byte[] buf = result.getBytes("data"); fos.write(buf, 0, len); System.out.println("read image.main() success.");
PostgreSQL java读取bytes字段
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。