Servlet--小案例

结合所学的Servlet知识点及web三层架构,做一个简单的登录小案例。

以及输入账号密码登录后跳转的主页面

登录需要从数据库校验对应数据,通过jdbc进行连接数据库,配置jdbc。

配置jdbc.properties

在resource中添加jdbc.properties配置文件

driverClass = com.mysql.cj.jdbc.Driver
username = root
password = 123456
url = jdbc:mysql://localhost:3306/whn1?characterEncoding=UTF-8

加入Config配置类

创建util包,加入config配置类

package com.by.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class Config {
    private static String username;
    private static String password;
    private static String url;
    private static String driverClass;

    static{
        //特殊Hashtable
        Properties prop = new Properties();
        try {
            //读取类路径下的配置文件
            InputStream is = Config.class.getClassLoader().getResourceAsStream("jdbc.properties");
            //InputStream in = new FileInputStream("javaweb\\user_mgr\\src\\main\\resources\\jdbc.properties");
            //加载配置文件内容
            prop.load(is);
            // acquire the properties from file
            username = prop.getProperty("username");
            password = prop.getProperty("password");
            url = prop.getProperty("url");
            driverClass = prop.getProperty("driverClass");
            // register the driver
            if(driverClass != null && url != null && username != null && password != null){
                //加载驱动
                Class.forName(driverClass);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值