我正在尝试从董事会获得COUNT(*),
但是我想知道以下代码会带来更好的性能:
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM board_*** WHERE article_category = " + foo);
OR
PreparedStatement pstmt = connection.prepareStatement("SELECT COUNT(*) FROM board_*** WHERE article_category = ?");
pstmt.setInt(1 , foo);
ResultSet rs = pstmt.executeQuery();
很难决定。
答案 0 :(得分:0)
PreparedStatement是更好的方法。本文应该帮助您绕过两者之间的区别: https://www.journaldev.com/2489/jdbc-statement-vs-preparedstatement-sql-injection-example
答案 1 :(得分:0)
使用PreparedStatement
总是很好。因为(从sql注入)更安全(从sql注入)和性能上它要快得多,因为使用绑定变量时,数据库服务器本身会缓存参数并提高查询性能。
如果您对此感兴趣的话,请读一篇好论文