Tried to execute this using Statement object. Since this is a block, an exception was raised saying that this is not an sql statement
Since you were trying to execute a plsql block, you should not use Statement object.
From https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html:
The object used for executing a static SQL statement and returning the results it produces.
This is the way you need to execute a block:
CallableStatement anonBlock = null // Note that CallableStatement is an interface
String anonBlockString = '' //Generally multi line
// Set in and out parameters as needed by your anonBlockString
anonBlock.registerOutParameter( <number> , <type> )
...
// executeUpdate() is inherited from PreparedStatement and can be used for executing DML statements (update, insert, delete)
anonBlock.executeUpdate();
To access out parameters:
anonBlock.getString(<number which you have assigned in registerOutParameter() calls);
For complete example: (https://download.oracle.com/otn_hosted_doc/timesten/1121/quickstart/sample_code/jdbc/plsqlJDBC.java)
This can be split up into sql statements, but I have 100s of such blocks which would be cumbersome for the code and thought of leaving this to the sqlplus.
Prefer to use stored procedures instead of anonymous blocks. Since stored procedures are stored in a compiled and optimized format, they have a performance boost compared to anonymous ones
Tried with CallableStatement which did not work as well:
What was the code, error/stack?
CallableStatementversion)? And post the exact error stack you're getting.Statementobject, you have to useStatement.execute()though. You only need aCallableStatementif oyu need to pass parameters to the PL/SQL block. And you need to post your Java code. "did not work" is neither an acceptable error description nor a valid Java or Oracle exception