我可以使用 MySQL Connector/J 执行以分号分隔的多个查询吗? [英] Can I execute multiple queries separated by semicolon with MySQL Connector/J?

查看:16
本文介绍了我可以使用 MySQL Connector/J 执行以分号分隔的多个查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用于 mysql db 的 jdbc 驱动程序的版本是 5.1.25.

My jdbc driver for mysql db is of version 5.1.25.

我想像这样执行 sql 查询:

I want to execute sql query like so:

statement.execute("select fullName from user where user_id=1; select fullName from user where user_id=2");

而且我总是收到异常:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select fullName from user where user_id=2' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2809)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2758)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:894)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:732)
    at dbViewer.model.UserConnectionManager.retrieveRoutinesNames1(UserConnectionManager.java:622)
    at dbViewer.model.UserConnectionManager.main(UserConnectionManager.java:637)

但是,当我从命令行运行相同的查询(用分号分隔)时,它可以完美运行并按预期输出两个表.

BUT when I run this same query(separated by semicolon) from command line it works perfectly and outputs two tables as expected.

推荐答案

在大多数数据库的查询中使用 ; 不起作用,因为它通常不是语句语法本身的一部分,而是一个用于分隔语句的命令行或脚本输入的终止符.命令行或脚本处理器将分号视为语句完成并可以发送到服务器的信号.

Using ; in a query for most databases doesn't work as it is usually not part of the statement syntax itself, but a terminator for command line or script input to separate statements. The command line or script processor sees a semi-colon as the signal that the statement is complete and can be sent to the server.

同样在 JDBC 中,单个语句准备(或执行)应该只一个实际语句,因此不允许多个语句,因此也不需要分号,至于某些(大多数?)数据库,分号不是语句语法的一部分,包含分号只是语法错误.

Also in JDBC a single statement prepare (or execute) should only be one actual statement so multiple statements are not allowed and so there is also no need to have a semi-colon, and as for some (most?) databases the semi-colon isn't part of the statement syntax, it is simply a syntax error to have one included.

如果要执行多条语句,需要使用单独的executes.从技术上讲,MySQL 确实有一个选项来支持可以通过连接属性启用的多个执行.此行为不符合 JDBC 规范/API 并降低您的代码的可移植性.请参阅 allowMultiQueries>连接器/J的驱动程序/数据源类名称、URL语法和配置属性

If you want to execute multiple statements, you need to use separate executes. Technically, MySQL does have an option to support multiple executions which can be enabled by a connection property. This behavior is not compliant with the JDBC specification/API and makes your code less portable. See allowMultiQueries on Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J

这篇关于我可以使用 MySQL Connector/J 执行以分号分隔的多个查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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