如何在Java中执行创建触发器SQL?

时间:2018-11-08 08:17:18

标签: java plsql

我想通过Java设置触发器。

因此,我使用了ibatis提供的ScriptRunner

但是JDBC用分号分隔查询,因此构成触发器的PL / SQL并不完全清楚。

您有解决此问题的想法吗?

(我正在使用PostgreSQL DB)

这是我的代码

URL ddlUrl = ClassLoader.getSystemClassLoader().getResource( "com/puzzledata/pa/schema/origin/ddl.sql");
URL baseDataUrl = ClassLoader.getSystemClassLoader().getResource( "com/puzzledata/pa/schema/origin/basedata.sql");

File[] files = new File[2];
files[0] = new File( ddlUrl.getPath());
files[1] = new File( baseDataUrl.getPath());

for( File f : files) {
    conn = DriverManager.getConnection( URL, USER_NAME, PASSWORD);//some method to get a Connection
    ScriptRunner runner= new ScriptRunner(conn);
    InputStreamReader reader = new InputStreamReader(new FileInputStream(f));
    runner.runScript(reader);
    reader.close();
    conn.close();

}

这是执行PL / SQL时的错误

   16:32:10.611 ERROR jdbc.sqltiming [] - [ : ]2. Statement.execute(create OR REPLACE function msg_bigsync() returns trigger AS $bigsync_trg$
    BEGIN
        IF NEW.state = 'Processing' THEN
            UPDATE PDMessage set state='Processing', message = NEW.content, modifiedat=NEW.executedat WHERE jobid = NEW.jobid
) FAILED! create OR REPLACE function msg_bigsync() returns trigger AS $bigsync_trg$
    BEGIN
        IF NEW.state = 'Processing' THEN
            UPDATE PDMessage set state='Processing', message = NEW.content, modifiedat=NEW.executedat WHERE jobid = NEW.jobid
 {FAILED after 8 msec}

这是PL / SQL

  create OR REPLACE function msg_bigsync() returns trigger AS $bigsync_trg$
    BEGIN
        IF NEW.state = 'Processing' THEN
            UPDATE PDMessage set state='Processing', message = NEW.content, modifiedat=NEW.executedat WHERE jobid = NEW.jobid;
        END IF;
        RETURN NULL;
    END;
$bigsync_trg$ LANGUAGE plpgsql;

1 个答案:

答案 0 :(得分:0)

我自己找到了答案

我需要逐行执行整个脚本。

有一个ScriptRunner.class选项可以设置runner.setSendFullScript(true) 并且可以完全正常工作。

我想最好让用户可以通过method的参数来设置此选项。