3
CREATE USER Person identified by 2012;GRANT ALL PRIVILEGES TO Person;

These statements are successfully executed by Oracle 11g (GUI). But, when I copy and paste the above statement exactly and try to execute it by using executeUpdate(String sql), I get the exception below. Why?

java.sql.SQLSyntaxErrorException: ORA-00911: invalid character
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
sweet dreams
  • 2,094
  • 13
  • 32
  • 46

2 Answers2

4

You should not give a two different SQL statements as one. There is no way that you can JDBC driver will execute two statements passed as one string.

try to execute them as

Statement stmt = conn.createStatement();
stmt.executeUpdate("CREATE USER Person identified by 2012");
stmt.executeUpdate("GRANT ALL PRIVILEGES TO Person;");

That should do. Cheers.

sakthisundar
  • 3,278
  • 3
  • 16
  • 29
3

Dependend on your database jdbc driver, the driver will not support executing two statements in one "executeUpdate". You have to do something like:

Statement stmt = conn.createStatement();
for(String statement : statements) {
    stmt.addBatch(statement);
}
stmt.executeBatch();
markus
  • 602
  • 4
  • 13
  • 1
    Thanks ! I wanted to add some extra info about executeUpdate vs executeBatch() here - http://stackoverflow.com/questions/1143053/how-effective-is-executebatch-on-a-prepared-statement – sweet dreams Aug 14 '12 at 07:50