分享第二十四篇励志语句
对自己狠一点,逼自己努力,再过五年你将会感谢今天发狠的自己,恨透今天懒惰自卑的自己,既然认准了一条路,那就坚持走下去,命运不会辜负每一个认真而努力的人。
目录
在MySQL中新建一个servletdatabase数据库,专门用来学习servlet操作数据库
1 案例需求
实现登录功能,登录成功后显示所有管理员信息,登录失败给出“账号或密码错误,无法登录”提示信息
2 创建表admin并添加数据
#创建表adminCREATE TABLE IF NOT EXISTS `admin`(`username` VARCHAR(20) PRIMARY KEY,`password` VARCHAR(20) NOT NULL,`phone` VARCHAR(11) UNIQUE NOT NULL,`address` VARCHAR(20) NOT NULL);#向admin表中插入数据INSERT INTO `admin`(`username`,`password`,`phone`,`address`)VALUES('张三','123456','13112345678','安徽合肥蜀山区');INSERT INTO `admin`(`username`,`password`,`phone`,`address`)VALUES('李四','123456','13822334455','安徽合肥高新区');
3 创建Web项目
创建Web项目adminProject01,在项目下创建包目录结构如下,并导入相关jar包及配置文件
-
com.cxyzxc.www.dao包:数据访问层接口
-
com.cxyzxc.https://blog.csdn.net/swy2560666141/article/details/www.dao.impl包:数据访问层接口实现类
-
com.cxyzxc.www.entity包:实体类
-
com.cxyzxc.www.service包:业务逻辑层接口
-
com.cxyzxc.https://blog.csdn.net/swy2560666141/article/details/www.service.impl包:业务逻辑层接口实现类
-
com.cxyzxc.www.servlet包:Servlet类
-
com.cxyzxc.www.utils包:工具类
-
database.properties:数据库连接及连接池配置文件
4 database.properties文件
5 DBUtile类代码
package com.cxyzxc.www.utils;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import javax.sql.DataSource;import com.alibaba.druid.pool.DruidDataSource;import com.alibaba.druid.pool.DruidDataSourceFactory;public class DBUtils {// 声明一个连接池对象private static DruidDataSource druidDataSource;static {// 实例化配置文件对象Properties properties = new Properties();try {// 加载配置文件内容InputStream is = DBUtils.class.getResourceAsStream("/database.properties");properties.load(is);// 创建连接池druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}//返回一个数据源public static DataSource getDataSource(){return druidDataSource;}}
6 Admin实体类
package com.cxyzxc.www.entity;public class Admin { private String username; private String password; private String phone; private String address; public Admin() { } public Admin(String username, String password, String phone, String address) { this.username = username; this.password = password; this.phone = phone; this.address = address; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Admin{" + "username='" + username + '\'' + ", password='" + password + '\'' + ", phone='" + phone + '\'' + ", address='" + address + '\'' + '}'; }}
7 AdminDao接口
package com.cxyzxc.www.dao;import com.cxyzxc.www.entity.Admin;import java.util.List;public interface AdminDao { //查询用户(查询单个) Admin selectOneByUsernameAndPassword(String username, String password); //查询所有用户 List selectAll();}
8 AdminDaoImpl实现类
package com.cxyzxc.https://blog.csdn.net/swy2560666141/article/details/www.dao.impl;import com.cxyzxc.www.dao.AdminDao;import com.cxyzxc.www.entity.Admin;import com.cxyzxc.www.utils.DBUtils;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import java.sql.SQLException;import java.util.List;public class AdminDaoImpl implements AdminDao { // 创建QueryRunner对象,并传递一个数据源对象 private final QueryRunner QUERYRUNNER = new QueryRunner(DBUtils.getDataSource()); @Override public Admin selectOneByUsernameAndPassword(String username, String password) { String sql = "SELECT * FROM `admin` WHERE `username` = ? AND `PASSWORD`=?;"; Object[] args = {username, password}; try { return QUERYRUNNER.query(sql, new BeanHandler(Admin.class), args); } catch (SQLException e) { e.printStackTrace(); } return null; } @Override public List selectAll() { String sql = "SELECT * FROM `admin`;"; try { return QUERYRUNNER.query(sql, new BeanListHandler(Admin.class)); } catch (SQLException e) { e.printStackTrace(); } return null; }}
9 AdminService接口
package com.cxyzxc.www.service;import com.cxyzxc.www.entity.Admin;import java.util.List;public interface AdminService { Admin login(String username, String password); List selectAllAdmin();}
10 AdminServiceImpl实现类
package com.cxyzxc.https://blog.csdn.net/swy2560666141/article/details/www.service.impl;import com.cxyzxc.www.service.AdminService;import com.cxyzxc.www.dao.AdminDao;import com.cxyzxc.https://blog.csdn.net/swy2560666141/article/details/www.dao.impl.AdminDaoImpl;import com.cxyzxc.www.entity.Admin;import java.util.List;public class AdminServiceImpl implements AdminService { private final AdminDao ADMINDAO = new AdminDaoImpl(); @Override public Admin login(String username, String password) { return ADMINDAO.selectOneByUsernameAndPassword(username, password); } @Override public List selectAllAdmin() { return ADMINDAO.selectAll(); }}
11 HTML页面
11.1 login.html页面
登录页面 来源地址:https://blog.csdn.net/swy2560666141/article/details/128465946