DAO设计模式

  • 实验目的

掌握DAO设计模式,实现对前台页面与后台代码的分离。

  • 实验环境

IDEA2022.2

  • 实验内容

在实验三内容的基础上,利用DAO模式重构。

  • 实验步骤

1、建立dao包,在dao包下建impl包并建立userdao类

public interface UserDao {
    //定义抽象方法user,查询user表中的所有数据信息
    List<user> getUserList();


    //测试增删改查基础操作
    int addUser(user user1);
    //修改数据信息
    int updateUser(user user1);
    //删除数据
    int deleteUser(int id);
    //参数传递,使用map传递
    int addUserByMap(Map<String , Object>map);
    //模糊查询
    List<user> getUserListByName(String name);
    //多参数传递
    //根据姓名密码进行唯一的用户信息
    user getUserByNameAndPwd(@Param("name") String name,
                             @Param("pwd") String pwd);

2、建立util包并建立dbhelp类

3、把数据库的url ,driver,username,pwd写在资源文件中

mysql.driver= com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/java2304
mysql.userName=root
mysql.password=root

4、静态代码块读取资源中的上述内容,然后写出和关闭资源

package com.BlackTea.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: Yeman
 * @Date: 2023-04-09-15:51
 * @Description: 测试Servlet另一种使用方式
 * servlet请求传递参数
 */
public class MyServlet2 extends HttpServlet {
    //用来处理前端发送的get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接收了get请求,查询了用户信息");
//接受前端传入的userid查询user用户的信息
       String userId= req.getParameter("userId");
        System.out.println("查询用户的信息id为:"+userId);

    }

    //用来处理前端发送的post请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接收了post请求,正在校验数据登录");
        //接受前端的用户名和密码进行登录校验
        String username = req.getParameter("username");
        String password =req.getParameter("password");

        if (username.equals("admin")&&password.equals("123321")){
            System.out.println("登录成功");
        }else {
            System.out.println("登录失败");
        }

    }
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
    //注册驱动
    Class.forName("com.mysql.jdbc.Driver");
    //定义连接参数
    String url="jdbc:mysql://localhost:3306/java2302?"+
            "userSSl=true&useUnicode=true&characterEncoding=utf8";
    String usernanme="root";
    String password="root";
    //获取连接
    Connection connection= DriverManager.getConnection(url,usernanme,password);
    //获取传输器
    Statement statement=connection.createStatement();
    //执行sql语句
    String insert="insert into user(name,password) values('测试1','123123')";
    String update ="update user set name='修改后名称',password='qweqwe' where id=7";
    String delete ="delete from user where id=5";

    int row=statement.executeUpdate(insert);

    //解析结果
    System.out.println("影响行数:"+row);

    //关闭资源

    statement.close();
    connection.close();
}

5、在uesrdao写出对于数据库表的添加,删除,查找,修改操作,并进行测试

添加:

修改:

删除:

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值