//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.

mit();

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

mit();

}

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

}

}

}

postgresql java类型_JAVA存取PostgreSQL大对象类型oid相关推荐

  1. JAVA 【引用类型】和【对象类型】在【继承】中的异同

    介绍 JAVA [引用类型]和[对象类型]在[继承]中的异同.这个问题自己整理过N次.也被人当菜鸟问过N次.所以,在此简单整理一下.以供大家分享. 在继承关系中.一般成员变量是依据引用类型 在继承关系 ...

  2. Java大对象类型的Hibernate映射

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

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

    在oracle中,有4个大对象(lobs)类型可用,分别是blob,clob,bfile,nclob.下面是对lob数据类型的简单介绍.blob:二进制lob,为二进制数据,最长可达4GB,存贮在数据 ...

  4. oracle 对象类型是什么意思,Oracle对象类型 (转)

    Oracle对象类型也有属性和方法. 创建对象类型与创建表很相似,只是实际上不为存储的数据分配空间: 不带方法的简单对象类型: CREATE TYPE type_name as OBJECT ( co ...

  5. js对象是什么?js对象类型有哪些?js对象类型的总结

    对象是需求场景中的名词(如人.事.物)在程序中的表示 JavaScript中,除了string.number.Boolean.null.undefined之外,其他的数据都是对象,如数组.日期甚至函数 ...

  6. python映射类型是什么意思_Python对象类型

    Python对象类型 2019-02-04 蘭喆 蘭喆的生活 问题1:Python知识结构? 答:1.程序由模块构成:2.模块包含语句:3.语句包含表达式:4.表达式创建并处理对象. 问题2:Pyth ...

  7. java 难度_java中难度大一点的面试题

    1.请大概描述一下Vector和ArrayList的区别,Hashtable和HashMap的区别.(5) (1)Vector和ArrayList的异同 实现原理,功能相同,可以互用 主要区别: Ve ...

  8. java程序设计_Java程序设计-类和对象(笔记)

    1)类(Class)和对象(Object)是面向对象的核心概念. 类是对一类事物的描述,是抽象的.概念上的定义 对象是实际存在的该类事物的每个个体,因而也称为实例(instance). 2)" ...

  9. java中几种常用的对象类型(po,vo,bo,dto)

    PO(persistant object) 1.持久对象 在o/r映射的时候出现的概念,如果没有o/r映射,没有这个概念存在了.   2.通常对应数据模型(数据库),本身还有部分业务逻辑的处理.可以看 ...

最新文章

  1. 【每日亿题#12】AtCoder Grand Contest 021 (A ~ F)全部题解
  2. 微博达人硅谷之歌:Testin云測移动搜索性能測试非常是让人信服
  3. SpannableString 设置一段文字中部分字体颜色
  4. 企业网络推广浅析外包企业网络推广如何有效布局关键词优化?
  5. DeepLab v2
  6. Qt for Android / ios 将图片或文件打包进安装包中
  7. Java继承、重写与重载 笔记
  8. MFC中获取命令行参数的几种方法
  9. 优化了破网站的搜索功能
  10. Downie 4 for Mac(视频下载)
  11. oracle 批量插入 mysql 区别,Mysql与Oracle中批量插入和更新区别
  12. Bootstrap基础九辅助类
  13. 长尾关键词是什么意思?如何使用5118挖掘和下载长尾词?
  14. u盘数据恢复的原理_U盘数据恢复工具 U盘常见故障及使用保养
  15. RPM 包的构建 - SPEC 基础知识-01
  16. 还在原地踏步,提高软件测试能力的方法你知道吗?
  17. Win11任务栏太宽了怎么办?教你一招快速修改任务栏大小
  18. Qt编写物联网管理平台17-记录清理
  19. 计算机在职英语,我是社会在职人员,能考什么样的英语?
  20. Error relaunching VirtualBox VM process:5错误解决

热门文章

  1. 在ListView控件中绘底图
  2. linux apache tomcat php 共用 80,apache与tomcat共用80端口
  3. bupt summer training for 16 #5 ——数据结构
  4. 连接目标数据库+无恢复目录连接目标数据库+使用有恢复目录连接目标数据库+注册数据库+目录同步+取消目标数据库的连接...
  5. Redis之Ubuntu开机启动
  6. 在后台中高效工作 – 后台任务
  7. CentOS7下解决yum install mysql-server没有可用包的问题
  8. 耳目一新的在线答疑服务背后的核心技术
  9. HTML与XHTML区别
  10. php软件开发--linux进阶