jdbc的工具类和三层架构模型

jdbc工具类

由于JDBC的注册驱动,连接数据库,关闭资源的步骤是相同的,所以我们可以写一个JDBC工具类。

/*
工具类:
私有化构造方法
提供静态方法
 */
public class jdbcUtil {
    private final static String DRIVER = "com.mysql.cj.jdbc.Driver";
    private final static String URL = "jdbc:mysql://127.0.0.1:3306/mysql_day2";
    private final static String NAME = "root";
    private final static String PASSWORD = "123456";

    //注册驱动
    static//静态代码块,类加载的时候执行,且只执行一次
    {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    //与数据库连接
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, NAME, PASSWORD);
    }

    //释放资源
    public static void Close(Statement s, Connection c, ResultSet r) {
        if (r != null) {
            try {
                r.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (s != null) {
            try {
                s.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (c != null) {
            try {
                c.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public static void Close(Statement s, Connection c)
    {
        Close(s,c,null);
    }

}

测试: 

public class testUtil {
    @Test
    public void testUpdate() {
        Connection con = null;
        PreparedStatement pstat = null;
        try {
            //获取连接
            con = jdbcUtil.getConnection();

            con.setAutoCommit(false);//开启事务

            //编写sql语句
            String sql = "update user set password=? where name=?";
            //获取数据库对象
            pstat = con.prepareStatement(sql);
            pstat.setInt(1, 23456);
            pstat.setString(2, "hhh");

            int i = pstat.executeUpdate();
            if (i > 0) {
                System.out.println("数据修改成功");
                con.commit();//提交
            }
        } catch (Exception e) {//有异常终止就回滚
            try {
                con.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        } finally {//不管有没有异常,都要关闭资源
            jdbcUtil.Close(pstat, con);
        }
    }
}

开发中常用的三层架构模型

  1. web层:接收客户端发送的数据->把接收的数据封装成对象->调用services层的方法(并传递数据对象)->根据services层方法执行结果,给客户端回馈结果
  2. service层:处理业务逻辑(会调用dao的方法)
  3. dao层:和数据库交互(底层利用jdbc技术)

在开发中三层架构通常的命名

web层 com.hhh.web

service层 com.hhh.service

dao层 com.hhh.dao

分层的目的

解耦:降低代码之间的依赖关系

可维护性:哪一层出问题,就去维护哪一层

可扩展性:哪一层需要数据,就在某一层添加数据

下面我们使用三层架构模式实现用户登录

先创建一个对象,保存用户的名字和密码

public class User {
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

web层:

public class web {
    @Test
    public void login() {
        String name = "hhh";//获取客户端发来的数据
        String password = "23456";

        //封装成对象
        User user = new User();
        user.setName(name);
        user.setPassword(password);

        //调用service层的方法,把对象发送给service
        service s=new service();
        boolean b = s.userLogin(user);
        if(b)
        {
            System.out.println("有当前用户");
        }
        else {
            System.out.println("没有此用户");
        }
    }
}

service层

public class service {
    public boolean userLogin(User loginUser)
    {
        //校验:传递的数据是否合法
        if(loginUser==null)
        {
            throw new RuntimeException("传递的参数为null");
        }

        //调用dao层的方法
        dao d=new dao();
        User login = d.login(loginUser);
        if(login!=null)
        {
            return true;
        }
        return false;
    }
}

Dao层

public class dao {
    public User login(User loginUser)
    {
        User u=null;

        Connection con=null;
        PreparedStatement pstat=null;
        ResultSet rs=null;
        try{
            con=jdbcUtil.getConnection();  //获取连接
            String sql="select name,password from user where name=? and password=?";
             pstat = con.prepareStatement(sql);
             pstat.setString(1,loginUser.getName());
             pstat.setString(2,loginUser.getPassword());
             rs = pstat.executeQuery();
             if(rs!=null&&rs.next())
             {
                 String name=rs.getString("name");
                 String password=rs.getString("password");
                 u=new User(name,password);
             }
        }catch (Exception e)
        {
            e.printStackTrace();
        }finally {
            jdbcUtil.Close(pstat,con,rs);
        }
        return u;//如果没有数据返回的是null
    }
}

数据库连接池

数据库连接池是一个容器,负责分配,管理数据库连接

它允许应用程序重复使用一个现有的数据库连接,而不是重新建立一个

好处:

资源重用

提升系统响应速度

相关推荐

  1. jdbc工具架构模型

    2024-04-30 07:08:03       11 阅读
  2. JDBC高级篇-JDBC工具、DAO封装BaseDAO工具

    2024-04-30 07:08:03       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-30 07:08:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-30 07:08:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-30 07:08:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-30 07:08:03       20 阅读

热门阅读

  1. EureKa详解

    2024-04-30 07:08:03       13 阅读
  2. Docker in Docker原理与实战

    2024-04-30 07:08:03       9 阅读
  3. Golang 程序运行报汇编错误

    2024-04-30 07:08:03       9 阅读
  4. Chrome 插件如何开发?

    2024-04-30 07:08:03       14 阅读
  5. npm详解

    2024-04-30 07:08:03       14 阅读
  6. 贪心算法基础题(第三十三天)

    2024-04-30 07:08:03       11 阅读
  7. 从零手写实现 apache Tomcat-01-入门介绍

    2024-04-30 07:08:03       10 阅读
  8. 微信小程序实现用户手机号授权

    2024-04-30 07:08:03       13 阅读
  9. 人工智能论文:GPT, GPT-2, GPT-3 对比和演进的思路

    2024-04-30 07:08:03       11 阅读
  10. webpack 区分环境

    2024-04-30 07:08:03       10 阅读