I was wondering if it is possible to accomplish something similar using JDBC.
"SELECT FROM * TABLE;INSERT INTO TABLE;"
Yes it is possible. As far as I know, there are two ways. They
- By setting the database connection property to allow multiple queries, separated by the default semicolon.
- A call to a stored procedure that returns cursors implicitly.
The following examples show the two above possibilities.
Example 1 : (To allow multiple requests):
When sending a connection request, you need to add the connection property allowMultiQueries=true to the database URL. This is an additional connection property for those that already exist, for example, autoReConnect=true , etc. Valid values ββfor the allowMultiQueries property are true , false , yes and no . Any other value is rejected at runtime by using SQLException .
String dbUrl = "jdbc:mysql:///test?allowMultiQueries=true";
If such an instruction is not passed, an SQLException is thrown.
You should use execute( String sql ) or other options to get query results.
boolean hasMoreResultSets = stmt.execute( multiQuerySqlString );
To repeat and process the results, you need the following steps:
READING_QUERY_RESULTS: // label while ( hasMoreResultSets || stmt.getUpdateCount() != -1 ) { if ( hasMoreResultSets ) { Resultset rs = stmt.getResultSet(); // handle your rs here } // if has rs else { // if ddl/dml/... int queryResult = stmt.getUpdateCount(); if ( queryResult == -1 ) { // no more queries processed break READING_QUERY_RESULTS; } // no more queries processed // handle success, failure, generated keys, etc here } // if ddl/dml/... // check to continue in the loop hasMoreResultSets = stmt.getMoreResults(); } // while results
Example 2 : steps to complete:
- Create a procedure with one or more
select and DML queries. - Call it from java using
CallableStatement . - You can capture several
ResultSet executed in a procedure.
DML results cannot be captured, but may produce another select
to find how rows are affected in the table.
Example table and procedure:
mysql> create table tbl_mq( i int not null auto_increment, name varchar(10), primary key (i) ); Query OK, 0 rows affected (0.16 sec) mysql> delimiter // mysql> create procedure multi_query() -> begin -> select count(*) as name_count from tbl_mq; -> insert into tbl_mq( names ) values ( 'ravi' ); -> select last_insert_id(); -> select * from tbl_mq; -> end; -> // Query OK, 0 rows affected (0.02 sec) mysql> delimiter ; mysql> call multi_query(); +------------+ | name_count | +------------+ | 0 | +------------+ 1 row in set (0.00 sec) +------------------+ | last_insert_id() | +------------------+ | 3 | +------------------+ 1 row in set (0.00 sec) +---+------+ | i | name | +---+------+ | 1 | ravi | +---+------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.00 sec)
Call procedure with Java:
CallableStatement cstmt = con.prepareCall( "call multi_query()" ); boolean hasMoreResultSets = cstmt.execute(); READING_QUERY_RESULTS: while ( hasMoreResultSets ) { Resultset rs = stmt.getResultSet();