JAVA存取PG大对象类型OID数据
转载地址: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数据的更多相关文章
- [原创]java WEB学习笔记81:Hibernate学习之路--- 对象关系映射文件(.hbm.xml):hibernate-mapping 节点,class节点,id节点(主键生成策略),property节点,在hibernate 中 java类型 与sql类型之间的对应关系,Java 时间和日期类型的映射,Java 大对象类型 的 映射 (了解),映射组成关系
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Statement和PreparedStatement的特点 MySQL数据库分页 存取大对象 批处理 获取数据库主键值
1 Statement和PreparedStatement的特点 a)对于创建和删除表或数据库,我们可以使用executeUpdate(),该方法返回0,表示未影向表中任何记录 b)对于创建和 ...
- hibernate 大对象类型hibernate制图
基础知识: 在 Java 在, java.lang.String 它可以用来表示长串(超过长度 255), 字节数组 byte[] 可用于存放图片或文件的二进制数据. 此外, 在 JDBC API 中 ...
- hibernate 大对象类型的hibernate映射
在 Java 中, java.lang.String 可用于表示长字符串(长度超过 255), 字节数组 byte[] 可用于存放图片或文件的二进制数据. 此外, 在 JDBC API 中还提供了 j ...
- oracle对大对象类型操作:blob,clob,nclob
1.基本介绍 Oracle和plsql都支持lob(large object) 类型,用来存储大数量数据,如图像文件,声音文件等.Oracle 9i realse2支持存储最大为4g的数据,or ...
- Java操作Redis存储对象类型数据
背景描述 关于JAVA去操作Redis时,如何存储一个对象的数据,大家是非常关心的问题,虽然官方提供了存储String,List,Set等等类型,但并不满足我们现在实际应用.存储一个对象是是 ...
- JAVA处理Blob大对象
Blob对象是SQL Blob的Java语言映射.SQL Blob是一个内置类型,它可以将一个二进制大对象保存在数据库中.接口ResultSet.CallableStatement和PreparedS ...
- 【JSON 注解】JSON循环引用2----JSON注解@JsonIgnoreProperties+JAVA关键字transient+后台对象与JSON数据的格式互相转化
接着来说这个JSON循环引用的问题: 关于JSON格式的转化,其实关键就是这几个依赖: <!-- json --> <!-- 1号 --> <dependency> ...
- java中Redis5大基本类型的用法
存储格式 基本用法 通过Jedis(封装了redis的Java客户端)对redis进行操作. Jedis工具类 public class JedisPoolUtil { private static ...
随机推荐
- 经验分享:使用 Restyle.js 简化 CSS 预处理
Andrea Giammarchi的restyle.js是一个新的,基于JavaScript的CSS预处理器,能够运行在服务端(通过Node.js)或者浏览器中.它宣称自己是“一种简化的CSS方法”, ...
- 基于Storm的工程中使用log4j
最近使用Storm开发,发现log4j死活打不出debug级别的日志,网上搜到的关于log4j配置的方法都试过了,均无效. 最终发现问题是这样的:最新的storm使用的日志系统已经从log4j切换到了 ...
- ruby学习网站
Ruby官方中文网(推荐): https://www.ruby-lang.org/zh_cn/ 国内非常不错的Ruby学习教程网站(推荐): http://www.yiibai.com/ruby Ru ...
- 使用MediaPlayer播放音频-----之二
MediaPlayer播放不同来源的音频文件: 一.播放应用的资源文件 1.调用MediaPlayer的create(Context context , int resid)方法加载指定资源文件. ...
- java8Lambda详解
[移步] http://blog.csdn.net/ioriogami/article/details/12782141/
- ComboBox绑定
this.ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; this.ComboBox1.AutoCompleteSource ...
- Windows多线程编程总结
1 内核对象 1 .1 内核对象的概念 内核对象是内核分配的一个内存块,这种内存块是一个数据结构,表示内核对象的各种特征.并且只能由内核来访问.应用程序若需要访问内核对象,需要通过操作系统提供的函数来 ...
- 从ajax获取的数据无法通过Jquery选择器来调用事件
如果标签是动态生成的,比如说div.tr.td等,若需通过Jquery来获取事件,那么需要用live来绑定相应的事件. 比如说绑定div的click事件 $("div").live ...
- https://docs.mongodb.org/manual/reference/operator/aggregation/unwind/#examples
https://docs.mongodb.org/manual/reference/operator/aggregation/unwind/#examples http://www.clusterdb ...
- SelectedRows.CurrentRowSelected 和 DeleteItem
procedure TBMListEh.SetCurrentRowSelected(Value: Boolean); var Index: Integer; Current: TUniBookmark ...