1. 程式人生 > >Java-Blob-取出來插入另一個數據庫[導庫程式]

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