Can you put multiple statements in one query-strin

2019-07-15 07:29发布

问题:

I have a JDBC connection to an Oracle database. I create a Statement. The SQL query String contains multiple statements separated by a semicolon, and is provided by a different system.

Example:

connection.prepareStatement("SELECT * FROM A; SELECT * FROM B");

According to ddimitrov it isn't possible.

But all other databases I've tried support it. And JDBC even has support to retrieve multiple results.

Does anyone have either pointers to Oracle documentation explicitly stating that it is not supported or have a way to make it work (without using of stored procedures)?

回答1:

For executing multiple statements:

JDBC 2.0 lets you submit multiple statements at one time with the addBatch method

See here.



回答2:

No, this is not possible with the Oracle JDBC driver.

You will have to parse and split the string into their individual statements.

Btw: I think the only databases that allow this are Microsoft SQL Server and MySQL. Which also makes them vulnerable to certain kind of SQL injection attacks that would not work Oracle or PostgreSQL.



回答3:

AFAIK most databases only allow you to execute / prepare one statement per execute or prepare call. Although not very explicitly expressed, the intent of the JDBC methods is to execute a single SQL statement:

sql - **an** SQL statement that may [...]

The retrieval of multiple resultsets is for (very rare) single(!) statements or stored procedures which return multiple resultsets (as explained in the javadoc of Statement#execute).



标签: java oracle jdbc