我可以在JDBC准备的查询中使用多个语句吗? [英] Can I use multiple statements in a JDBC prepared query?

查看:95
本文介绍了我可以在JDBC准备的查询中使用多个语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MySQL服务器上执行类似的操作:

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);

这可以从控制台正常工作,但不能从我的Java PreparedStatement工作。它会在分隔语句的';'处引发语法错误而抛出异常。我喜欢变量,因为我不必重复查找子句,但如果需要我可以重写它。对于UNION条款,等效的JOIN也有点尴尬。

This works fine from the console, but not from my Java PreparedStatement. 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.

谢谢,

Joshua

推荐答案

JDBC从未支持解析分隔查询。每次调用都是一次数据库之旅。也许你可以实现你为每个单独的查询做PreparedStatement.addBatch()的意义,然后执行和检索两个结果集?

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 ?

这篇关于我可以在JDBC准备的查询中使用多个语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆