没有使用MySQL Connector/J的服务器端准备好的语句 [英] No server-side prepared statements using MySQL Connector/J
问题描述
据我了解,MySQL 5.1支持服务器端准备好的语句.因此,以下代码应准备一次该语句,然后执行10次:
From what I understand, MySQL 5.1 supports server-side prepared statements. Therefore the following code should prepare the statement once, and execute it 10 times:
Connection conn = ds.getConnection();
PreparedStatement stmt = conn.prepareStatement("SELECT COUNT(*) FROM users WHERE user_id=?");
for (int i=0; i<10; i++)
{
stmt.setString(1, "FOO"+i);
ResultSet res = stmt.executeQuery();
res.close();
}
stmt.close();
conn.close();
我在mysqld日志中看到的是直接执行的查询:
What I see instead in the mysqld log is the query being executed directly:
SELECT @@session.tx_isolation
SELECT USER()
SELECT COUNT(*) FROM users WHERE user_id='FOO0'
SELECT COUNT(*) FROM users WHERE user_id='FOO1'
SELECT COUNT(*) FROM users WHERE user_id='FOO2'
...
我也在协议日志中每次都看到完整发送的查询(使用tcpdump).
I see the query sent in the full each time in the protocol logs too (using tcpdump).
使用Connector/J 5.1.12和MySQL 5.1.44. JDBC URL中没有有趣的JDBC选项.直接进入驱动程序进行测试,没有池.
Using Connector/J 5.1.12 and MySQL 5.1.44. No funny JDBC options in the JDBC URL. Going straight to the driver for this test, no pool.
为什么不准备陈述?
推荐答案
除非使用连接参数useServerPrepStmts=true
打开真实的服务器端语句,否则Connector/J驱动程序将在本地处理准备好的语句.
The Connector/J driver handles prepared statements locally unless you turn on real server side statements using the connection parameter useServerPrepStmts=true
.
http://dev. mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
这篇关于没有使用MySQL Connector/J的服务器端准备好的语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!