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






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM