发表于: 2017-05-06 18:55:13

1 1327


Task1第六天

今日计划

继续学习JDBC,编写dao

今日完成

jdbc的增加操作

jdbc的删除操作

jdbc的修改操作

jdbc的查询操作

学习PreparedStatement

相比statement,preparedstatement便于参数设置,可读性好。预编译机制,性能更快。防止sql注入攻击。

学习ORM

编写DAO

StudentDAO接口,定义学生相关操作

  1. /*

 * StudentDAO接口,定义学生相关的操作

 */

public interface StudentDAO {

    //添加学生

    public void addStudent(Student stu);

    //删除学生

    public void deleteStudent(string name);

    //修改学生

    public void updateStudent(Student stu);

    //查询所有学生

    public List<Student> getAllStudents();

    //根据姓名查询学生

    public Student getStudentByname(string name);

    //根据条件模糊查询

    public List<Student> getStudentsByCondition(String qq,String profession);

}


StudentDaoimpl实现类,实现相关的操作

/*

 * StudentDAOImpl实现类,实现相关的操作

 */

public class StudentDAOImpl implements StudentDAO {

    Connection conn=null;

    PreparedStatement pstmt=null;

    ResultSet rs=null;


    @Override

    public void addStudent(Student stu) {

        String sql="insert into student values (null,?,?,?,?,?,?,?,?,?,?,?,?,?)";

        try {

            conn=DBUtil.getConnection();

            pstmt=conn.prepareStatement(sql);

            pstmt.setString(1, stu.getName());

            pstmt.setString(2, stu.getQq());

            pstmt.setString(3, stu.getProfession());

            pstmt.setInt(4, stu.getJoin_datr());

            pstmt.setString(5, stu.getSchool());

            pstmt.setString(6, stu.getOnline_class());

            pstmt.setString(7, stu.getOnline_id());

            pstmt.setString(8, stu.getDdaily_url());

            pstmt.setString(9, stu.getDeclaration());

            pstmt.setString(10, stu.getIntroducer());

            pstmt.setString(11, stu.getReferee());

            pstmt.setString(12, stu.getCounselor());

            pstmt.setString(13, stu.getResource());

            pstmt.executeUpdate();

            System.out.println("添加學生成功!");

        } catch (SQLException e) {

            e.printStackTrace();

        }finally{

            DBUtil.closeAll(rs, pstmt, conn);

        }

    }

    @Override

    public void deleteStudent(string name) {

        String sql="delete from student where name=?";

        try {

            conn=DBUtil.getConnection();

            pstmt=conn.prepareStatement(sql);

            pstmt.setObject(1, id);

            pstmt.executeUpdate();

            System.out.println("删除学生成功!");

        } catch (SQLException e) {

            e.printStackTrace();

        }finally{

            DBUtil.closeAll(rs, pstmt, conn);

        }

    }

    @Override

    public void updateStudent(Student stu) {

        String sql="update student set qq=?,profession=?,join_date=?,school=?,online_class=?,online_id=?,daily_url=?declaration=?,introducer=?,referee-?,counselor=?,resource=? where name=?";

        try {

            conn=DBUtil.getConnection();

            pstmt=conn.prepareStatement(sql);

            pstmt.setString(2, stu.getQq());

            pstmt.setString(3, stu.getProfession());

            pstmt.setInt(4, stu.getJoin_datr());

            pstmt.setString(5, stu.getSchool());

            pstmt.setString(6, stu.getOnline_class());

            pstmt.setString(7, stu.getOnline_id());

            pstmt.setString(8, stu.getDdaily_url());

            pstmt.setString(9, stu.getDeclaration());

            pstmt.setString(10, stu.getIntroducer());

            pstmt.setString(11, stu.getReferee());

            pstmt.setString(12, stu.getCounselor());

            pstmt.setString(13, stu.getResource());

            pstmt.executeUpdate();

            System.out.println("修改学生成功!");

        } catch (SQLException e) {

            e.printStackTrace();

        }finally{

            DBUtil.closeAll(rs, pstmt, conn);

        }

    }

    @Override

    public List<Student> getAllStudents() {

        List<Student> students=new ArrayList<Student>();

        String sql="select * from student";

        try {

            conn=DBUtil.getConnection();

            pstmt=conn.prepareStatement(sql);

            rs=pstmt.executeQuery();

            while(rs.next()){

                Student stu=new Student(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10),rs.getString(11),rs.getString(12)rs.getString(13), rs.getString(14));

                students.add(stu);

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }finally{

            DBUtil.closeAll(rs, pstmt, conn);

        }

        return students;

    }

    @Override

    public Student getStudentById(int id) {

        Student stu=null;

        String sql="select * from student where name=?";

        try {

            conn=DBUtil.getConnection();

            pstmt=conn.prepareStatement(sql);

            pstmt.setInt(1, id);

            rs=pstmt.executeQuery();

            if(rs.next()){

                stu=new Student(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10),rs.getString(11),rs.getString(12)rs.getString(13), rs.getString(14));

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }finally{

            DBUtil.closeAll(rs, pstmt, conn);

        }

        return stu;

    }

    @Override

    public List<Student> getStudentsByCondition(String qq, String profession) {

        List<Student> students=new ArrayList<Student>();

        String sql="select * from student where qq like ? and profession=?";

        try {

            conn=DBUtil.getConnection();

            pstmt=conn.prepareStatement(sql);

            pstmt.setObject(1, qq);

            pstmt.setObject(2, profession);

            rs=pstmt.executeQuery();

            while(rs.next()){

                Student stu=new Student(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7), rs.getString(8), rs.getString(9), rs.getString(10),rs.getString(11),rs.getString(12)rs.getString(13), rs.getString(14));

                students.add(stu);

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }finally{

            DBUtil.closeAll(rs, pstmt, conn);

        }

        return students;

    }

}


实体类Student Bean


public class Student {

    private int id;

    private String name;

    private String qq;

    private String profession;

    private String join_date;

    private String school;

    private String online_class;

    private String online_id;

    private String daily_url;

    private String declaration;

    private String introducer;

    private String referee;

    private String counselor;

    private String resource;

    public Student() {

        super();

    }

    public Student(int id, String name, int qq, String profession, String join_date,String school,  String online_class, String online_id,

                 String daily_url,String declaration,String introducer,String referee,String counselor,

                  String resource) {

        super();

        this.id = id;

        this.name = name;

        this.qq = qq;

        this.profession = profession;

        this.join_date = join_date;

        this.school = school;

        this.online_class = online_class;

        this.online_id = online_id;

        this.daily_url = daily_url;

        this.declaration = declaration;

        this.introducer = introducer;

        this.referee = referee;

        this.counselor = counselor;

        this.resource = resource;

    }

    public Student(String name, int qq, String profession, String join_date,String school,  String online_class, String online_id,

                  String daily_url,String declaration,String introducer,String referee,String counselor,

            String resource) {

        super();

        this.name = name;

        this.qq = qq;

        this.profession = profession;

        this.join_date = join_date;

        this.school = school;

        this.online_class = online_class;

        this.online_id = online_id;

        this.daily_url = daily_url;

        this.declaration = declaration;

        this.introducer = introducer;

        this.referee = referee;

        this.counselor = counselor;

        this.resource = resource;

    }

    public int getId() {

        return id;

    }

    public void setId(int id) {

        this.id = id;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public int getQq() {

        return qq;

    }

    public void setQq(string qq) {

        this.qq = qq;

    }

    public String getProfession() {

        return profession;

    }

    public void setProfession(String profession) {

        this.profession = profession;

    }

    

    }

}


测试类


数据库工具类


明日计划

学习Jnuit,Spring,并进行单元测试


问题总结

编写dao耗费了大量时间,不知道其他师兄有没有好的办法应对


收获

搞清楚了dao层的结构,学会了JDBC原始DAO编写方法



返回列表 返回列表
评论

    分享到