I'm trying to insert a file into a oracle db table by using groovy. I am using the following code:
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()
I get the following output when I execute the code.
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)
If I pass the clob argument as null instead of file.bytes, it inserts all rows without any error. The structure of my table is the following:
CREATE TABLE SCM_GROOVY_SCRIPTS (
SCRIPT_NAME VARCHAR2(255) NOT NULL,
SCRIPT_SOURCE CLOB,
LAST_UPDATED DATE DEFAULT SYSDATE ,
PRIMARY KEY (SCRIPT_NAME)
);
In addition, the code works if I use BLOB data type instead of CLOB.
Finally, I found a way to insert CLOB data type.
The solution is to use java.sql.Clob and oracle.sql.CLOB classes.