public class BlobCopy { |
public static void main(String[] args) { |
Connection oraConn, pgConn = null; |
try { |
// 双方のJDBCドライバをロードします。 |
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver() ); |
DriverManager.registerDriver( new org.postgresql.Driver() ); |
|
// 双方のデータベースに接続します。 |
oraConn = DriverManager.getConnection( |
"jdbc:oracle:thin:@127.0.0.1:1521:ORA10", "tsukaeru", "sql" |
); |
pgConn = DriverManager.getConnection( |
"jdbc:postgresql://127.0.0.1:5432/tsukaeru", "postgres", "postgres" |
); |
|
// 目的のバイナリデータ列(BLOB)から、データを移行します。 |
String select = "SELECT IMG_DATA FROM COMPANY_LOGO"; |
PreparedStatement selStmt = oraConn.prepareStatement( select ); |
ResultSet selRS = selStmt.executeQuery(); |
|
while ( selRS.next() ) { |
PreparedStatement insStmt = null; |
try { |
String insert = "INSERT INTO COMPANY_LOGO VALUES ( ? )"; |
insStmt = pgConn.prepareStatement( insert ); |
Blob blob = selRS.getBlob("img_data"); |
InputStream bin = blob.getBinaryStream(); |
insStmt.setBinaryStream( 1, bin, (int)blob.length() ); |
insStmt.executeUpdate(); |
bin.close(); |
} |
catch (IOException ex) { |
ex.printStackTrace(); |
} |
finally { |
try { insStmt.close(); } catch (SQLException ex) {} |
} |
} |
selRS.close(); |
selStmt.close(); |
} |
catch (SQLException ex) { |
ex.printStackTrace(); |
} |
finally { |
try { oraConn.close(); } catch (SQLException ex) {} |
try { pgConn.close(); } catch (SQLException ex) {} |
} |
} |
} |