开发者

Can I use multiple statements in a JDBC prepared query?

开发者 https://www.devze.com 2023-01-09 23:36 出处:网络
I\'d like to execute something like this on my MySQL server: SET @id=(SELECT id FROM lookupTable WHERE field=?);

I'd like to execute something like this on my MySQL server:

SET @id=(SELECT id FROM lookupTable WHERE field=?);
(SELECT * FROM table2 WHERE id=@id)
UNION
(SELECT * FROM table3 WHERE id=@id)
UNION
(SELECT * FROM table4 WHERE id=@id);

This works fine from the console, but not from my Java PreparedS开发者_运维问答tatement. It throws an exception with a syntax error at the ';' separating the statements. I like the variable because I don't have to repeat the lookup clause, but I could rewrite it if necessary. The equivalent JOIN is a little awkward with the UNION clauses too.

Thanks,

Joshua


JDBC has never supported parsing delimited queries. Each invocation is one trip to the database. Perhaps you can achieve what you meant to doing PreparedStatement.addBatch() for each separate query, then executing and retrieving the two resultsets ?


Just running this as two separate queries (within one connection) should give you same results.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号