mysql关闭prepareStatement功能
2015-11-19 16:03
555 查看
环境为
mysql 5.1.39
mysql-connector-j 5.1.11
测试代码
Java代码
![](http://kabike.iteye.com/images/icon_star.png)
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
PreparedStatement ps = conn
.prepareStatement("select * from test where name=?");
ps.setInt(1, 1);
ps.execute();
Statement stmt = conn.createStatement();
stmt.execute("select * from test where name=1");
conn.commit();
conn.close();
}
private static Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db_test?useServerPrepStmts=false",
"db_test", "db_test");
conn.setAutoCommit(false);
return conn;
}
在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数量增加。
mysql 5.1.39
mysql-connector-j 5.1.11
测试代码
Java代码
![](http://kabike.iteye.com/images/icon_star.png)
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
PreparedStatement ps = conn
.prepareStatement("select * from test where name=?");
ps.setInt(1, 1);
ps.execute();
Statement stmt = conn.createStatement();
stmt.execute("select * from test where name=1");
conn.commit();
conn.close();
}
private static Connection getConnection() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db_test?useServerPrepStmts=false",
"db_test", "db_test");
conn.setAutoCommit(false);
return conn;
}
在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数量增加。
相关文章推荐
- mysql问题汇总
- jsp中利用MySQL实现分页技术
- MySQL中SELECT+UPDATE处理并发更新问题解决方案分享
- Mysql分页
- [转]淘宝内部分享:怎么跳出MySQL的10个大坑
- mysql 性能优化方案 (转)
- MySQL学习笔记
- 解压版mysql相关配置(五种常见配置需求)
- Mysql slave 同步错误解决
- MySQL重要参数收藏--以后逐步添加
- Navicat For Mysql快捷键
- MySQL几条赋权语句
- MYSQL中limit的使用
- MySQL语法大全
- mysql跨服务器数据复制功能。
- 服务器重启导致mysql同步出错
- [MySQL] 实现树形的遍历(关于多级菜单栏以及多级上下部门的查询问题)
- MYSQL 删除重复项
- MySQL连接失效
- mysql 性能分析及explain用法