jdbc连接数据库增删改查,条件查询,分层

发布时间:2021-02-22编辑:dyy阅读(909)

以对学生表进行增删改查


1、创建数据库,建立学生表

CREATE TABLE `zxc` (

  `NAME` varchar(10) DEFAULT NULL,

  `AGE` int(11) DEFAULT NULL,

  `BIRTHDAY` date DEFAULT NULL,

  `ID` int(11) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2、创建学生类

定义私有变量,对应数据库表中的字段顺序,

定义无参构造方法,带参构造方法,定义get和set方法,定义toString方法

image.pngimage.png

3、创建dao层包,定义一个

studentDao接口,定义5个抽象方法:用来查询学生信息,条件查询,新增学生信息,修改学生信息,删除学生信息,查询学生信息创建一个集合用来保存

image.png

4、创建一个接口实现类 SytudentDaoImpl,实现studentDao接口,重写里面的方法

5、创建一个StudentService接口

image.png

6、创建一个StudentService接口实现类,重写方法




一、查询信息

第一步

/*

接口实现类

 */

public class SytudentDaoImpl implements StudentDao {

    //释放资源,提前做出声明

    Connection con\= null;

    Statement statement = null;

    ResultSet resultSet = null;


    //查询所以学生信息

    @Override

    public ArrayList<Student> findAll() {


        //创建一个ArrayList集合

        ArrayList<Student> arry = new ArrayList<Student>();


        //释放资源,提前做出声明

        Connection con\= null;

        Statement statement = null;

        ResultSet resultSet = null;


        try {

            //注册驱动

            Class.forName("com.mysql.cj.jdbc.Driver");

            //获取连接

            con\= DriverManager.getConnection("jdbc:mysql://localhost:3306/qwe", "root", "123");

            //获取执行者对象

            statement = connection.createStatement();

            //执行sql语句,连接数据库

            String sql = "SELECT * FROM zxc";

            resultSet = statement.executeQuery(sql);


            //处理结果集

            while (resultSet.next()) {

                String name = resultSet.getString("name");

                Integer age = resultSet.getInt("age");

                Date birthday = resultSet.getDate("birthday");

                Integer id = resultSet.getInt("id");


                //封装Student对象,通过有参构造进行赋值

                Student stu = new Student(name, age, birthday, id);


                //将Student对象保存到集合中

                arry.add(stu);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            //对声明做出不为空的的判断,不为空才进行资源的释放

            if (connection != null) {

                try {

                    connection.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

            }


            if (statement != null) {

                try {

                    statement.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

            }


            if (resultSet != null) {

                try {

                    resultSet.close();

                } catch (SQLException e) {

                    e.printStackTrace();

                }

            }

        }

       return arry;


    }



 2、StudentServiceImpl接口实现类


public class StudentServiceImpl implements StudentService {

    private StudentDao dao=new SytudentDaoImpl();


    //查询所以学生信息

    @Override

    public ArrayList<Student> findAll() {

        return dao.findAll();

    }


3、控制层,测试方法



public class StudentControllerTsetTest {

  private StudentService studentService=new StudentServiceImpl();

  //获取所有信息

    @Test

    public void findAll() {

        ArrayList<Student> all = studentService.findAll();

        for (Student aw:all){

            System.out.println(aw);

        }

    }



二、条件查询,查询id为3的学生全部信息

1、StudentDao接口实现类

public class SytudentDaoImpl implements StudentDao {

//条件查询,根据id获取学生信息

        @Override

        public Student findById(Integer id) {

              //只需要获取单条信息,直接创建对象

            Student tw=new Student();


            //释放资源,提前做出声明

            Connection con\= null;

            Statement statement = null;

            ResultSet resultSet = null;


            try {

                //注册驱动

                Class.forName("com.mysql.cj.jdbc.Driver");

                //获取连接

                con\= DriverManager.getConnection("jdbc:mysql://localhost:3306/qwe", "root", "123");

                //获取执行者对象

                statement = connection.createStatement();

                //执行sql语句,连接数据库

                //通过id条件查询

                String sql = "SELECT * FROM zxc WHERE id='"+id+"'";

                resultSet = statement.executeQuery(sql);


                //处理结果集

                while (resultSet.next()) {

                    String name = resultSet.getString("name");

                    Integer age = resultSet.getInt("age");

                    Date birthday = resultSet.getDate("birthday");

                    Integer id1 = resultSet.getInt("id");


                   tw.setName(name);

                   tw.setAge(age);

                   tw.setBirthday(birthday);

                   tw.setID(id);

                }

            } catch (Exception e) {

                e.printStackTrace();

            } finally {

                if (connection != null) {

                    try {

                        connection.close();

                    } catch (SQLException e) {

                        e.printStackTrace();

                    }

                }


                if (statement != null) {

                    try {

                        statement.close();

                    } catch (SQLException e) {

                        e.printStackTrace();

                    }

                }


                if (resultSet != null) {

                    try {

                        resultSet.close();

                    } catch (SQLException e) {

                        e.printStackTrace();

                    }

                }

            }

            return tw;


        }


2、StudentService接口实现类

public class StudentServiceImpl implements StudentService {

    private StudentDao dao=new SytudentDaoImpl();

   //条件查询,根据id获取学生信息

   @Override

   public Student findById(Integer id) {

       return dao.findById(id);

public class StudentControllerTsetTest {
  private StudentService studentService=new StudentServiceImpl();
     //单条语句查询
       @Test
       public void findById() {
           Student byId = studentService.findById(2);
            System.out.println(byId);
    }


标签

评论