码迷,mamicode.com
首页 > 数据库 > 详细

PostgreSQL java读取bytes字段

时间:2017-05-09 12:40:15      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:eclips   rest   output   manager   pre   get   length   data   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字段

标签:eclips   rest   output   manager   pre   get   length   data   bytes   

原文地址:http://www.cnblogs.com/kuang17/p/6829810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!