jfy3d(劍事)BLOG

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            37 隨筆 :: 0 文章 :: 363 評論 :: 0 Trackbacks

          為了提高寫程序效率,做了一個簡單的封裝JDBC類

          SqLModify.java

          使用方法

          SqLModify.modify("insert into usertable(username,password)values('lucy','123456')"); 
          這個是靜態方法的調用 可以執行insert   update  和直接調用jdbc方式比少寫了很多的代碼

          為了防止特殊字符 和 SQL注入可以用另外的調用方式
          SqLModify sqlcom = new SqlModify(true);  //true表示建立連接
          sqlcom.setSql("insert into usertable(username,password)values(?,?)"); 
          sqlcom.setString(1,"lucy");
          sqlcom.setString(2,"123456");
          sqlcom.exesqlandClose();

          如果是插入多條記錄可以使用循環

          SqLModify sqlcom = new SqlModify(true);  //true表示建立連接
          sqlcom.setSql("insert into usertable(username,password)values(?,?)"); 
          for(int i=0;i<30;i++)
          {
             
             sqlcom.setString(1,"lucy");
             sqlcom.setString(2,"123456");
             sqlcom.exesql();
          }
          sqlcom.closeall();




          SqlModify.java
          代碼

          import conn.DBConnManager;

          import java.sql.Connection;
          import java.sql.PreparedStatement;

          public class SqlModify
          {
           private String SQL;
           static SqlModify sqlcom = null;
           private DBConnManager conn = null;
           private Connection con = null;
           private PreparedStatement stmt = null;
           
           public SqlModify()
           {
            
           }
           public SqlModify(boolean connect)
           {
            if(connect)
             connect();
           }
           public static int modify(String sqlStr)
           {
            if(sqlcom==null)
              sqlcom = new SqlModify();
            return sqlcom.exec(sqlStr);
           }
           public PreparedStatement getStmt()
           {
            return stmt;
           }
           public void connect(String sqlStr)
           {
            try{
             connect();
             stmt = con.prepareStatement(sqlStr);
            }catch(Exception e){
             e.printStackTrace();
            }
           }
           public void connect()
           {
            try{
             conn = DBConnManager.getInstance();
             con  = conn.getConnection("mssql");
            }catch(Exception e){
             
            }
           }
           public int exesqlandClose()
           {
            int result = exesql();
            closeall();
            return result;
           }
           public int exesql()
           {
            try{
             return stmt.executeUpdate();
            }catch(Exception e){
             e.printStackTrace();
             return -1;
            }
           }
           public void setSql(String sql)
           {
            try{
             stmt = con.prepareStatement(sql);
            }catch(Exception e){
             e.printStackTrace();
            }finally{
             
            }
           }
           public int exec(String sqlStr)
           {
            int flag=-2;

            try{
             connect(sqlStr); 
             int value=stmt.executeUpdate();
             flag=value;
            }catch(java.lang.Exception ex){
             ex.printStackTrace();
            }finally{
             closeall();
            }
            return flag;
           }
           public void closeall()
           {
            try{
             if(stmt!=null)
             {
              stmt.close();
             }
             if(conn!=null)
             {
              conn.releaseConnection("mssql",con);
             }
             
            }catch(Exception e){
             e.printStackTrace();
            }
           }
           public void setString(int col,String value)
           {
            try{
             stmt.setString(col,value);
            }catch(java.lang.Exception ex){
             ex.printStackTrace();
            }  
           }
           public void setInt(int col,int value)
           {

            try{
             stmt.setInt(col,value);
            }catch(java.lang.Exception ex){
             ex.printStackTrace();
            }
             
           }
           public void setLong(int col,long value)
           {
            try{
             stmt.setLong(col,value);
            }catch(java.lang.Exception ex){
             ex.printStackTrace();
            } 
           }
           public void setFloat(int col,float value)
           {
            try{
             stmt.setFloat(col,value);
            }catch(java.lang.Exception ex){
             ex.printStackTrace();
            } 
           }
          }

          posted on 2005-09-11 20:15 劍事 閱讀(4415) 評論(3)  編輯  收藏 所屬分類: 小程序

          評論

          # re: JDBC簡單封裝,節省大量代碼 2005-09-12 11:06 xmlspy
          可以看看spring的jdbc模塊,會更方便  回復  更多評論
            

          # re: JDBC簡單封裝,節省大量代碼 2005-09-12 15:15 Xuefeng
          你的靜態方法有沒有考慮多線程問題?  回復  更多評論
            

          # re: JDBC簡單封裝,節省大量代碼 2005-09-12 18:58 劍事
          靜態方法主要是同步應用的
          如記數等 防止并發

            回復  更多評論
            

          主站蜘蛛池模板: 太原市| 连云港市| 齐河县| 沾益县| 西和县| 时尚| 鹤山市| 彰化市| 黄龙县| 北安市| 招远市| 偃师市| 共和县| 措美县| 盈江县| 顺平县| 佛坪县| 湟中县| 古蔺县| 庆云县| 通河县| 新营市| 广东省| 临沭县| 玛纳斯县| 郎溪县| 堆龙德庆县| 遂宁市| 阜阳市| 屯门区| 海伦市| 贡嘎县| 黔南| 法库县| 阳谷县| 安仁县| 宁陕县| 甘肃省| 芒康县| 麻阳| 安康市|