博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mysql关闭prepareStatement功能
阅读量:4106 次
发布时间:2019-05-25

本文共 963 字,大约阅读时间需要 3 分钟。

环境为 

mysql 5.1.39 

mysql-connector-j  5.1.11 

测试代码 

Java代码  
  1. public static void main(String[] args) throws Exception {  
  2. Connection conn = getConnection();  
  3.   
  4. PreparedStatement ps = conn  
  5. .prepareStatement("select * from test where name=?");  
  6. ps.setInt(11);  
  7. ps.execute();  
  8.   
  9. Statement stmt = conn.createStatement();  
  10.   
  11. stmt.execute("select * from test where name=1");  
  12.   
  13. conn.commit();  
  14. conn.close();  
  15.   
  16. }  
  17.   
  18. private static Connection getConnection() throws Exception {  
  19. Class.forName("com.mysql.jdbc.Driver");  
  20. Connection conn = DriverManager.getConnection(  
  21. "jdbc:mysql://localhost:3306/db_test?useServerPrepStmts=false",  
  22. "db_test""db_test");  
  23. conn.setAutoCommit(false);  
  24. return conn;  
  25. }  

在mysql驱动url中如果设置useServerPrepStmts=false,那么上面两条语句执行时,向服务器发送的数据包一样(用wireshark抓包工具加断点看到)。 

如果设置useServerPrepStmts=true,那么执行conn.prepareStatement("select * from test where name=?");语句时,会向服务器发送命令,告知这条语句,执行ps.execute();时则只发送参数和语句编号。此时在服务器端 

mysql> show global status like 'Com_stmt%' ; 

可以看到 Com_stmt_prepare数量增加。

转载地址:http://sfnsi.baihongyu.com/

你可能感兴趣的文章
(python版)《剑指Offer》JZ13:调整数组顺序使奇数位于偶数前面
查看>>
(python版)《剑指Offer》JZ28:数组中出现次数超过一半的数字
查看>>
(python版)《剑指Offer》JZ30:连续子数组的最大和
查看>>
(python版)《剑指Offer》JZ02:替换空格
查看>>
JSP/Servlet——MVC设计模式
查看>>
使用JSTL
查看>>
Java 8新特性:Stream API
查看>>
管理用户状态——Cookie与Session
查看>>
最受欢迎的前端框架Bootstrap 入门
查看>>
JavaScript编程简介:DOM、AJAX与Chrome调试器
查看>>
通过Maven管理项目依赖
查看>>
通过Spring Boot三分钟创建Spring Web项目
查看>>
Spring的IoC(依赖注入)原理
查看>>
Java编程基础:static的用法
查看>>
Java编程基础:抽象类和接口
查看>>
Java编程基础:异常处理
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
Spring处理表单提交
查看>>
Spring MVC异常处理
查看>>
Leetcode 1180. Count Substrings with Only One Distinct Letter [Python]
查看>>