使用不同的实现在另一个Clob中复制Clob

问题描述 投票:-1回答:1

在我的公司,我们正在执行数据库迁移。由于很多原因我们无法使用数据库迁移工具,因此我们必须开发我们的迁移工具,以便从DB复制到另一些特定表中包含的所有行。我们使用多个数据库的JDBC开发了一个工具。目前,我们正在从DB2迁移到Oracle,并向H2进行中间步骤。相同的表格是Clob列。当我们将这个列从DB2导出到H2时,我们没有错误或问题,但是当我们尝试使用JDBC将Clob从H2复制到Oracle时,我们得到以下异常:

ClassCastException: cannot cast from org.h2.jdbc.JdbcClob to oracle.jdbc.Clob

有没有办法或程序来执行这种转换?像Clob类型中的ClobCopy实用程序?遗憾的是,由于客户的规范,我们只能使用Java和Jdbc,没有JPA或DB迁移工具来完成此任务。

这是我正在尝试做的一个例子:

public class CopyTable {

    public void doCopy(){
        Connection h2 = getH2Connection(); //suppose this exists and works
        Connection oracle = getOracleConnection(); //suppose this exists and works

        String sqlSelect = "select * from tabletoexport";
        String sqlInsert = "insert into tabletofill(ID, DATA) values (?,?)";

        PreparedStatement select = h2.prepareStatement(sqlSelect);
        PreparedStatement insert = oracle.prepareStatement(sqlInsert);
        ResultSet rs = select.executeQuery();
        while (rs.next()){
            insert.setLong(1, rs.getLong("ID"));
            insert.setClob(2, rs.getClob("DATA")); //this throws an exception
            insert.executeUpdate();
        }

    }

}
java jdbc clob
1个回答
3
投票

Clob界面有一个getCharacterStream()方法返回Reader,而PreparedStatement界面有一个setClob()方法,需要一个Reader。要使副本正常工作,您需要做的就是使用这些方法。

换句话说,更换线

    insert.setClob(2, rs.getClob("DATA")); //this throws an exception

    insert.setClob(2, rs.getClob("DATA").getCharacterStream());

至于为什么从DB / 2导入到H2并没有抱怨,也许H2 JDBC驱动程序不假设传入ClobsetClob值来自H2,但Oracle JDBC驱动程序确实假设Clobs传入方式来自Oracle。但是,Oracle JDBC无法合理地对Reader进行任何此类假设,因为这些假设可能来自任何地方

© www.soinside.com 2019 - 2024. All rights reserved.