JDBC2

作者: 岁月是首歌 | 来源:发表于2017-04-18 18:12 被阅读0次

    jdbcUtils

    1.四大参数的出现了硬编码

    static String className;
        static String url;
        static String user;
        static String password;
    
         static  {
            try {
                Properties p = new Properties();
                p.load(new FileInputStream("jdbc.properties"));
                className = p.getProperty("className");
                url = p.getProperty("url");
                user = p.getProperty("user");
                password = p.getProperty("password");
                Class.forName(className);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static Connection getConnection() throws SQLException{
                  Connection connection = DriverManager.getConnection(url,user,password);
                  return connection;
        }
    

    2.sql字符串拼接:不方便,sql有注入
    通过prepareStatement防止sql注入

    String sql="select * from user where username=? and password=?";
            PreparedStatement ps = con.prepareStatement(sql);
            for(int i=0;i<value.length;i++){
            ps.setObject(i+1,value[i]);
            }
            ResultSet rs = ps.executeQuery();
    

    3.javabean的封装不方便
    泛型+反射

    注意:类中的数据名要和表中的完全相同

    //得到单个对象
        if(!rs.next()){
                return null;
            }
            T obj = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                String name = field.getName();
                field.setAccessible(true);
                if(field.getType()==int.class){
                int value1=rs.getInt(name);
                    field.set(obj, value1);
                }
                else if(field.getType()==String.class){
                    String  value1=rs.getString(name);
                    field.set(obj, value1);
                }
                else if(field.getType()==float.class){
                    float   value1=rs.getFloat(name);
                    field.set(obj, value1);
                }
                else{
                    double  value1=rs.getDouble(name);
                    field.set(obj, value1);
                }
                
            }
            return obj;
    
    //得到对象的集合
        ArrayList<T> list = new ArrayList<T>();
            Connection con = getConnection();
            PreparedStatement ps = con.prepareStatement(sql);
            for (int i = 0; i < value.length; i++) {
                ps.setObject(i + 1, value[i]);
            }
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                T obj = getBean(clazz, rs);
                list.add(obj);
            }
            return list;
        }
    

    查找单个数

      if(rs.next()){
                return rs.getObject(1);
            }
            return null;
                String sql="select count(*) from user";
            Number num = (Number)JdbcUtils.selectNum(sql);
            int i = num.intValue();
            System.out.println(i);
    

    查询封装

            ArrayList list = new ArrayList();
            String sql = "update user set id=?";
            list.add(user.getId());
            if (user.getUsername() != null &&!"".equals(user.getUsername())) {
                 sql=sql+",username=?";
                 list.add(user.getUsername());
            }
            if (user.getPassword()!= null &&!"".equals(user.getPassword())) {
                sql=sql+",password=?";
                list.add(user.getPassword());
            }
            sql=sql+" where id="+user.getId();
            int i = JdbcUtils.update(sql, list.toArray());
            return i;
        }
    

    增删改封装

            ArrayList list = new ArrayList();
            String sql = "select * from user where 1=1";
            if (user.getUsername() != null && !"".equals(user.getUsername())) {
                sql = sql + " and username like ?";
                list.add("%" + user.getUsername() + "%");
            }
            if (user.getPassword() != null && !"".equals(user.getPassword())) {
                sql = sql + " and password like ?";
                list.add("%" + user.getPassword() + "%");
            }
            System.out.println(sql);
            System.out.println(list);
            ArrayList<User> list2 = JdbcUtils.toList(User.class, sql,
                    list.toArray());
            return list2;
    

    配置文件

    property是map与io流(FilleInputStream)的结合

    Property p=new Property();//创建对象
    p.load(new FileInputStream("文件名"));//读取文件对象
    Object obj=p.getProperty(key)//传入key得到文件中对应的对象
    

    相关文章

      网友评论

          本文标题:JDBC2

          本文链接:https://www.haomeiwen.com/subject/ndrqzttx.html