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 劍事 閱讀(4393) 評論(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 劍事
          靜態方法主要是同步應用的
          如記數等 防止并發

            回復  更多評論
            

          主站蜘蛛池模板: 雷山县| 山阴县| 平乐县| 盐城市| 漳浦县| 勃利县| 海伦市| 建湖县| 北碚区| 阳江市| 湘乡市| 台前县| 临猗县| 建湖县| 丘北县| 龙州县| 扬州市| 塘沽区| 道孚县| 延寿县| 江孜县| 彰武县| 林芝县| 财经| 丰都县| 彩票| 大庆市| 固始县| 崇信县| 时尚| 凌海市| 安吉县| 玉山县| 志丹县| 巫山县| 高要市| 获嘉县| 沾化县| 宁国市| 宝山区| 阿巴嘎旗|