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

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

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

    posts - 5, comments - 14, trackbacks - 0, articles - 11

    我的DBConnection

    Posted on 2007-02-08 14:41 東舟 閱讀(1208) 評(píng)論(2)  編輯  收藏 所屬分類: J2EE

    ?

    ??1 import ?org.apache.log4j.Logger;
    ??2 import ?JAVA.sql. * ;
    ??3 import ?javax.sql.DataSource;
    ??4 import ?javax.naming.Context;
    ??5
    ??6 import ?config.ConfigBundle;
    ??7
    ??8 public ? class ?DBConnection? {
    ??9
    ?10 ? private ? static ? final ?Logger?logger? = ?Logger.getLogger(DBConnection. class );
    ?11
    ?12 ? private ? static ?DBConnection?instance? = ? null ;
    ?13 ?
    ?14 ? public ?JAVA.sql.Connection?conn? = ? null ;? // ?connection?object
    ?15
    ?16 ? public ?ResultSet?rs? = ? null ;? // ?resultset?object
    ?17
    ?18 ? public ?Statement?stmt? = ? null ;? // ?statement?object
    ?19
    ?20 ? public ?PreparedStatement?prepstmt? = ? null ;? // ?preparedstatement?object
    ?21
    ?22 ? private ?String?drivers? = ? null ;? // ?connection?parameter:drivers
    ?23
    ?24 ? private ?String?url? = ? null ;? // ?connection?parameter:url
    ?25
    ?26 ? private ?String?user? = ? null ;? // ?connection?parameter:user
    ?27
    ?28 ? private ?String?password? = ? null ;? // ?connection?parameter:password
    ?29 ?
    ?30 ? private ?String?conMode? = ? null ;
    ?31 ?
    ?32 ? public ?DataSource?ds? = ? null ;
    ?33
    ?34 ? public ?CallableStatement?callstmt? = ? null ;
    ?35 ?
    ?36 /**
    ?37 ?*?單實(shí)例
    ?38 ? */

    ?39 ? public ? static ? synchronized ?DBConnection?getInstance()? {
    ?40 ?? if ?(instance == null )
    ?41 ???instance = new ?DBConnection();
    ?42 ??
    ?43 ?? return ?instance;
    ?44 ?}

    ?45
    ?46 ? private ?DBConnection()? {
    ?47 ??conMode? = ?ConfigBundle.getString( " connction_mode " );
    ?48 ??init(conMode);
    ?49
    ?50 ?? // 使用xa協(xié)議的連接池驅(qū)動(dòng)時(shí),?AutoCommit缺省為false,?造成多處數(shù)據(jù)未提交
    ?51 ?? try ? {
    ?52 ???setAutoCommit( true );
    ?53 ??}
    ? catch ?(Exception?ex)? {
    ?54 ???logger.error( " Set?auto?commit?error! " );
    ?55 ??}

    ?56 ?}

    ?57 ?
    ?58 ? /**
    ?59 ??*?原來(lái)是采用配置文件確定構(gòu)造方式,這里我只用一種構(gòu)造方式,即conMode沒(méi)有起作用
    ?60 ??*? @param ?conMode?String
    ?61 ?? */

    ?62 ? private ? void ?init(String?conMode)? {
    ?63 ??drivers? = ?ConfigBundle.getString( " drivers " );
    ?64 ??url? = ?ConfigBundle.getString( " url " );
    ?65 ??user? = ?ConfigBundle.getString( " user " );
    ?66 ??password? = ?ConfigBundle.getString( " password " );
    ?67 ??jndiRoot? = ?ConfigBundle.getString( " jndi_root " );
    ?68 ??jndiName? = ?ConfigBundle.getString( " jndi_name " );
    ?69 ??
    ?70 ?? try ? {
    ?71 ????Class.forName(drivers);
    ?72 ????conn? = ?DriverManager.getConnection(url,?user,?password);
    ?73 ????stmt? = ?conn.createStatement();
    ?74 ??}
    ? catch ?(Exception?ex)? {
    ?75 ???logger.error( " Initialize?data?connection?error! " );
    ?76 ??}

    ?77 ?}

    ?78 ?
    ?79 ? /**
    ?80 ??*?@function?executeQuery
    ?81 ??*? @param ?sql??String
    ?82 ??*? @throws ?SQLException
    ?83 ??*? @return ?ResultSet
    ?84 ?? */

    ?85 ? public ?ResultSet?executeQuery(String?sql)? throws ?SQLException? {
    ?86 ?? if ?(stmt? != ? null )? {
    ?87 ??? return ?stmt.executeQuery(sql);
    ?88 ??}
    ? else ? {
    ?89 ??? return ? null ;
    ?90 ??}

    ?91 ?}

    ?92
    ?93 ? /**
    ?94 ??*?@function?executeUpdate
    ?95 ??*? @param ?sql??String
    ?96 ??*? @throws ?SQLException
    ?97 ?? */

    ?98 ? public ? void ?executeUpdate(String?sql)? throws ?SQLException? {
    ?99 ?? if ?(stmt? != ? null )? {
    100 ???stmt.executeUpdate(sql);
    101 ??}

    102 ?}

    103
    104 /**
    105 ??*?@function?setAutoCommit
    106 ??*? @param ?value?boolean
    107 ??*? @throws ?SQLException
    108 ?? */

    109 ? public ? void ?setAutoCommit( boolean ?value)? throws ?SQLException? {
    110 ?? this .conn.setAutoCommit(value);
    111 ?}

    112
    113 ? /**
    114 ??*?@function?commit
    115 ??*? @throws ?SQLException
    116 ?? */

    117 ? public ? void ?commit()? throws ?SQLException? {
    118 ?? this .conn.commit();
    119 ?}

    120
    121 ? /**
    122 ??*?@function?rollback
    123 ??*? @throws ?SQLException
    124 ?? */

    125 ? public ? void ?rollback()? throws ?SQLException? {
    126 ?? this .conn.rollback();
    127 ?}

    128
    129 ? /**
    130 ??*?@function?close
    131 ??*? @throws ?Exception
    132 ?? */

    133 ? public ? void ?close()? {
    134 ?? try ? {
    135 ??? if ?(rs? != ? null )? {
    136 ????rs.close();
    137 ????rs? = ? null ;
    138 ???}

    139 ??}
    ? catch ?(Exception?e)? {
    140 ???logger.error( " DBConnection?close?rs?error! " );
    141 ??}
    ? finally ? {
    142 ??? try ? {
    143 ???? if ?(stmt? != ? null )? {
    144 ?????stmt.close();
    145 ?????stmt? = ? null ;
    146 ????}

    147 ???}
    ? catch ?(Exception?e)? {
    148 ????logger.error( " DBConnection?close?stmt?error! " );
    149 ???}
    ? finally ? {
    150 ???? try ? {
    151 ????? if ?(prepstmt? != ? null )? {
    152 ??????prepstmt.close();
    153 ??????prepstmt? = ? null ;
    154 ?????}

    155 ????}
    ? catch ?(Exception?e)? {
    156 ?????logger.error( " DBConnection?close?prepstmt?error! " );
    157 ????}
    ? finally ? {
    158 ????? try ? {
    159 ?????? if ?(conn? != ? null )? {
    160 ???????conn.close();
    161 ???????conn? = ? null ;
    162 ??????}

    163 ?????}
    ? catch ?(Exception?e)? {
    164 ??????logger.error( " DBConnection?close?conn?error! " );
    165 ?????}

    166 ????}

    167 ???}

    168 ??}

    169 ?}

    170 }

    171
    172
    173

    這是個(gè)最簡(jiǎn)單的,當(dāng)然還可以添加其他內(nèi)容,比如事務(wù)處理一組SQL...
    歡迎大家批評(píng)指正。

    Feedback

    # re: 我的DBConnection  回復(fù)  更多評(píng)論   

    2007-02-08 15:23 by 馬嘉楠
    ....
    Connection ,ResultSet , Statement

    這三個(gè)每次使用之后你都不進(jìn)行關(guān)閉么?

    # re: 我的DBConnection  回復(fù)  更多評(píng)論   

    2007-02-10 12:04 by 東舟
    謝謝 馬嘉楠 的批評(píng)。

    但是調(diào)用程序里是這樣寫(xiě)的:
    DBConnection db = DBConnection.getInstance();
    String strTitle = "";
    try {
    ResultSet rs = db.executeQuery("select name from TB_PAGEFRAME where type = '1'");
    while(rs.next()){
    strTitle = rs.getString("name");
    }
    } catch (SQLException e) {
    e.printStackTrace();
    } finally {
    db.close();
    }

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 黄色一级视频免费| 国产午夜亚洲精品不卡电影| 亚洲AV无码专区国产乱码不卡| 免费无码国产V片在线观看| 久久久久久成人毛片免费看| 永久免费av无码网站大全| 色噜噜AV亚洲色一区二区| 亚洲国产成人精品无码区在线网站 | 免费毛片在线视频| 国产aⅴ无码专区亚洲av| 亚洲熟妇成人精品一区| a级特黄毛片免费观看| 女人让男人免费桶爽30分钟| 国产AV无码专区亚洲AV毛网站| 亚洲精品一卡2卡3卡四卡乱码| 国产线视频精品免费观看视频| 日本精品人妻无码免费大全| 亚洲色成人WWW永久网站| 亚洲综合激情五月色一区| 青青操免费在线观看| 日韩a级毛片免费视频| 精品亚洲成a人片在线观看| 免费人成动漫在线播放r18| 性短视频在线观看免费不卡流畅| 亚洲五月午夜免费在线视频| 性xxxx黑人与亚洲| 中文无码成人免费视频在线观看 | 国产在线国偷精品产拍免费| 亚洲一区二区女搞男| 亚洲国产成人AV在线播放| 在线免费观看你懂的| 成人午夜亚洲精品无码网站| 亚洲国产av玩弄放荡人妇| 毛片免费全部播放无码| 在线亚洲人成电影网站色www| 亚洲另类无码专区首页| 91精品免费久久久久久久久| 亚洲韩国精品无码一区二区三区| 日韩国产欧美亚洲v片| 在线观看av永久免费| 亚洲国产成人久久综合碰碰动漫3d|