Terry.Li-彬

          虛其心,可解天下之問;專其心,可治天下之學;靜其心,可悟天下之理;恒其心,可成天下之業。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            143 隨筆 :: 344 文章 :: 130 評論 :: 0 Trackbacks
          沒事查看很早以前的備份文件,發現7年前寫的一個連庫類,想起了當年的點點滴滴,原來代碼也是有生命的。


          package?com.itgenius.netoa.admin;

          import?java.sql.*;
          import?javax.sql.*;
          import?javax.naming.Context;
          import?javax.naming.InitialContext;
          import?javax.naming.*;

          import?java.util.List;

          public?class?EJBDAO?implements?ApplicationUtil?{
          ??
          private??Connection?conn=null;
          ??
          private?Statement?st=null;
          ??
          private?PreparedStatement?pst=null;
          ??
          private?CallableStatement?cs=null;
          ??
          private?ResultSet?rs=null;
          ??
          private?DataSource?ds=null;
          ??
          private?boolean?isCorrect=false;

          ??
          public?EJBDAO()?{
          ??}
          ??
          public?void?getConnection(){
          ????
          try{
          ??????Context?ctx?
          =?new?InitialContext();
          ??????ds
          =(DataSource)ctx.lookup(dsJndi);
          ??????conn
          =ds.getConnection();
          ????}
          catch?(NamingException?ex)?{
          ??????ex.printStackTrace();
          ????}
          catch(SQLException?e){
          ??????e.printStackTrace();
          ????}
          ??}
          ??
          public?void?getConnection(String?dburl,String?dbdriver,String?dbusername,String?dbpasswd){
          ??}
          ??
          public?void?getStatement(){
          ????
          try{
          ??????getConnection();
          ??????st?
          =?conn.createStatement();
          ????}
          catch?(SQLException?ex)?{
          ??????ex.printStackTrace();
          ????}
          ??}
          ??
          //得到預備狀態通道
          ??public?void?getPStatement(String?sql){
          ????
          try{
          ??????getConnection();
          ??????pst?
          =?conn.prepareStatement(sql);
          ????}
          catch?(SQLException?ex)?{
          ????}
          ??}
          ??
          //執行查詢得到結果集
          ??public?ResultSet?getResultSet(String?sql){
          ????
          try{
          ???????getStatement();
          ???????rs
          =st.executeQuery(sql);
          ????}
          catch(SQLException?ex)?{
          ????}
          finally{
          ???????
          return?rs;
          ????}
          ??}
          ??
          //給預備狀態通道中的sql的變量付值,然后執行
          ??public?boolean?executePstatement(List?list){
          ?????isCorrect
          =false;
          ?????
          try{
          ??????????
          for(int?i?=?0;?i?<?list.size();?i++){
          ????????????pst.setString((i?
          +?1),?list.get(i).toString());
          ??????????}
          ??????????pst.executeUpdate();
          ??????????isCorrect
          =true;
          ??????}
          catch?(SQLException?ex)?{
          ??????}
          finally{
          ?????????
          return?isCorrect;
          ??????}
          ??}
          ??
          //執行新增、修改、刪除
          ??public?boolean?doUpdate(String?sql){
          ????isCorrect
          =false;
          ????
          try{
          ??????getStatement();
          ??????st.executeUpdate(sql);
          ??????isCorrect
          =true;
          ????}
          catch?(SQLException?ex){
          ???????ex.printStackTrace();
          ????}
          finally{
          ??????
          return?isCorrect;
          ????}
          ??}
          ??
          //執行存儲過程
          ??public?boolean?doProcure(String?pname,List?list){
          ??????isCorrect
          =false;
          ????
          try{
          ????????getConnection();
          ???????cs
          =conn.prepareCall("{call?"+pname+"}");
          ???????
          for(int?i?=?0;?i?<?list.size();?i++)
          ???????{
          ???????????cs.setString((i?
          +?1),?list.get(i).toString());
          ???????}
          ???????cs.execute();
          ???????isCorrect
          =true;
          ????}
          catch?(SQLException?ex)?{
          ????}
          finally{
          ??????
          return?isCorrect;
          ????}
          ??}
          ??
          public?void?addBatch(List?list){
          ????
          try{
          ??????
          for(int?i=0;i<list.size();i++){
          ????????st.addBatch(list.get(i).toString());
          ??????}
          ????}
          catch(Exception?e){
          ????}
          ??}
          ??
          public?boolean?ExecuteBatch(){
          ?????isCorrect
          =false;
          ?????
          try{
          ???????st.executeBatch();
          ???????isCorrect
          =true;
          ?????}
          catch(Exception?e){
          ????????e.printStackTrace();
          ?????}
          finally{
          ????????Close();
          ????????
          return?isCorrect;
          ?????}
          ??}
          ??
          //關閉連接
          ??public?void?Close(){
          ??????
          try{
          ????????
          if(rs!=null)
          ????????{
          ??????????rs.close();
          ????????}
          ????????
          if(st!=null){
          ??????????st.close();
          ????????}
          ????????
          if(pst!=null)
          ????????{
          ??????????pst.close();
          ????????}
          ????????
          if(cs!=null)
          ??????????cs.close();
          ????????
          if(conn!=null)
          ????????conn.close();
          ??????}
          catch?(SQLException?ex)?{
          ??????}
          ??}

          }

          posted on 2013-03-01 09:52 禮物 閱讀(573) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。

          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 修水县| 光泽县| 尉犁县| 安化县| 岚皋县| 济源市| 章丘市| 怀安县| 武平县| 东乡| 本溪市| 芦山县| 贺兰县| 厦门市| 平原县| 梁河县| 察隅县| 昭苏县| 安宁市| 温泉县| 贺州市| 澄江县| 安顺市| 临武县| 武川县| 新平| 普宁市| 东海县| 西藏| 三门峡市| 平阴县| 时尚| 革吉县| 长春市| 太白县| 偏关县| 福安市| 莱州市| 蒙阴县| 类乌齐县| 奇台县|