JavaWeb-连接数据库实现登录功能,并显示商品列表

一、MySQL:

users表:

JavaWeb-连接数据库实现登录功能,并显示商品列表_第1张图片

goods表:

JavaWeb-连接数据库实现登录功能,并显示商品列表_第2张图片

二、jsp

jsp前端登录显示代码:

<%--
  Created by IntelliJ IDEA.
  User: xbyan
  Date: 2023/2/14
  Time: 16:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Ybb778








效果图:

如果数据库有该用户,跳转到商品页面,jsp代码:

<%@ page import="java.util.List" %>
<%@ page import="com.bing.bean.Goods" %><%--
  Created by IntelliJ IDEA.
  User: xbyan
  Date: 2023/2/16
  Time: 12:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    welcome



欢迎你

<% HttpSession session1 = request.getSession(); List goodsList = (List) session1.getAttribute("goodList"); for (Goods goods : goodsList) { %> <% } %>
商品编号 商品名称 商品价格 商品库存
<%=goods.getGid()%> <%=goods.getGname()%> <%=goods.getGprice()%> <%=goods.getGinventory()%>

效果图:

JavaWeb-连接数据库实现登录功能,并显示商品列表_第3张图片

三、java代码:

连接数据库:

package com.bing.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBCUtil {
    private static String driver = "com.mysql.cj.jdbc.Driver";// 驱动包
    private static String url = "jdbc:mysql://localhost:3306/jwTest?useSSL=false&serverTimezone=UTC";// 数据库地址
    private static String username = "root";// 数据库账号
    private static String password = "root";// 数据库密码
    private static Connection con = null;

    public static Connection getCon() {
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    public static void close(ResultSet rs, PreparedStatement ps, Connection con) {
        try {
            if (rs != null) {
                rs.close();
                System.out.println("ResultSet已释放...");
            }
            if (ps != null) {
                ps.close();
                System.out.println("PreparedStatement已释放...");
            }
            if (con != null) {
                con.close();
                System.out.println("Connection已释放...");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void close(PreparedStatement ps, Connection con) {
        try {
            if (ps != null) {
                ps.close();
                System.out.println("PreparedStatement已释放...");
            }
            if (con != null) {
                con.close();
                System.out.println("Connection已释放...");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

登录代码:

package com.bing.servlet;

import com.bing.dao.EnrollDao;
import com.bing.dao.impl.UserDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/welcome")
public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        /*
         * 处理请求源发送过来的数据
         * */
        req.setCharacterEncoding("UTF-8");// 将编码改为UTF-8
        String user = req.getParameter("userName");// user接收上个页面的userName值
        String pwd = req.getParameter("pwd");// pwd接收上个页面的pwd值
        // 响应的编码
        resp.setCharacterEncoding("UTF-8");
        // 文本格式
        resp.setContentType("text/html;charset=UTF-8");
        UserDaoImpl userDao = new UserDaoImpl();

        if (userDao.userExists(user) && userDao.login(user, pwd) == null) {
            resp.sendRedirect("forget.jsp");// 用户存在 密码不对 进入更改密码、忘记密码界面
        } else if (userDao.login(user, pwd) != null) {
            req.getRequestDispatcher("selectAllGoods").forward(req, resp);
            // resp.sendRedirect("welcome.jsp");// 账号、密码正确  进入欢迎界面
        } else if (!userDao.userExists(user)) {
            resp.sendRedirect("enroll.jsp");// 用户不存在 注册用户
        } else {
            resp.sendRedirect("cao.jsp");// 特殊情况
        }
    }

}

查询商品表代码:

package com.bing.servlet;

import com.bing.bean.Goods;
import com.bing.dao.impl.GoodsDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectAllGoods")
public class SelectAllGoods extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        GoodsDaoImpl goodsDao = new GoodsDaoImpl();
        List goodsList = goodsDao.selectGoods();
        System.out.println(goodsList);
        HttpSession session = req.getSession();
        session.setAttribute("goodList",goodsList);
        resp.sendRedirect("welcome.jsp");
    }
}

这样我们就完成了

你可能感兴趣的:(tomcat,java,servlet,intellij-idea)