Java-Blob-取出来插入另一个数据库[导库程序]

//注意

//[数据源-content字段是Blob类型字段;数据目的库-CRINF_CONTENT1是Blob类型字段]


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import com.tzt.tools.DateUtil;
import com.tzt.tools.RandomUtil;

public class BolbTest {

public static void main(String[] args) {

//数据库连接【数据来源库】

Connection msConn = ORACLEConnectionPool.getConnection();
PreparedStatement msPs = null;

ResultSet msRs = null;

//数据库连接【数据目的库】

Connection orclConn = LocalORACLEConnectionPool.getConnection();
PreparedStatement orclPs = null;
ResultSet orclRs = null;
StringBuffer msSQL=null;
//查询本地申购建议-资讯最大的id
String maxIDSql="select max(CRINF_BUSI_ID) maxId from CORE_TZT_INFO where CRINF_TYPE=7";
try {
orclPs = orclConn.prepareStatement(maxIDSql);
orclRs=orclPs.executeQuery();
String maxID=null;
while (orclRs.next()) {
maxID=orclRs.getString("maxId");
System.out.println("maxID:"+maxID);
}
//根据不同类型资讯拼接查询和插入sql
msSQL= new StringBuffer(); 
msSQL.append(" SELECT t.objid AS id,t.title AS INFOTITLE,t.writetime AS infopubldate,a.content FROM v_rschdoc_search t,attachment a WHERE (a.docid = t.objid AND a.name LIKE '%.pdf' AND t.doctypeid = 130 AND status = 200) AND t.objid>600409");

//查询对应类型的最新资讯信息
System.out.println("msSQL:"+msSQL);
msPs = msConn.prepareStatement(msSQL.toString());
msRs = msPs.executeQuery();
int j=0;
while (msRs.next()) {
j++;
if(j<=1){
Blob blobContent=msRs.getBlob("content");
InputStream inStream=blobContent.getBinaryStream();
//data是读出并需要返回的数据,类型是byte[]
                    byte[] data = new byte[(int)blobContent.length() ];
                  inStream.read(data);
                    inStream.close();
getUpdate(msRs.getString("INFOTITLE"),data,msRs.getString("infopubldate"),msRs.getString("id"));

}else{
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(orclRs, orclPs, orclConn);
close(msRs, msPs, msConn);
}
}


public static void getUpdate(String title, byte[] data, String udate, String id) throws IOException {
Connection orclConn = LocalORACLEConnectionPool.getConnection();
PreparedStatement orclPs = null;
ResultSet orclRs = null;
StringBuffer orclSQL=new StringBuffer();
try {
orclConn.setAutoCommit(false);

String uuid=RandomUtil.generateString(16);
//插入一个空对象
orclSQL.append(" insert into CORE_TZT_INFO (CRINF_UNID,CRINF_UUID,CRINF_TITLE,CRINF_CONTENT,CRINF_CONTENT1,CRINF_TYPE,CRINF_CDATE,CRINF_UDATE,CRINF_BUSI_ID) values( ")
.append("CORE_TZT_INFO_SEQUENCE.NEXTVAL,").append("'"+uuid+"',").append("'"+title+"',")
.append("'1111',")
.append("empty_blob(),")
.append("7,").append("to_date('"+DateUtil.formatTimesTampDate(new Date())+"','yyyy-MM-dd hh24:mi:ss'),")
.append("to_date('"+udate+"','yyyy-MM-dd hh24:mi:ss'),")
.append(id)
.append(" )");
orclPs=orclConn.prepareStatement(orclSQL.toString());
orclPs.executeUpdate(orclSQL.toString());

//锁定数据行进行更新,注意“for update”语句
    String sqlString="select CRINF_CONTENT1 crinfcontent1 from CORE_TZT_INFO where CRINF_UUID='"+uuid+"' for update";
    orclRs =orclPs.executeQuery(sqlString);
    if (orclRs.next()) {
    //得到java.sql.Blob对象后强制转换为oracle.sql.BLOB
        oracle.sql.BLOB blob = (oracle.sql.BLOB) orclRs.getBlob("crinfcontent1");
        OutputStream outStream = blob.getBinaryOutputStream();
        //data是传入的byte数组,定义:byte[] data
        outStream.write(data, 0, data.length);
        outStream.flush();
        outStream.close();
    }
    System.out.println("cheru ");
    orclConn.commit();
} catch (SQLException e) {
e.printStackTrace();
}finally {
close(orclRs, orclPs, orclConn);
}
}

public static void close(ResultSet rs, PreparedStatement ps, Connection conn) {
try {
if (rs != null) {
rs.close();
rs = null;
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
ps = null;
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (conn != null && !conn.isClosed()) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值