Java Web ——实现登录注册(Servlet+Jdbc)

Web三层架构Web层——转发jsp页面,封装数据传给Service层处理Service层——接受Web层传来的数据,处理业务逻辑Dao层——实现对数据库的操作,提供接口给Service层调用三层架构有利于更好的解耦,不同层各司其职。开发顺序这里我是置底向上的开发。目录结构cn.login.dao.implcn.login.daocn.login.exceptioncn....
摘要由CSDN通过智能技术生成

Web三层架构

  • Web层——转发jsp页面,封装数据传给Service层处理
  • Service层——接受Web层传来的数据,处理业务逻辑
  • Dao层——实现对数据库的操作,提供接口给Service层调用
    三层架构有利于更好的解耦,不同层各司其职。

开发顺序

这里我是置底向上的开发。
目录结构

cn.login.dao.impl
cn.login.dao
cn.login.exception
cn.login.impl
cn.login.service
cn.login.utils
cn.login.web

jdbc工具类

封装了常用的操作jdbc的方法,以便dao层来调用.

使用了java中的.properties文件来设置配置信息

public class JdbcUtils{
    private static String url ;
    private static String dirver ;
    private static String user ;
    private static String password ;

    static{//在类第一次加载时,会执行块中的代码
        Properties properties =new Properties();
    try(InputStream in = new FileInputStream((String.valueOf(JdbcUtils.class.getResource("db.properties").getPath()))))  //自动关闭资源,非常重要
    {    //加载配置文件
            properties.load(in);
        }
    catch (IOException e) {
            e.printStackTrace();
        }
    //获取配置文件的信息
    url = properties.getProperty("URL");
    dirver = properties.getProperty("DRIVER");
    user = properties.getProperty("USER");
    password = properties.getProperty("PASSWORD");

        try {
            Class.forName(dirver);//加载驱动
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //静态方法就不需要创建实例来调用了
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }

    public static void release(Connection connection, Statement statement, ResultSet resultSet){
        if(resultSet != null)
            try{
  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值