最開始 我在程序里使用SQL訪問數據庫時,都是直接用“+”將字段值嵌到SQL中。?

          String?sql? = ? " select?s.name?from?student?s?where?s.id=' " ? + ?sId? + ? " ' ";


          后來我開始使用PreparedStatement,在SQL里使用“?”標記參數,然后用setXXX方法給參數賦值。當時只知道這樣用是因為有setDate,setTimeStamp這些用“+”連接不能直接嵌入的值。

          感覺PreparedStatement在性能方面應該也能快點,可能DBMS會重用,但不明底層原理。

          這次《數據庫實現》課程終于讓我弄懂了:每個SQL語句都要通過 語法分析 -> 生成邏輯查詢計劃 -> 邏輯優化 -> 物理優化 等步驟生成具體的執行計劃,用PreparedStatement就可以生成一個執行計劃,以后只是參數改變(相當于執行計劃的執行環境改變),前面的四個步驟也會省略,從而提高了效率。

          如果一個SQL語句要被反復多次執行,或多用戶同時操作,則應選用PreparedStatement!



          版權所有 羅明
          posted on 2006-11-01 20:18 羅明 閱讀(1785) 評論(19)  編輯  收藏 所屬分類: Java 、ProgramTechStudyDatabase
          Comments
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句?
            GoKu
            Posted @ 2006-11-12 10:34
            用參數傳遞的方式更安全,代碼也更易讀懂,速度也稍有提升  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            羅明
            Posted @ 2007-08-26 21:44
            還有一個原因,可能就是樓上說的安全吧^_^,今天剛發現的-_-!

            ******************************************
            評論人:liusoft 發表時間: Wed Dec 03 09:22:35 CST 2003
            千萬不要把參數值寫在SQL語句中

            如果你用 insert into table1 values(?,?,?);

            然后調用PreparedStatement.setFloat(3,234234235.234);

            就不會使得234234235.234變成2.34234235234E8字符串了
            *******************************************
            原評論見:http://www.javaresearch.org/article/10854.htm  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            傳世私服
            Posted @ 2008-05-31 22:31
            1936  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            傳奇世界私服
            Posted @ 2008-05-31 22:32
            1377  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:32
            1497  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:32
            6294  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:33
            0183  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:33
            2809  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:33
            9272  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:33
            6601  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:33
            1074  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:33
            6279  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            1367  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            3520  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            9155  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            7091  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            5462  回復  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            0216  回復  更多評論   
          • # Unikal'Nyi Kontent
            Unikal'Nyi Kontent
            Posted @ 2009-05-18 03:38
            Hi everyone. We require from buildings two kinds of goodness: first, the doing their practical duty well: then that they be graceful and pleasing in doing it.
            I am from Norway and also am speaking English, tell me right I wrote the following sentence: "So often when people encounter seo and want to start optimizing their sites they different consultants define seo differently.We offer affordable seo, ppc, search engine optimization, pay per click."

            :-) Thanks in advance. Helki.  回復  更多評論   
           
          主站蜘蛛池模板: 北安市| 开鲁县| 东乡族自治县| 靖安县| 昌宁县| 南部县| 巍山| 米泉市| 榆林市| 桂林市| 连城县| 韶关市| 上饶市| 驻马店市| 榆林市| 剑阁县| 乌拉特后旗| 怀柔区| 那坡县| 维西| 长乐市| 崇信县| 库伦旗| 德安县| 辉县市| 蓬溪县| 潞西市| 商洛市| 白河县| 报价| 扎赉特旗| 沙洋县| 叶城县| 乐都县| 永登县| 琼海市| 河南省| 突泉县| 台中县| 乡城县| 敦化市|