Avatar billede fredand Forsker
22. september 2004 - 19:35 Der er 8 kommentarer og
1 løsning

How to create PreparedStatements with multiple ResultSet ?

Hello!

I belive that PreparedStatements can return multiple ResultSet since they got the method:
preparedStatement.getMoreResults()
...that I belive jumps to the next ResultSet.

But when I try to create a PreparedStatement with a question that should give back more then one ResultSet like and rub with my mysql:

connection.prepareStatement("SELECT * FROM tbl_test; SELECT * FROM tbl_users;");

I just get a exception like there is something wrong with the SQL-statement.

Has any one tried this?

I also have an other problem, that may depend on the same as above, when I create PreparedStatement with more than one questions like above but which only should give back one ResultSet:

connection.prepareStatement("INSERT INTO tbl_test (text1, text2) VALUES(?, ?); SELECT LAST_INSERT_ID() FROM tbl_test;");

The exception is:
java.sql.SQLException: Syntax error or access violation: You have an error in your SQL syntax near';SELECT LAST_INSERT_ID() FROM tbl_test; ' at line 1


So perhaps it something wrong how I create the SQL-statements but the strange thing is that it works fine from the commandprompt for mysql. Like if I just type:
INSERT INTO tbl_test (text1, text2) VALUES(?, ?); SELECT LAST_INSERT_ID() FROM tbl_test;

...that works fine in.

So if any one got a solution for this it would be great!

Best regards
Fredrik
Avatar billede arne_v Ekspert
22. september 2004 - 19:41 #1
PreparedStatement inherits it from Statement.

The only place where I know it can be used is for CallableStatement (which
inherits from PreparedStatement).

It is quite common to use CallableStatement to calll a store procedure that
returns multiple reseult set.
Avatar billede arne_v Ekspert
22. september 2004 - 19:42 #2
Also read Java Doc for Statement execute:

execute

public boolean execute(String sql)
                throws SQLException

    Executes the given SQL statement, which may return multiple results. In some (uncommon) situations, a single SQL statement may return multiple result sets and/or update counts. Normally you can ignore this unless you are (1) executing a stored procedure that you know may return multiple results or (2) you are dynamically executing an unknown SQL string.

    The execute method executes an SQL statement and indicates the form of the first result. You must then use the methods getResultSet or getUpdateCount to retrieve the result, and getMoreResults to move to any subsequent result(s).
Avatar billede fredand Forsker
22. september 2004 - 20:07 #3
Hello!

Yes...but I guess that I create the SQL-statements wrong, for eg:
INSERT INTO tbl_test (text1, text2) VALUES(?, ?); SELECT LAST_INSERT_ID() FROM tbl_test;

Perhaps it is not possible to do this at all or I should seperate them with somethin else then ;

Again the line of swl-statement works fine like one row in mysql-commandprompt.

And like you wrote:
Executes the given SQL statement, which may return multiple results...
it looks like this should be possible.

Best regards
Fredrik
Avatar billede arne_v Ekspert
22. september 2004 - 20:11 #4
It also said:

In some (uncommon) situations

Normally you can ignore this unless you are (1) executing a stored procedure that you know may return multiple results
Avatar billede arne_v Ekspert
22. september 2004 - 20:14 #5
MySQL is special that it sometimes permits multiple SQL statements separated
by semicolon where one statement is expected.

And if the JDBC driver implementators had decided to support it, then it
would have worked.

But apperently not.
Avatar billede arne_v Ekspert
22. september 2004 - 20:15 #6
BTW, the specific last_insert_id you can get via stmt.getGeneratedKeys()
for any JDBC 3 compliant driver.

And newest MYSQL is JDBC 3 compliant !
Avatar billede arne_v Ekspert
03. oktober 2004 - 15:19 #7
OK ?
Avatar billede fredand Forsker
04. oktober 2004 - 07:38 #8
Hello Arne!

I'm trying to found out if it is a bug by trying to contact MySQL about it. So I will get back to you with this.

Best regards
Fredrik
Avatar billede fredand Forsker
07. oktober 2004 - 07:52 #9
Hello Arne!

I got this reply from MySQL so I guess it is just not possible.
Thanks for your time.

MySQL-4.1 is the first version of MySQL to support multiple statements, however it doesn't support multiple statements per prepared statement. You'll have to use 'normal' statements to accomplish this.

You'll also have to enable this feature on the JDBC side (as well as use Connector/J 3.1.x), as it isn't enabled by default, because multiple-query support is a SQL-injection security vulnerability if not used correctly by the end-user. You can do this by setting 'allowMultiQueries=true' in your JDBC-url
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester