mysql关闭prepareStatement功能

本文探讨了MySQL中使用Prepared Statement的不同场景及效果对比。通过Java示例代码展示如何配置并使用预编译语句,解释了useServerPrepStmts参数的作用,并分析了其对SQL执行效率的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境为 
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数量增加。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值