在Groovy中使用Clob列插入Oracle DB表时出现异常

时间:2016-12-28 08:11:40

标签: java oracle jdbc groovy clob

我尝试使用groovy将文件插入到oracle db表中。我使用以下代码:

    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)

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

  

ServiceUpdateRule

     

2016年12月28日

     

2016年12月28日上午11:01:56 groovy.sql.Sql执行   警告:无法执行:INSERT INTO SCM_GROOVY_SCRIPTS   (SCRIPT_NAME,SCRIPT_SOURCE,LAST_UPDATED)VALUES(?,?,?)因为:   ORA-01461:只能插入一个LONG值才能插入LONG列

     

Caught:java.sql.SQLException:ORA-01461:只能绑定LONG值   插入LONG栏

     

java.sql.SQLException:ORA-01461:只能绑定LONG值   插入LONG栏

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

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

    Switch(v.getId()){

     case R.id.Yourid1:

     CountDownTimer test=  new CountDownTimer(3000,1000) { //count down from 3 sec to 1 sec

            @Override
            public void onTick(long millisUntilFinished) {
                //do some thing after starting timer

            }

            @Override
            public void onFinish() {

            // do something after timer is finished
            }
        };


        test.start();

break;

    case R.id.YourId2:


         CountDownTimer test1=  new CountDownTimer(3000,1000) { //count down from 3 sec to 1 sec

            @Override
            public void onTick(long millisUntilFinished) {
                //do some thing after starting timer

            }

            @Override
            public void onFinish() {

            // do something after timer is finished
            }
        };


        test1.start();

      }

break;

此外,如果我使用BLOB数据类型而不是CLOB,则代码可以工作。

1 个答案:

答案 0 :(得分:0)

最后,我找到了一种插入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()