gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:58:32 25 4
gpt4 key购买 nike

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

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 传递,它将插入所有行而不会出现任何错误。我的表的结构如下:

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

关于java - 在 Groovy 中插入带有 Clob 列的 Oracle DB 表时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41357958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com