PostgreSQL java读取bytes字段
Posted 狂神314
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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字段的主要内容,如果未能解决你的问题,请参考以下文章
Python读取cvs文件报错:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 6(代