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.");

 

posted @ 2017-05-09 11:35  狂神314  阅读(863)  评论(0编辑  收藏  举报