How to include multiple commands in a JDBC SQL query to Oracle 10g?

别说谁变了你拦得住时间么 提交于 2019-12-08 08:34:47

问题


I'm completely new to Oracle (I come from MySQL and MSSQL) and am novice at JDBC. One of my table creation queries looks like this:

CREATE TABLE  "LISTS" 
   ("ID" NUMBER NOT NULL ENABLE, 
    "NAME" VARCHAR2(1000) NOT NULL ENABLE, 
    "DOMAIN_ID" NUMBER NOT NULL ENABLE, 
     CONSTRAINT "LISTS_PK" PRIMARY KEY ("ID") ENABLE
   )
/

CREATE OR REPLACE TRIGGER  "BI_LISTS" 
  before insert on "LISTS"               
  for each row  
begin   
    select "LISTS_SEQ".nextval into :NEW.ID from dual; 
end; 

/
ALTER TRIGGER  "BI_LISTS" ENABLE
/

When I try to connection.createStatement().execute() this query, I get java.sql.SQLSyntaxErrorException: ORA-00922: missing or invalid option. If I remove the slashes, I get the same. If I try replacing them with semicolons, I get java.sql.SQLSyntaxErrorException: ORA-00911: invalid character.

Is it not possible to include multiple commands in one query in JDBC and/or Oracle? Or am I just missing some kind of syntax to separate them?


回答1:


Each of those are separate statements. Issue one at a time via separate Statement objects via Connection#createStatement() or via multiple SQL calls to Statement#execute(String).

Conversely, what is your reason for wanting them in one delineated statement?




回答2:


For oracle if you include your sql between BEGIN and END; it should work.

Ex:

BEGIN
    CREATE TABLE  "LISTS" 
       ("ID" NUMBER NOT NULL ENABLE, 
        "NAME" VARCHAR2(1000) NOT NULL ENABLE, 
        "DOMAIN_ID" NUMBER NOT NULL ENABLE, 
        CONSTRAINT "LISTS_PK" PRIMARY KEY ("ID") ENABLE
       )
    ;

    CREATE OR REPLACE TRIGGER  "BI_LISTS" 
      before insert on "LISTS"               
      for each row  
    begin   
        select "LISTS_SEQ".nextval into :NEW.ID from dual; 
    end; 
    ;

    ALTER TRIGGER  "BI_LISTS" ENABLE;
END;


来源:https://stackoverflow.com/questions/4404141/how-to-include-multiple-commands-in-a-jdbc-sql-query-to-oracle-10g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!