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

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


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

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

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

          如果一個SQL語句要被反復(fù)多次執(zhí)行,或多用戶同時操作,則應(yīng)選用PreparedStatement!



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

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

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

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

            就不會使得234234235.234變成2.34234235234E8字符串了
            *******************************************
            原評論見:http://www.javaresearch.org/article/10854.htm  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            傳世私服
            Posted @ 2008-05-31 22:31
            1936  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            傳奇世界私服
            Posted @ 2008-05-31 22:32
            1377  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:32
            1497  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:32
            6294  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:33
            0183  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:33
            2809  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:33
            9272  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:33
            6601  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸世界私服
            Posted @ 2008-05-31 22:33
            1074  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:33
            6279  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            1367  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            3520  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            9155  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            7091  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            5462  回復(fù)  更多評論   
          • # re: 為什么用PreparedStatement而不是直接用"+"連接SQL語句
            魔獸私服
            Posted @ 2008-05-31 22:34
            0216  回復(fù)  更多評論   
          • # 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.  回復(fù)  更多評論   
           
          主站蜘蛛池模板: 台北县| 绥芬河市| 新郑市| 新宁县| 安福县| 太仆寺旗| 卢龙县| 平果县| 靖边县| 平原县| 克什克腾旗| 沈阳市| 夹江县| 和硕县| 乌拉特前旗| 麟游县| 白朗县| 元朗区| 镇原县| 高雄市| 老河口市| 托克托县| 唐山市| 通道| 三穗县| 林州市| 额济纳旗| 个旧市| 无锡市| 曲麻莱县| 尼玛县| 温州市| 河北区| 舞钢市| 剑川县| 大同市| 汤阴县| 十堰市| 亚东县| 石泉县| 阳原县|