zoukankan      html  css  js  c++  java
  • JDBC的URL设置allowMultiQueries的原因

    如下的一个普通JDBC示例:

    String user ="root";
    String password = "root";
    String url = "jdbc:mysql://localhost:3306";
    Connection conn = java.sql.DriverManager.getConnection(url , user, password);
    Statement stmt = conn.createStatement();
    String sql = "select 'hello';select 'world'";
    stmt.execute(sql);

    执行时会报错:

    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 'world'' at line 1at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

    若一个sql中通过分号分割(或包含)了多个独立sql的话,如:

    select 'hello';select 'world'

    默认就会报上述错误,当若显式设置allowMultiQueries为true的话,就可以正常执行不会报错.如下所示:
    String url = "jdbc:mysql://localhost:3306?allowMultiQueries=true";

    allowMultiQueries
    Allow the use of ';' to delimit multiple queries during one statement (true/false), defaults to 'false', and does not affect the addBatch() and executeBatch() methods, which instead rely on rewriteBatchStatements.
    Default: false
    Since version: 3.1.1





  • 相关阅读:
    第03组 Alpha冲刺(3/4)
    第03组 Alpha冲刺(2/4)
    第03组 Alpha冲刺(1/4)
    第08组 Beta版本演示
    第08组 Beta冲刺(4/4)
    第08组Beta冲刺(3/4)
    第08组Beta冲刺(2/4)
    第08组 Beta冲刺(1/4)
    Alpha事后诸葛亮
    第08组 Alpha冲刺(4/4)
  • 原文地址:https://www.cnblogs.com/jeffen/p/6038261.html
Copyright © 2011-2022 走看看