开发者

Oracle jdbc creating a table, sequence and trigger

开发者 https://www.devze.com 2023-04-07 17:35 出处:网络
I\'m trying to create a table with a sequence and with a trigger for that sequence. When I do this within Oracle Express edition I have the following SQL statements. Which DO run correctly inside Orac

I'm trying to create a table with a sequence and with a trigger for that sequence. When I do this within Oracle Express edition I have the following SQL statements. Which DO run correctly inside Oracle Express using the same user I log开发者_C百科 in with JDBC.

CREATE table "TEST" (
    "ID"         NUMBER(10) NOT NULL,
    "NAME"       VARCHAR2(20),
    constraint  "TEST_PK" primary key ("ID")
)


CREATE sequence "TEST_SEQ" 


CREATE trigger "BI_TEST"  
  before insert on "TEST"              
  for each row 
begin  
  if :NEW."ID" is null then
    select "TEST_SEQ".nextval into :NEW."ID" from dual;
  end if;
end;

So now what I do is put each of these into a List of Strings and execute 1 by one like this:

List<String> commands = new ArrayList<String>(4);
commands.add("Create table ...");
commands.add("Create sequence ...");
commands.add("Create trigger...");

st = con.createStatement();
for (String command : commands) {
    st.execute(command);
}

etc...

But I get an error with the trigger statement. "Error with Oracle command: ORA-00942: table or view does not exist"

It seems to me that Oracle has not yet seen the new table at this point. How can I correct this? Should I create multiple statement objects or try committing in-between each command?

TIA


It seems that an error in invoking method execute. Another method Statement#executeUpdate should be used for DDL queries. See official javadoc.

Update: Have you tried to use executeBatch method? I'm trying to execute batch using spring jdbc:

getJdbcTemplate().batchUpdate(new String[] { createTable, createTrigger, insert });

it works fine for me.


Check for synonym of the table created. You can not access, if synonym is not created for the user.


Thanks for the input.

I had 2 issues. The "table or view does not exist" error was resolved by creating a new statement for each SQL I ran + using executeUpdate method instead of execute method.

Then I had an issue with the trigger being invalid - so I made sure all the trigger/table/index names used upper case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号