Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以使用MySQL Connector/J执行多个以分号分隔的查询吗?_Java_Mysql_Jdbc - Fatal编程技术网

Java 我可以使用MySQL Connector/J执行多个以分号分隔的查询吗?

Java 我可以使用MySQL Connector/J执行多个以分号分隔的查询吗?,java,mysql,jdbc,Java,Mysql,Jdbc,我的mysql数据库jdbc驱动程序的版本是5.1.25 我希望执行sql查询,如下所示: 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: Y

我的mysql数据库jdbc驱动程序的版本是5.1.25

我希望执行sql查询,如下所示:

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)

但是,当我从命令行运行同一个查询(用分号分隔)时,它工作得很好,并按预期输出两个表。

不,您不能。通过调用statement.execute(…)您希望得到什么? 它返回一个ResultSet(…表示一个表)

您只需调用“selectfullname from user where user_id in(1,2)”,即可返回这两个结果


在JDBC语句中使用分号通常非常容易出错。一些JDBC驱动程序不支持这种情况(例如,如果您用“;”关闭SQL语句,IBM的DB210.x JDBC驱动程序将抛出异常)

使用
不起作用,因为它通常不是语句语法本身的一部分,而是命令行或脚本输入到单独语句的终止符。命令行或脚本处理器将分号视为语句完成并可以发送到服务器的信号

此外,在JDBC中,单个语句prepare(或execute)应仅为一个实际语句,因此不允许使用多个语句,因此也不需要使用分号。对于某些(大多数?)数据库,分号不是语句语法的一部分,包含一个分号只是语法错误

如果要执行多个语句,则需要使用单独的executes。从技术上讲,MySQL确实有一个支持多个执行的选项,该选项可以通过连接属性启用。这种行为不符合JDBC规范/API,并且会降低代码的可移植性。请参见上的
allowmultiquerys

我希望执行sql查询,如下所示:

语句。执行(“从user_id=1的用户选择全名;从user_id=2的用户选择全名”)

只有当您将一个数据库连接属性设置为允许同时执行多个查询时,这才是可能的。属性名为
allowMultiQueries=true
。必须设置此属性,并将其与数据库连接请求一起发送到服务器。一般语法如下所示:

String dbUrl = "jdbc:mysql:///test?allowMultiQueries=true";
这是那些已经存在的连接属性的附加属性,例如
autoReConnect=true
,等等

allowMultiQueries
属性的可接受值为
true
false
yes
no
。任何其他值在运行时都会被拒绝,并出现
SQLException

您必须使用或其其他变体来获取查询执行的结果

multiQuerySqlString =  "select fullName from user where user_id=1; ";
multiQuerySqlString += "select fullName from user where user_id=2; ";
// you can multiple types of result sets
multiQuerySqlString += "select last_login from user_logs where user_id=1; ";

boolean hasMoreResultSets = stmt.execute( multiQuerySqlString );
要遍历和处理结果,需要执行以下步骤:

int rsNumber = 0;
while ( hasMoreResultSets ) {  
    rsNumber += 1;
    Resultset rs = stmt.getResultSet();

    // based on the structure of the result set,
    // you can handle column values.
    if ( rsNumber == 1 ) {
      while( rs.next() ) {
          // handle your rs here
      } // while rs
    } // if rs is 1
    else if ( rsNumber == 2 ) {
      // call a method using this rs.
      processMyResultSet( rs ); // example
    } // if rs is 2
    // ... etc

    // check whether there exist more result sets  
    hasMoreResultSets = stmt.getMoreResults();  
} // while results
请参阅

    • 其中一个类似的帖子,我给出了答案

可能不会-sql将返回两个结果集,execute()将返回什么?如果您只有一条语句,那么一切都正常吗?在一个查询中不允许多条语句也是一种安全保障,尽管这不是一种完美的保障。如果可以执行多个查询,则注入可以让入侵者执行完全任意的SQL:。您应该从user\u id=1或user\u id=2的用户中选择全名。为了防止SQL注入的效率和安全性,您还应该使用准备好的语句。JDBC可以为一个语句/查询返回多个结果集。使用statement.getResultSet()获取当前结果集,使用statement.next()移动到下一个结果集。您需要将allowMultiQueries属性设置为true以禁用JDBC保护,因为允许分号会打开SQL注入攻击向量。