zoukankan      html  css  js  c++  java
  • 大二寒假作业之JavaWeb

    进来发现自己在写Dao类时候,写的十分繁琐,观看了他人的Dao类写法,吸收了不少经验,减少了很多冗长的代码。

     public Collection query(String strif){
            BookCaseForm bookCaseForm1=null;
            Collection bookcaseColl=new ArrayList();
            String sql="";
            if(strif!="all" && strif!=null && strif!=""){
                sql="select * from tb_bookcase where "+strif+"";
            }else{
                sql="select * from tb_bookcase";
            }
            ResultSet rs=conn.executeQuery(sql);
            try {
                while (rs.next()) {
                    bookCaseForm1=new BookCaseForm();
                    bookCaseForm1.setId(Integer.valueOf(rs.getString(1)));
                    bookCaseForm1.setName(rs.getString(2));
                    bookcaseColl.add(bookCaseForm1);
                }
            } catch (SQLException ex) {
            }
            conn.close();
            return bookcaseColl;
        }

    首先是查询功能的实现,原先我在写查询功能时会写好几个查询函数,将条件查询与非条件查询分开了。

    这里通过strif来判断是否为调教查询,并以此来判断条件是什么,大量简化了代码。

        public int insert(BookCaseForm bookCaseForm){
        String sql1="SELECT * FROM tb_bookcase WHERE name='"+bookCaseForm.getName()+"'";
        ResultSet rs = conn.executeQuery(sql1);
        String sql = "";
        int falg = 0;
        try {
            if (rs.next()) {
                falg = 2;
            } else {
                sql ="Insert into tb_bookcase (name) values('"+bookCaseForm.getName()+"')";
                falg = conn.executeUpdate(sql);
                System.out.println("添加书架信息的SQL:" + sql);
                conn.close();
            }
        } catch (SQLException ex) {
            falg = 0;
        }
        System.out.println("falg:"+falg);
        return falg;
    }

    这里是添加的代码,在添加之前判断了添加对象是否以存在数据库中,自己在写添加是没有这一步判断,有很大的漏洞。

    这里的flag 0,1,2代表了三种情况,没有添加成功,添加成功,数据库中已存在。

  • 相关阅读:
    python note 19 异常处理
    python note 18 序列化模块
    python note 17 random、time、sys、os模块
    python note 16 re模块的使用
    python note 15 正则表达式
    python note 13 内置函数
    python note 12 生成器、推导式
    C++ int型负数除法取余问题
    Leetcode162. 寻找峰值
    Leetcode450. 删除二叉搜索树中的节点
  • 原文地址:https://www.cnblogs.com/fengchuiguobanxia/p/14297183.html
Copyright © 2011-2022 走看看