在 Groovy 中插入带有 Clob 列的 Oracle DB table 时出现异常

Exception while Inserting Oracle DB table with Clob column in Groovy

我正在尝试使用 groovy 将文件插入 oracle 数据库 table。我正在使用以下代码:

import groovy.io.FileType
import groovy.sql.Sql
import oracle.jdbc.OracleDriver

import java.sql.Date

final def PROJECT_DIR = "/appdata/project/pmp"
final def SCRIPT_DIR = "/scm/src/main/scripts"

// To be able to use driver...
new OracleDriver();

sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:XE", "PMP", "pmp")

sql.execute("delete from SCM_GROOVY_SCRIPTS")

def dir = new File(PROJECT_DIR + SCRIPT_DIR);
dir.eachFileRecurse(FileType.FILES) { file ->
    String scriptName = file.name.substring(0, file.name.indexOf('.'))
    def timestamp = new Date(System.currentTimeMillis())

    println scriptName
    println timestamp

    List<Object> params = new ArrayList<>()
    params.add(scriptName)
    params.add(file.bytes)
    params.add(timestamp)

    sql.execute("INSERT INTO SCM_GROOVY_SCRIPTS (SCRIPT_NAME, SCRIPT_SOURCE, LAST_UPDATED) VALUES (?, ?, ?)", params)
}

sql.close()

我在执行代码时得到以下输出。

ServiceUpdateRule

2016-12-28

Dec 28, 2016 11:01:56 AM groovy.sql.Sql execute WARNING: Failed to execute: INSERT INTO SCM_GROOVY_SCRIPTS (SCRIPT_NAME, SCRIPT_SOURCE, LAST_UPDATED) VALUES (?, ?, ?) because: ORA-01461: can bind a LONG value only for insert into a LONG column

Caught: java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column

java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:754)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:219)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:972)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1192)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3415)
    at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3521)
    at InsertUpdate$_run_closure1.doCall(InsertUpdate.groovy:35)
    at InsertUpdate.run(InsertUpdate.groovy:23)

如果我将 clob 参数作为 null 而不是 file.bytes 传递,它会插入所有行而不会出现任何错误。我的 table 的结构如下:

CREATE TABLE SCM_GROOVY_SCRIPTS (
        SCRIPT_NAME VARCHAR2(255) NOT NULL,
        SCRIPT_SOURCE CLOB,
        LAST_UPDATED DATE DEFAULT SYSDATE ,
        PRIMARY KEY (SCRIPT_NAME)
);

此外,如果我使用 BLOB 数据类型而不是 CLOB,代码也能正常工作。

终于找到了插入CLOB数据类型的方法。

解决方案是使用java.sql.Clob和oracle.sql.CLOB类。

import groovy.io.FileType
import groovy.sql.Sql
import oracle.jdbc.OracleDriver
import oracle.sql.CLOB

import java.sql.Clob
import java.sql.Date

final def PROJECT_DIR = "/appdata/project/pmp"
final def SCRIPT_DIR = "/scm/src/main/scripts"

// To be able to use driver...
new OracleDriver();

Sql sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:XE", "PMP", "pmp")

sql.execute("delete from SCM_GROOVY_SCRIPTS")

def dir = new File(PROJECT_DIR + SCRIPT_DIR);
dir.eachFileRecurse(FileType.FILES) { file ->
    String scriptName = file.name.substring(0, file.name.indexOf('.'))
    def timestamp = new Date(System.currentTimeMillis())

    println scriptName
    println timestamp

    Clob clob = CLOB.createTemporary(sql.getConnection(), false, CLOB.DURATION_SESSION);
    clob.setString(1, file.getText("UTF-8"))

    List<Object> params = new ArrayList<>()
    params.add(scriptName)
    params.add(clob)
    params.add(timestamp)

    sql.execute("INSERT INTO SCM_GROOVY_SCRIPTS (SCRIPT_NAME, SCRIPT_SOURCE, LAST_UPDATED) VALUES (?, ?, ?)", params)
}

sql.close()