[英]which one would be better between these two?
我正在嘗試從董事會獲得COUNT(*),
但我想知道以下代碼會帶來更好的性能:
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM board_*** WHERE article_category = " + foo);
要么
PreparedStatement pstmt = connection.prepareStatement("SELECT COUNT(*) FROM board_*** WHERE article_category = ?");
pstmt.setInt(1 , foo);
ResultSet rs = pstmt.executeQuery();
很難決定。
PreparedStatement是更好的方法。 本文應該幫助您繞過兩者之間的區別: https : //www.journaldev.com/2489/jdbc-statement-vs-preparedstatement-sql-injection-example
使用PreparedStatement
總是很好。 因為(從sql注入)更安全(從sql注入)和性能上它要快得多,因為使用綁定變量時,數據庫服務器本身會緩存參數並提高查詢性能。
如果您對這里更感興趣,可以閱讀一本好論文
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.