转载地址: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. placeholder修改颜色

    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #f00; } ::-moz-placeholder { /* Mozilla Fir ...

  2. Eclipse导出可执行Jar文件(包含第三方Jar包)

    1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所示: 2. 选择Runnable JAR file后,会弹出如下所示的对话框,选择 ...

  3. 查看Nginx、apache、MySQL和PHP的编译参数

    1.nginx编译参数:#/usr/local/nginx/sbin/nginx -V2.apache编译参数:# cat /usr/local/apache/build/config.nice3.p ...

  4. flash as3 socket安全服务网关(socket policy file server)

    关键字: SecurityErrorEvent socket as3 flash有着自己的一套安全处理模式,在socket方面,我这样的菜鸟无法理解他的好处:一句话,不怀好意的人如果想用flash写一 ...

  5. closeChrome

    function closeChrome(){ var browserName=navigator.appName; if (browserName=="Netscape") { ...

  6. mongoose深层修改问题

    https://cnodejs.org/topic/50dde64ea7e6c6171a80a678 各位大神好,好久没写点什么东西了,最近也是cnode社区不知道咋的了都登录不进去,今天总算能回到这 ...

  7. [开发笔记]-“在引用COM组件时,出现了无法嵌入互操作类型。。。”的错误

    这两天在做一个需要将wps文档转换成word文档的程序,在调用wps的com组件时项目编译是没有问题的,但当运行的时候却弹出了下面的错误提示: 从网上百度一番后,找到了正确的解决方法. 先从Com组件 ...

  8. iOS开发之单例设计模式(完整正确版本)

    单例的意思从字面上就可以略知一二,所谓单例就是确保在程序运行过程中只创建一个对象实例.可以用于需要被多次广泛或者说多次使用的资源中,比如我们常见的网络请求类.工具类以及其它管理类等.比如我iOS开发中 ...

  9. 基于反射的通过set方法的依赖注入,可以看成一种设计模式,自己来用

    非常好用,在properties文件中配置字符串和类名之间的对应,在程序里读取文件,找到类名,通过反射,达到调用set方法的目的,然后直接将自己的指向其他类的对象的引用赋值,指向实体对象. 比如use ...

  10. iframe和form表单的target应用简单例子

    iframe和form表单的target属性   Problem: 刷新主页面中的其中一个iframe,其他内容不变 Solution: main.jsp <body onload=" ...