小言_互联网的博客

【Servlet+JDBC+HTML+MySQL+】------查询综合案例

357人阅读  评论(0)

分享第二十四篇励志语句

对自己狠一点,逼自己努力,再过五年你将会感谢今天发狠的自己,恨透今天懒惰自卑的自己,既然认准了一条路,那就坚持走下去,命运不会辜负每一个认真而努力的人。

 

目录

分享第二十四篇励志语句

1 案例需求

2 创建表admin并添加数据

3 创建Web项目

4 database.properties文件

 

5 DBUtile类代码

6 Admin实体类

7 AdminDao接口

8 AdminDaoImpl实现类

9 AdminService接口

10 AdminServiceImpl实现类

11 HTML页面

11.1 login.html页面

11.2 login.css

11.3 table.css

 

12 LoginServlet

13 ShowAllAdminServlet


在MySQL中新建一个servletdatabase数据库,专门用来学习servlet操作数据库  

 

1 案例需求

实现登录功能,登录成功后显示所有管理员信息,登录失败给出“账号或密码错误,无法登录”提示信息

2 创建表admin并添加数据


  
  1. #创建表admin
  2. CREATE TABLE IF NOT EXISTS `admin`(
  3. `username` VARCHAR( 20) PRIMARY KEY,
  4. `password` VARCHAR( 20) NOT NULL,
  5. `phone` VARCHAR( 11) UNIQUE NOT NULL,
  6. `address` VARCHAR( 20) NOT NULL
  7. );
  8. #向admin表中插入数据
  9. INSERT INTO `admin`(`username`,`password`,`phone`,`address`)
  10. VALUES( '张三', '123456', '13112345678', '安徽合肥蜀山区');
  11. INSERT INTO `admin`(`username`,`password`,`phone`,`address`)
  12. VALUES( '李四', '123456', '13822334455', '安徽合肥高新区');

 

3 创建Web项目

创建Web项目adminProject01,在项目下创建包目录结构如下,并导入相关jar包及配置文件

  • com.cxyzxc.www.dao包:数据访问层接口

  • com.cxyzxc.www.dao.impl包:数据访问层接口实现类

  • com.cxyzxc.www.entity包:实体类

  • com.cxyzxc.www.service包:业务逻辑层接口

  • com.cxyzxc.www.service.impl包:业务逻辑层接口实现类

  • com.cxyzxc.www.servlet包:Servlet类

  • com.cxyzxc.www.utils包:工具类

  • database.properties:数据库连接及连接池配置文件

4 database.properties文件

 

5 DBUtile类代码


  
  1. package com.cxyzxc.www.utils;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.Properties;
  5. import javax.sql.DataSource;
  6. import com.alibaba.druid.pool.DruidDataSource;
  7. import com.alibaba.druid.pool.DruidDataSourceFactory;
  8. public class DBUtils {
  9. // 声明一个连接池对象
  10. private static DruidDataSource druidDataSource;
  11. static {
  12. // 实例化配置文件对象
  13. Properties properties = new Properties();
  14. try {
  15. // 加载配置文件内容
  16. InputStream is = DBUtils.class
  17. .getResourceAsStream( "/database.properties");
  18. properties.load(is);
  19. // 创建连接池
  20. druidDataSource = (DruidDataSource) DruidDataSourceFactory
  21. .createDataSource(properties);
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. //返回一个数据源
  29. public static DataSource getDataSource (){
  30. return druidDataSource;
  31. }
  32. }

6 Admin实体类


  
  1. package com.cxyzxc.www.entity;
  2. public class Admin {
  3. private String username;
  4. private String password;
  5. private String phone;
  6. private String address;
  7. public Admin () {
  8. }
  9. public Admin (String username, String password, String phone, String address) {
  10. this.username = username;
  11. this.password = password;
  12. this.phone = phone;
  13. this.address = address;
  14. }
  15. public String getUsername () {
  16. return username;
  17. }
  18. public void setUsername (String username) {
  19. this.username = username;
  20. }
  21. public String getPassword () {
  22. return password;
  23. }
  24. public void setPassword (String password) {
  25. this.password = password;
  26. }
  27. public String getPhone () {
  28. return phone;
  29. }
  30. public void setPhone (String phone) {
  31. this.phone = phone;
  32. }
  33. public String getAddress () {
  34. return address;
  35. }
  36. public void setAddress (String address) {
  37. this.address = address;
  38. }
  39. @Override
  40. public String toString () {
  41. return "Admin{" +
  42. "username='" + username + '\'' +
  43. ", password='" + password + '\'' +
  44. ", phone='" + phone + '\'' +
  45. ", address='" + address + '\'' +
  46. '}';
  47. }
  48. }

7 AdminDao接口


  
  1. package com.cxyzxc.www.dao;
  2. import com.cxyzxc.www.entity.Admin;
  3. import java.util.List;
  4. public interface AdminDao {
  5. //查询用户(查询单个)
  6. Admin selectOneByUsernameAndPassword (String username, String password);
  7. //查询所有用户
  8. List<Admin> selectAll ();
  9. }

8 AdminDaoImpl实现类


  
  1. package com.cxyzxc.www.dao.impl;
  2. import com.cxyzxc.www.dao.AdminDao;
  3. import com.cxyzxc.www.entity.Admin;
  4. import com.cxyzxc.www.utils.DBUtils;
  5. import org.apache.commons.dbutils.QueryRunner;
  6. import org.apache.commons.dbutils.handlers.BeanHandler;
  7. import org.apache.commons.dbutils.handlers.BeanListHandler;
  8. import java.sql.SQLException;
  9. import java.util.List;
  10. public class AdminDaoImpl implements AdminDao {
  11. // 创建QueryRunner对象,并传递一个数据源对象
  12. private final QueryRunner QUERYRUNNER = new QueryRunner(DBUtils.getDataSource());
  13. @Override
  14. public Admin selectOneByUsernameAndPassword (String username, String password) {
  15. String sql = "SELECT * FROM `admin` WHERE `username` = ? AND `PASSWORD`=?;";
  16. Object[] args = {username, password};
  17. try {
  18. return QUERYRUNNER.query(sql, new BeanHandler<Admin>(Admin.class), args);
  19. } catch (SQLException e) {
  20. e.printStackTrace();
  21. }
  22. return null;
  23. }
  24. @Override
  25. public List<Admin> selectAll () {
  26. String sql = "SELECT * FROM `admin`;";
  27. try {
  28. return QUERYRUNNER.query(sql, new BeanListHandler<Admin>(Admin.class));
  29. } catch (SQLException e) {
  30. e.printStackTrace();
  31. }
  32. return null;
  33. }
  34. }

9 AdminService接口


  
  1. package com.cxyzxc.www.service;
  2. import com.cxyzxc.www.entity.Admin;
  3. import java.util.List;
  4. public interface AdminService {
  5. Admin login (String username, String password);
  6. List<Admin> selectAllAdmin ();
  7. }

10 AdminServiceImpl实现类


  
  1. package com.cxyzxc.www.service.impl;
  2. import com.cxyzxc.www.service.AdminService;
  3. import com.cxyzxc.www.dao.AdminDao;
  4. import com.cxyzxc.www.dao.impl.AdminDaoImpl;
  5. import com.cxyzxc.www.entity.Admin;
  6. import java.util.List;
  7. public class AdminServiceImpl implements AdminService {
  8. private final AdminDao ADMINDAO = new AdminDaoImpl();
  9. @Override
  10. public Admin login (String username, String password) {
  11. return ADMINDAO.selectOneByUsernameAndPassword(username, password);
  12. }
  13. @Override
  14. public List<Admin> selectAllAdmin () {
  15. return ADMINDAO.selectAll();
  16. }
  17. }

11 HTML页面

11.1 login.html页面


  
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>登录页面 </title>
  6. <link type="text/css" rel="stylesheet" href="css/login.css"/>
  7. </head>
  8. <body>
  9. <div>
  10. <form action="LoginServlet" method="post">
  11. <p>
  12. 账号: <input type="text" name="username"/>
  13. </p>
  14. <p>
  15. 密码: <input type="password" name="password"/>
  16. </p>
  17. <p>
  18. <input type="submit" value="登录"/>
  19. </p>
  20. </form>
  21. </div>
  22. </body>
  23. </html>

11.2 login.css


  
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. div {
  6. width: 400px;
  7. height: 100px;
  8. background-color: #ccc;
  9. margin: 30px auto;
  10. padding-top: 30px;
  11. text-align: center;
  12. }
  13. p {
  14. margin-top: 10px;
  15. }
  16. input {
  17. outline: none;
  18. }

11.3 table.css


  
  1. *{
  2. margin: 0;
  3. padding: 0;
  4. }
  5. table{
  6. margin: 20px auto;
  7. width: 500px;
  8. height: 100px;
  9. text-align: center;
  10. }

 

12 LoginServlet


  
  1. package com.cxyzxc.www.servlet;
  2. import com.cxyzxc.www.service.AdminService;
  3. import com.cxyzxc.www.service.impl.AdminServiceImpl;
  4. import com.cxyzxc.www.entity.Admin;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. @WebServlet("/LoginServlet")
  12. public class LoginServlet extends HttpServlet {
  13. private final AdminService ADMINSERVICE = new AdminServiceImpl();
  14. @Override
  15. protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws IOException {
  16. req.setCharacterEncoding( "UTF-8");
  17. //获取数据
  18. String username = req.getParameter( "username");
  19. String password = req.getParameter( "password");
  20. Admin admin = ADMINSERVICE.login(username, password);
  21. resp.setContentType( "text/html;charset=UTF-8");
  22. PrintWriter printWriter = resp.getWriter();
  23. if (admin != null) {
  24. printWriter.println( "<html>");
  25. printWriter.println( "<head>");
  26. printWriter.println( "<title>登录成功</title>");
  27. printWriter.println( "</head>");
  28. printWriter.println( "<body>");
  29. printWriter.println( "<h2>登录成功</h2>");
  30. printWriter.println( "</body>");
  31. printWriter.println( "</html>");
  32. } else {
  33. printWriter.println( "<html>");
  34. printWriter.println( "<head>");
  35. printWriter.println( "<title>登录失败</title>");
  36. printWriter.println( "</head>");
  37. printWriter.println( "<body>");
  38. printWriter.println( "<h2>账号或密码错误,无法登录</h2>");
  39. printWriter.println( "</body>");
  40. printWriter.println( "</html>");
  41. }
  42. }
  43. @Override
  44. protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws IOException {
  45. doGet(req, resp);
  46. }
  47. }

13 ShowAllAdminServlet


  
  1. package com.cxyzxc.www.servlet;
  2. import com.cxyzxc.www.service.AdminService;
  3. import com.cxyzxc.www.service.impl.AdminServiceImpl;
  4. import com.cxyzxc.www.entity.Admin;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import java.util.List;
  12. @WebServlet("/ShowAllAdminServlet")
  13. public class ShowAllAdminServlet extends HttpServlet {
  14. private final AdminService ADMINSERVICE = new AdminServiceImpl();
  15. @Override
  16. protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws IOException {
  17. req.setCharacterEncoding( "UTF-8");
  18. resp.setContentType( "text/html;charset=UTF-8");
  19. PrintWriter printWriter = resp.getWriter();
  20. List<Admin> adminList = ADMINSERVICE.selectAllAdmin();
  21. if (adminList.size() != 0) {
  22. printWriter.println( "<html>");
  23. printWriter.println( "<head>");
  24. printWriter.println( "<title>所有admin</title>");
  25. printWriter.println( "<link type=\"text/css\" rel=\"stylesheet\" href=\"css/table.css\" />");
  26. printWriter.println( "</head>");
  27. printWriter.println( "<table border='1px' cellspacing='0'>");
  28. printWriter.println( "<tr>");
  29. printWriter.println( "<th>账号</th>");
  30. printWriter.println( "<th>密码</th>");
  31. printWriter.println( "<th>手机号码</th>");
  32. printWriter.println( "<th>住址</th>");
  33. printWriter.println( "<th>操作</th>");
  34. printWriter.println( "</tr>");
  35. for (Admin admin : adminList) {
  36. printWriter.println( "<tr>");
  37. printWriter.println( "<td>" + admin.getUsername() + "</td>");
  38. printWriter.println( "<td>" + admin.getPassword() + "</td>");
  39. printWriter.println( "<td>" + admin.getPhone() + "</td>");
  40. printWriter.println( "<td>" + admin.getAddress() + "</td>");
  41. printWriter.println( "<td><a href=\"#\">修改</a> <a href=\"#\">删除</a></td>");
  42. printWriter.println( "</tr>");
  43. }
  44. printWriter.println( "</table>");
  45. printWriter.println( "</html>");
  46. } else {
  47. printWriter.println( "<html>");
  48. printWriter.println( "<head>");
  49. printWriter.println( "<title>所有admin</title>");
  50. printWriter.println( "</head>");
  51. printWriter.println( "<body>");
  52. printWriter.println( "<h2>当前没有用户</h2>");
  53. printWriter.println( "</body>");
  54. printWriter.println( "</html>");
  55. }
  56. }
  57. @Override
  58. protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws IOException {
  59. doGet(req, resp);
  60. }
  61. }

好了,这些就是单独查和全部查的全部方法,感谢大家支持

今天的分享就到此结束了

创作不易点赞评论互关三连

 

 


转载:https://blog.csdn.net/swy2560666141/article/details/128465946
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场