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

            回復  更多評論
            

          主站蜘蛛池模板: 合肥市| 兰考县| 西畴县| 方正县| 怀来县| 邵武市| 营山县| 洞口县| 中山市| 佛坪县| 肇庆市| 郑州市| 沙河市| 峨边| 湘阴县| 辽宁省| 海宁市| 小金县| 孙吴县| 汾阳市| 布拖县| 青河县| 伊春市| 越西县| 阿合奇县| 启东市| 六安市| 大悟县| 盐源县| 通海县| 五家渠市| 清涧县| 班玛县| 海原县| 沿河| 汉源县| 苍山县| 哈密市| 古丈县| 祁东县| 无棣县|