转载地址:http://my.oschina.net/liuyuanyuangogo/blog/151537

pg用大对象存储二进制数据的老文档:http://jdbc.postgresql.org/documentation/80/binary-data.html


//VM配置:256M-512M

//通过lo_import(‘文件路径’)函数向oid字段插入二进制文件,通过(不会内存溢出)。

 /**
*
* @author Liu Yuanyuan
*/
private void insertOid()
{
String driver = "org.postgresql.Driver";//"com.highgo.jdbc.Driver";//192.168.100.125
String url = "jdbc:postgresql://" + "127.0.0.1" + ":" + "5866" + "/" + "db1";
Connection conn = null;
Statement stmt = null;
try
{
Class.forName(driver);
System.out.println("success find class");
conn = DriverManager.getConnection(url, "highgo", "hg");
System.out.println("success connect");
stmt = conn.createStatement();
//driectly insert
String f = "d:/1.jpg";
stmt = conn.prepareStatement("INSERT INTO oidtable VALUES (11, lo_import(\'"+f+"\'))");
//or by update
//String f = "d://2.jpg";
//PreparedStatement ps = conn.prepareStatement("update oidtable set obj = lo_import(\'"+f+"\') where id=?");
//ps.setInt(1,11);
ps.executeUpdate();
}
catch(Exception ex)
{
ex.printStackTrace(System.out);
}
finally
{
try
{
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace(System.out);
}
finally
{
System.out.println("finally");
}
}
}

//VM配置:256M-512M

//直接通过setLong()向oid插入1GB的文件,通过(2分钟之内插入完毕);

public void insertOid()
{
Connection conn = null;
PreparedStatement ps = null;
try
{
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://" + "127.0.0.1" + ":" + "5432" + "/" + "db1";
Class.forName(driver);
System.out.println("class");
conn = DriverManager.getConnection(url, "postgres", "pg");
System.out.println("connect");
// All LargeObject API calls must be within a transaction block
conn.setAutoCommit(false);
// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection) conn).getLargeObjectAPI();
// Create a new large object
long oid = lobj.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE);
// Open the large object for writing
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);
//Now open the file
File file = new File("d://1.jpg");
FileInputStream fis = new FileInputStream(file);
// Copy the data from the file to the large object
byte buf[] = new byte[2048];
int s, tl = 0;
while ((s = fis.read(buf, 0, 2048)) > 0)
{
obj.write(buf, 0, s);
tl += s;
}
// Close the large object
obj.close();
// Now insert the row into imageslo
ps = conn.prepareStatement("INSERT INTO lob.oidtable VALUES (?, ?)");
ps.setInt(1, 1);
ps.setLong(2, oid);
ps.executeUpdate();
fis.close();
// Finally, commit the transaction.
conn.commit();
conn.setAutoCommit(true);
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
finally
{
try
{
if (ps != null)
{
ps.close();
}
if(conn != null)
{
conn.close();
}
System.out.println("close all");
}
catch (SQLException ex)
{
ex.printStackTrace(System.out);
}
}
}

//VM配置:256M-512M

//直接通过getLong()从oid取出1GB的文件,通过(2分钟之内取出完毕);

public void getBinaryFile()
{
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try
{
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://" + "127.0.0.1" + ":" + "5866" + "/" + "db1";
Class.forName(driver);
System.out.println("class");
conn = DriverManager.getConnection(url, "highgo", "hg");
System.out.println("connect");
// All LargeObject API calls must be within a transaction block
conn.setAutoCommit(false);
// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection) conn).getLargeObjectAPI(); ps = conn.prepareStatement("SELECT obj FROM lob.oidtable WHERE id = ?");
ps.setInt(1, 1);
rs = ps.executeQuery();
while (rs.next())
{
// Open the large object for reading
long oid = rs.getLong(1);
LargeObject obj = lobj.open(oid, LargeObjectManager.READ);
// Read the data
// obj.read(buf, 0, obj.size());//its read method
// Do something with the data read here
//for example:load the file to disk
OutputStream ops = new FileOutputStream(new File("d:\\111.jpg"));
byte buf[] = new byte[1024];//当文件很大时,用obj.size()将内存溢出,所以可以自定义一个合适的值
for (int i; (i = obj.read(buf, 0,1024)) > 0;)
{
ops.write(buf, 0, i);
ops.flush();
}
// Close the object
obj.close();
ops.close();
}
// Finally, commit the transaction
conn.commit();
}
catch (Exception ex)
{
ex.printStackTrace(System.out);
}
finally
{
try
{
if (rs != null)
{
rs.close();
}
if (ps != null)
{
ps.close();
}
if(conn != null)
{
conn.close();
}
System.out.println("close all");
}
catch (SQLException ex)
{
ex.printStackTrace(System.out);
}
}
}

JAVA存取PG大对象类型OID数据的更多相关文章

  1. [原创]java WEB学习笔记81:Hibernate学习之路--- 对象关系映射文件(.hbm.xml):hibernate-mapping 节点,class节点,id节点(主键生成策略),property节点,在hibernate 中 java类型 与sql类型之间的对应关系,Java 时间和日期类型的映射,Java 大对象类型 的 映射 (了解),映射组成关系

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. Statement和PreparedStatement的特点 MySQL数据库分页 存取大对象 批处理 获取数据库主键值

    1 Statement和PreparedStatement的特点   a)对于创建和删除表或数据库,我们可以使用executeUpdate(),该方法返回0,表示未影向表中任何记录   b)对于创建和 ...

  3. hibernate 大对象类型hibernate制图

    基础知识: 在 Java 在, java.lang.String 它可以用来表示长串(超过长度 255), 字节数组 byte[] 可用于存放图片或文件的二进制数据. 此外, 在 JDBC API 中 ...

  4. hibernate 大对象类型的hibernate映射

    在 Java 中, java.lang.String 可用于表示长字符串(长度超过 255), 字节数组 byte[] 可用于存放图片或文件的二进制数据. 此外, 在 JDBC API 中还提供了 j ...

  5. oracle对大对象类型操作:blob,clob,nclob

     1.基本介绍 Oracle和plsql都支持lob(large object) 类型,用来存储大数量数据,如图像文件,声音文件等.Oracle 9i realse2支持存储最大为4g的数据,or ...

  6. Java操作Redis存储对象类型数据

    背景描述      关于JAVA去操作Redis时,如何存储一个对象的数据,大家是非常关心的问题,虽然官方提供了存储String,List,Set等等类型,但并不满足我们现在实际应用.存储一个对象是是 ...

  7. JAVA处理Blob大对象

    Blob对象是SQL Blob的Java语言映射.SQL Blob是一个内置类型,它可以将一个二进制大对象保存在数据库中.接口ResultSet.CallableStatement和PreparedS ...

  8. 【JSON 注解】JSON循环引用2----JSON注解@JsonIgnoreProperties+JAVA关键字transient+后台对象与JSON数据的格式互相转化

    接着来说这个JSON循环引用的问题: 关于JSON格式的转化,其实关键就是这几个依赖: <!-- json --> <!-- 1号 --> <dependency> ...

  9. java中Redis5大基本类型的用法

    存储格式 基本用法 通过Jedis(封装了redis的Java客户端)对redis进行操作. Jedis工具类 public class JedisPoolUtil { private static ...

随机推荐

  1. spring的依赖注入DI(IOC)

    1.手动注入 (1)set注入 public class UserService { private UserDao userDao; public void setUserDao(UserDao d ...

  2. Redhat6.x下如何制作虚拟机快照和镜像封装

    一.虚拟机快照 1.确认你的物理机上的vg还有足够的剩余空间 [root@hacker ~]# vgs  VG        #PV #LV #SN Attr   VSize  VFree   vg_ ...

  3. 宜家的幸福生活,源于K2 BPM的支撑

    很久很久以前,有一篇很火的文章在各大网站被疯狂转载<一个在北欧生活10年的MM,告诉你为什么北欧全球幸福指数第一>,开头第一段就已经让人羡慕嫉妒恨了. "下午的四.五点钟,北欧人 ...

  4. 使用Myeclipse创建自定义签名debug keystore

    1.在已经创建后的android项目上右击鼠标,如图所示 2.选择next下一步 3.选择create new keystore 注意  这里密码要输入android 4.点击next,录入基本信息 ...

  5. android listview getviewtypecount和getItemViewType

    package newdemo.jeno.listviewdemo; import android.app.Activity;import android.os.Bundle;import andro ...

  6. JAVA每日一记

    1.两个最基本的java回收算法:复制算法和标记清理算法                 复制算法:两个区域A和B,初始对象在A,继续存活的对象被转移到B.此为新生代最常用的算法            ...

  7. JSON.parse()和JSON.stringify() 的用法区别

    parse用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age":&qu ...

  8. 知道创宇研发技能表v2.2

    知道创宇研发技能表v2.2 2014/3/9 发布 by @知道创宇(www.knownsec.com) @余弦 & 行之 知道创宇是国内Geek十足且普遍被认为特别有前途的互联网安全公司, ...

  9. (转)IOS之Info.plist文件简介

    原文:IOS之Info.plist文件简介 http://www.apkbus.com/android-130240-1-1.html (出处: Android开发论坛 - 安卓开发论坛 - Andr ...

  10. Makefile总结和反序字符数组,整体右移数组,杨辉三角!

    2015.1.23今天我值日 继续接着昨天Makefile的目标开始记录:第一种:有 .PHNOY声明,但是冒号后面没有依赖文件.PHNOY:clean clean://没有依赖文件 rm *.0 t ...