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.");
严以律己、宽以待人