postgresql java类型_JAVA存取PostgreSQL大对象类型oid
//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相关推荐
- JAVA 【引用类型】和【对象类型】在【继承】中的异同
介绍 JAVA [引用类型]和[对象类型]在[继承]中的异同.这个问题自己整理过N次.也被人当菜鸟问过N次.所以,在此简单整理一下.以供大家分享. 在继承关系中.一般成员变量是依据引用类型 在继承关系 ...
- Java大对象类型的Hibernate映射
在 Java 中, java.lang.String 可用于表示长字符串(长度超过 255 ),字节数组 byte[] 可以用于存放图片户或文件二进制数据.此外,在 JDBC API 中还提供了 ja ...
- oracle对大对象类型操作:blob,clob,nclob,bfile
在oracle中,有4个大对象(lobs)类型可用,分别是blob,clob,bfile,nclob.下面是对lob数据类型的简单介绍.blob:二进制lob,为二进制数据,最长可达4GB,存贮在数据 ...
- oracle 对象类型是什么意思,Oracle对象类型 (转)
Oracle对象类型也有属性和方法. 创建对象类型与创建表很相似,只是实际上不为存储的数据分配空间: 不带方法的简单对象类型: CREATE TYPE type_name as OBJECT ( co ...
- js对象是什么?js对象类型有哪些?js对象类型的总结
对象是需求场景中的名词(如人.事.物)在程序中的表示 JavaScript中,除了string.number.Boolean.null.undefined之外,其他的数据都是对象,如数组.日期甚至函数 ...
- python映射类型是什么意思_Python对象类型
Python对象类型 2019-02-04 蘭喆 蘭喆的生活 问题1:Python知识结构? 答:1.程序由模块构成:2.模块包含语句:3.语句包含表达式:4.表达式创建并处理对象. 问题2:Pyth ...
- java 难度_java中难度大一点的面试题
1.请大概描述一下Vector和ArrayList的区别,Hashtable和HashMap的区别.(5) (1)Vector和ArrayList的异同 实现原理,功能相同,可以互用 主要区别: Ve ...
- java程序设计_Java程序设计-类和对象(笔记)
1)类(Class)和对象(Object)是面向对象的核心概念. 类是对一类事物的描述,是抽象的.概念上的定义 对象是实际存在的该类事物的每个个体,因而也称为实例(instance). 2)" ...
- java中几种常用的对象类型(po,vo,bo,dto)
PO(persistant object) 1.持久对象 在o/r映射的时候出现的概念,如果没有o/r映射,没有这个概念存在了. 2.通常对应数据模型(数据库),本身还有部分业务逻辑的处理.可以看 ...
最新文章
- js 控制选中文字
- 微博客之后有可能是“切客”
- Asymptote 学习记录(2):例子阅读
- 苹果cms V8模板 价值200RMB时尚大气功能超强模板
- SkyDNS2源码分析
- [Liferay] Liferay 实现单点登录 - OpenLDAP
- iOS 工程中引入另一个工程,多工程管理
- DatabaseMetaData的用法(转)
- html 中shadow DOM 的使用
- Altium Designer使用者,你想要一键出Gerber吗?
- Gradle全版本资源下载
- 弘辽科技:多多进宝怎么找团长?多多进宝怎么找推手推广?
- 二进制,八进制,十进制,十六进制的详解
- html爱心特效代码教程
- Hadoop(八)网站流量分析
- 杂谈——每日热量消耗
- knife-4j 点击列表出现空白页怎么办?
- 客户端开发 Windows驱动开发(1)SDK WDK DDK WDM的关系
- 北美电影票房Top10-2019年12月20日:《星战9》1.77亿不及预期
- Flash视频播放器flowplayer的使用