close

undefined

PreparedStatement 與 Statement 的差別在於 PreparedStatement 可以防止 SQL injection (SQL注入) 的惡意攻擊。

PreparedStatement 是預編譯的語法,可以先編譯接下來要重複使用的 SQL 語法,對於批次存取 DB 有很大的幫助,而Statement則是一次性的存取 DB。

PreparedStatement 與 Statement 比較如下:

  PreparedStatement Statement
預防SQL injection Yes No
適合方法 批次存取DB 一次性存取DB
寫法
  • String SQL = "SELECT * FROM table";
  • PreparedStatement preparedStatement = dbConnection.prepareStatement(SQL);
  • ResultSet resultSet2 = preparedStatement.executeQuery();
  • String SQL = "SELECT * FROM table";
  • Statement statement = dbConnection.createStatement();
  • ResultSet resultSet1 = statement.executeQuery(SQL);

 

 

[Reference]

1. http://lolikitty.pixnet.net/blog/post/203166340-java-%E4%BD%BF%E7%94%A8-prepared-statement-%E5%9F%B7%E8%A1%8C-sql-%E8%AA%9E%E6%B3%95-%28%E4%BD%BF%E7%94%A8-pos

2. http://fecbob.pixnet.net/blog/post/38077231-jdbc%E4%B8%ADstatement%E4%BA%8Epreparedstatement%E5%8D%80%E5%88%A5-

3. https://read01.com/zh-tw/OnKgA.html#.We_8KmiCxPY

arrow
arrow
    全站熱搜

    葛瑞斯肯 發表在 痞客邦 留言(1) 人氣()