<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無線&移動互聯網技術研發

    換位思考·····
    posts - 19, comments - 53, trackbacks - 0, articles - 283
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
    這個是我在wap項目中用到的DB BEAN。
    這個看起來,使用起來都會很方便。感覺封裝的很好,其實不然。這里用的并不是SQL預編譯,PreparedStatement。每每在使用的時候都是直接合成后送到這里。安全性非常的不好。很容易引起SQL注入性侵入。另外這樣寫的通用性非常的不好。表現的最典型的就是update。下面先看做法,再貼兩個使用的實例:


    package com.yixun.wap;

    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;

    /** *//** *//** *//**
     * 
     * @descripte get DB connection
     * 
    @author Gavin.lee
     * @date 2009-5-18下午03:11:34
     * 
    @version 1.0
     *
     
    */


    public class DBBean {
        
    private String Driver = "";
        
    private String Url ="";
        
    private String Username ="";
        
    private String Password ="";
        
    public CallableStatement cstmt;
        
    private Statement statement = null;
        
    private Connection conn = null;
        
    private ResultSet rs = null;

        
    public DBBean() {
            Properties prop 
    = new Properties();
            
    try {
                
    //load class by absolute path
                InputStream is = Class.forName("com.yixun.wap.DBBean").getResourceAsStream("/dbsource.properties");
                prop.load(is);
                
    if (is != null{
                    is.close();
                }


                Driver 
    = prop.getProperty("Driver");
                Url 
    = prop.getProperty("Url");
                Username 
    = prop.getProperty("Username");
                Password 
    = prop.getProperty("Password");

                System.setProperty(
    "jdbc.drivers", Driver);
                Class.forName(Driver);
                conn 
    = DriverManager.getConnection(Url,Username,Password);
                
            }
     catch (ClassNotFoundException e) {
                System.out.println(
    "Unable to load driver.\n" + e.getMessage());
            }
     catch (IOException e) {
                System.out.println(
    "Unable to read File stream.\n" + e.getMessage());
            }
     catch (SQLException e) {
                System.out.println(
    "Unable to get the connection.\n" + e.getMessage());
            }

        }

        
        
    public Connection getConnection(){             
            
    return (conn);
        }

        
        
    public ResultSet executeQuery(String sql) {
            rs 
    = null;
            
    try {
                statement 
    = conn.createStatement();
                rs 
    = statement.executeQuery(sql);
            }
     catch (SQLException ex) {
                System.err.println(
    "aq.executeQuery: " + ex.getMessage());
                System.err.println(
    "aq.executeQuerystrSQL: " + sql);
            }

            
    return rs;
        }

        
        
    public void executeUpdate(String sql) {
            
    try {
                statement 
    = conn.createStatement();
                statement.executeUpdate(sql);
            }
     catch (SQLException ex) {
                System.err.println(
    "aq.executeUpdate: " + ex.getMessage());
                System.err.println(
    "aq.executeUpadatestrSQL: " + sql);
            }

        }

        
        
    public ResultSet executeWapProc(String procName, Object[] params) {
            rs 
    = null;
            
    try {
                
    int index = 0;
                cstmt 
    = conn.prepareCall(procName);
                
    for (Object obj : params) {
                    index
    ++;
                    cstmt.setObject(index, obj);
                }

                rs 
    = cstmt.executeQuery();
            }
     catch (SQLException ex) {
                System.err.println(
    "NewsDbBean**:" + ex.getMessage());
            }

            
    return rs;
        }


        
    public void destroy() {
            
    try {
                
    if(statement != null)
                statement.close();
                
    if(cstmt != null)
                cstmt.close();
                conn.close();
            }
     catch (Exception e) {
                e.printStackTrace();
            }

        }

    }




    dbsource.properties文件:
    #dbsource.properties
    Driver = net.sourceforge.jtds.jdbc.Driver
    Url = jdbc:jtds:sqlserver://121.14.110.49:1433/wubai_wapcp
    Username = wap
    Password = esunxyz500wan!^wap



    使用實例:

    DBBean db  = new DBBean();
    String sql1 
    = "select cpusername,cppassword from logininterface where cpsid='"+ this.ck + "'";

    ResultSet rs 
    =null;
      
    try{
       rs 
    = db.executeQuery(query);
       
    while(rs.next()){
        un
    =rs.getString("cpusername");
        pw
    =rs.getString("cppassword");
       }

       
      }
    catch(Exception e){
       System.err.println(e.getMessage());
      }


    String sql2 
    = "insert into BuyService values('"+un+"','"+pw+"','"+0+"','"+this.lotid+"','"+this.playid+"','"+this.expect+"','"+this.allmoney+"','"+this.zhushu+"','"+this.beishu+"','"+this.title+"','"+this.codes+"','"+this.ck+"','"+this.dest_src+"','"+this.R_des+"','"+this.R_um+"','"+this.R_id+"',getdate(),'"+super.getHZFID()+"')";
    try{
      db.executeUpdate(inset);
      }
    catch(Exception e){
       e.printStackTrace();
      }
    finally{
       rs
    =null;
       db.destroy();
      }

    存儲過程的使用:"在sql server 中:exec wap_biz_addContentClick'001'"
        public void addClick(String id){
            NewsDbBean dbBean 
    = new NewsDbBean();
            Object[] ob 
    = new Object[1];
            ob[
    0]=id;
            
    try {
                String procName
    ="{call wap_biz_addContentClick(?)}";
                dbBean.executeWapProc(procName, ob);
            }
     catch (Exception e) {
                e.printStackTrace();
            }
    finally{
                dbBean.destroy();
            }

        }

    update這里我就不寫了,操作類似上面,就因為它沒有使用預編譯sql,每需要一次跟新,就需要一條sql語句。所以通用性非常不好。
    主站蜘蛛池模板: 1000部啪啪毛片免费看| 亚洲国产精彩中文乱码AV| 3344免费播放观看视频| 色婷婷综合缴情综免费观看| 亚洲校园春色另类激情| 亚洲精品午夜国产VA久久成人| 女人张开腿等男人桶免费视频 | a级在线免费观看| 亚洲国产av玩弄放荡人妇| 亚洲国产精品综合福利专区| 日本红怡院亚洲红怡院最新| 亚洲国产V高清在线观看| 免费黄色app网站| 亚洲精品视频免费看| 日本人成在线视频免费播放| igao激情在线视频免费| 国产尤物在线视精品在亚洲| 亚洲精品无码久久久久秋霞 | 国产免费一区二区三区| 先锋影音资源片午夜在线观看视频免费播放| 蜜芽亚洲av无码一区二区三区 | 毛片免费在线视频| 午夜性色一区二区三区免费不卡视频| 日本免费中文字幕| 久久久久国色av免费看| 暖暖在线视频免费视频| 手机看片国产免费永久| a级毛片毛片免费观看久潮喷| 国产特黄一级一片免费| 曰韩无码AV片免费播放不卡| 黄色a三级免费看| 青青视频免费在线| 少妇亚洲免费精品| 新最免费影视大全在线播放| 色妞www精品视频免费看| 国产精品亚洲а∨天堂2021| 鲁啊鲁在线视频免费播放| 黄网站色视频免费观看45分钟| 亚洲精品色在线网站| 男女污污污超污视频免费在线看| 夜夜爽妓女8888视频免费观看|