package com.lyon.hibernate.blob;
import java.io.*;
import java.sql.*;
import com.lyon.hibernate.dao.IPrintDao;
import com.lyon.hibernate.pojo.Print;
import com.lyon.hibernate.util.BeanFactory;
import oracle.sql.BLOB;
/**
*
* 功 能:向 分发 数据库 (update) 新增blob
* 时 间:2017年12月28日上午10:52:18
*@author
*/
public class saveXmlBlob {
public void addBlob(String policyCode) throws SQLException {
/**调用printDao接口*/
IPrintDao printDao=(IPrintDao) BeanFactory.getBean("printDao");
Connection con = null;
long start = System.currentTimeMillis(); // count runtime
/**
* 通过存储过程更新数据库
*/
CallableStatement pstmt = null;
InputStream fin = null;
OutputStream outStream = null;
/**文件所在地,将其写入到数据库*/
String path = "D:\\test.xml";
File file = new File(path);
/**得到打印的对象,通过对象的得到Id*/
Print p=printDao.getPrintCode(policyCode);
try {
con = DBConnection.getDBConnection();
con.setAutoCommit(false);
String sql = "update TAB_PUBLISH set XMLBLOB=? where PUBLISHID=?";
pstmt = con.prepareCall(sql);
Blob xmlblob = BLOB.empty_lob();
pstmt.setBlob(1, xmlblob);
pstmt.setInt(2, p.getPrinId());
pstmt.execute();
System.out.print("instert ok\n");
Statement stmt = con.createStatement();
/** 调用存储过程,将文件写入到数据库 */
ResultSet rs = stmt.executeQuery("select xmlblob from TAB_PUBLISH for update");
while (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("xmlblob");
outStream = blob.getBinaryOutputStream();
fin = new FileInputStream(file); // put file into stream
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
}
/*fin.close();
outStream.flush();
outStream.close();*/
}
System.out.print("\nupdate ok\n");
con.commit();
}
catch (Exception e) {
e.printStackTrace();
}finally {
try {
fin.close();
outStream.flush();
outStream.close();
con.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//con.close();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
package com.lyon.hibernate.blob;
import java.io.*;
import java.sql.*;
import oracle.sql.BLOB;
/***
* 描 述:(无实际意义,仅仅用作参考)
* 功 能:新增blob 到 TAB_BLOB_TEST表的demo
* 时 间:2017年12月28日上午10:56:42
*@author
*/
public class bolb_test {
public void addBlob() throws SQLException {
Connection con = null;
long start = System.currentTimeMillis(); // count runtime
CallableStatement pstmt = null;
InputStream fin = null;
OutputStream outStream = null;
String path = "D:\\test.xml";
File file = new File(path);
try {
con = DBConnection.getDBConnection();
con.setAutoCommit(false);
String sql = "insert into TAB_BLOB_TEST values(?,?,?)";
// insert database
pstmt = con.prepareCall(sql);
for (int i = 0; i < 10; i++)
{
String name = "Young_" + i;
int id = 1;
pstmt.setInt(1, id + i);
pstmt.setString(2, name);
Blob word = BLOB.empty_lob();
pstmt.setBlob(3, word);
pstmt.execute();
}
System.out.print("instert ok\n");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select id,word from TAB_BLOB_TEST for update");
// get specially columns and rows for update
while (rs.next()) {
// System.out.print(rs.getInt(1)+rs.getString(2)+"\n");//print select sql for
// debug
BLOB blob = (BLOB) rs.getBlob("word");
outStream = blob.getBinaryOutputStream();
fin = new FileInputStream(file); // put file into stream
byte[] b = new byte[blob.getBufferSize()];
int len = 0;
while ((len = fin.read(b)) != -1) {
outStream.write(b, 0, len);
}
fin.close();
outStream.flush();
outStream.close();
}
System.out.print("\nupdate ok\n");
con.commit();
}
catch (Exception e) {
e.printStackTrace();
}
con.close();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
package com.lyon.hibernate.blob;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.lyon.hibernate.dao.IPrintDao;
import com.lyon.hibernate.dao.ISupPrintDao;
import com.lyon.hibernate.dao.impl.PrintDaoImpl;
import com.lyon.hibernate.dao.impl.SupPrintDaoImpl;
import com.lyon.hibernate.pojo.Print;
import com.mysql.jdbc.PreparedStatement;
import oracle.sql.BLOB;
/**
*
* 功 能: orcal的blob形式,通过二进制流下载xml文件
* 时 间:2017年12月28日上午10:48:08
*@author
*/
public class getbolb_test1 {
public void getXml(String policyCode) throws SQLException{
Connection con = null;
long start = System.currentTimeMillis(); // count runtime
/**
* 通过打印单编号查询得到对象,
* 把所需要的xml下载到相应的文件夹下
* 文件名是查询出来的 如:p.getXmlBlob()
* */
IPrintDao pd=new PrintDaoImpl();
Print p=pd.getPrintCode(policyCode);
String path = "E:\\eclipsedemo-workspace\\HibernateDemo2\\xml文件\\"+p.getXmlBlob()+"";
File file = new File(path);
try {
con = DBConnection.getDBConnection();
con.setAutoCommit(false);
Statement stmt = con.createStatement();
String hql="select id,word from tab_blob_test where id="+p.getPrinId()+"";
ResultSet rs = stmt.executeQuery(hql);
if (rs.next()) {
BLOB blob = (BLOB) rs.getBlob("word");
/** 通过流将文件写出*/
FileOutputStream output = new FileOutputStream(file);
InputStream input = blob.getBinaryStream();// put blob into input
byte[] buffer = new byte[blob.getBufferSize()];
int len = 0;
while ((len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
input.close();
output.flush();
output.close();
}
System.out.print("\ndownload ok\n");
}
}
catch (Exception e) {
e.printStackTrace();
}
con.close();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
参考地址:https://www.cnblogs.com/tobecrazy/archive/2012/12/22/2828734.html
Orcal Blob 更新 新增 下载 数据库数据(update)
猜你喜欢
转载自blog.csdn.net/qq_15007327/article/details/78921183
今日推荐
周排行