1.完成JDBC的登录操作;2.完成JDBC注册用户的操作;

一、完成JDBC的登录操作

登录 注册共用实体类:(User类、Goods类)

User类:

package com.nai.servlet;

public class User {
    private Integer uid;
    private String username;
    private String password;
    private String phone;
    private String address;

    public User(Integer uid, String username, String password, String phone, String address) {
        this.uid = uid;
        this.username = username;
        this.password = password;
        this.phone = phone;
        this.address = address;
    }

    public User() {

    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                '}';
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    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;
    }
}

 Goods类:

package com.nai.servlet;

public class Goods {
    private Integer gid;
    private String gname;

    private Double price;
    private String mark;



    public Goods(Integer gid, String gname, Double price, String mark) {
        this.gid = gid;
        this.gname = gname;
        this.price = price;
        this.mark = mark;
    }

    public Integer getGid() {
        return gid;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "gid=" + gid +
                ", gname='" + gname + '\'' +
                ", price='" + price + '\'' +
                ", mark='" + mark + '\'' +
                '}';
    }

    public void setGid(Integer gid) {
        this.gid = gid;
    }

    public String getGname() {
        return gname;
    }

    public void setGname(String gname) {
        this.gname = gname;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getMark() {
        return mark;
    }

    public void setMark(String mark) {
        this.mark = mark;
    }
}

第一步:

先创建一个数据库。

 第二步:

创建一个Login.jsp(登录)、goods.jsp(商品)

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2023/2/14
  Time: 11:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<form action="Login" method="post">
    账号:<input type="text" name="username" value=""><br>
    密码:<input type="password" name="password" value=""><br>
    <input type="submit" value="登录">
</form>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2023/2/16
  Time: 11:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>登陆成功</title>
</head>
<body>
<h1>登录成功</h1>
<h2>欢迎来到javaweb项目</h2>
<table>
  <thead>
  <tr>
    <th>编号</th>
    <th>名称</th>
    <th>价格</th>
    <th>说明</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td>泡面</td>
    <td>4.5</td>
    <td>够香够辣就是这个味!</td>
  </tr>
  <tr>
    <td>火腿</td>
    <td>8.5</td>
    <td>肉质细腻Q弹!</td>
  </tr><tr>
    <td>雪碧</td>
    <td>3.5</td>
    <td>清爽冰凉随心爽!</td>
  </tr>
  </tbody>

</table>

</body>
</html>

第三步:

创建一个Login类

package com.nai.servlet;

import com.nai.servlet.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class Login extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Login-get...");
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charest=UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Connection connection=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        User aa=null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/myweb?useSSL=false&serverTimezone=UTC","root","root");
            String sql="select * from t_user where username=? and password=?";
            pstm= connection.prepareStatement(sql);
            pstm.setObject(1,username);
            pstm.setObject(2,password);
            rs = pstm.executeQuery();
            if (rs.next()){
                aa=new User();
                aa.setUid(rs.getInt("uid"));
                aa.setUsername(rs.getString("username"));
                aa.setPassword(rs.getString("password"));
                aa.setPhone(rs.getString("phone"));
                aa.setAddress(rs.getString("address"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (rs!=null){
                    rs.close();
                }
                if (pstm!=null){
                    pstm.close();
                }
                if (connection!=null){
                    connection.close();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }

        if (aa != null){
            resp.sendRedirect("goods.jsp");
        } else {
            resp.sendRedirect("user.jsp");
        }

    }
}


二.完成JDBC注册用户的操作 

1.创建zhuce.jsp(注册)、index.jsp (首页)

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2023/2/14
  Time: 11:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册</title>
</head>
<body>
<form action="zhuce" method="post">
    账号:<input type="text" name="username" value=""><br>
    密码:<input type="password" name="password" value=""><br>
    电话:<input type="text" name="phone"><br>
    地址:<input type="text" name="address">
    <input type="submit" value="注册">
</form>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2023/2/14
  Time: 10:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>欢迎访问web网页</title>
</head>
<body>
<h>欢迎访问web网页</h>
<a href="login.jsp">去登录</a><br>
<a href="zhuce.jsp">去注册</a><br>
</body>
</html>

 2.创建一个web.xml硬设

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--    配置servlet类;-->
    <servlet>
        <!--        起别名-->
        <servlet-name>login</servlet-name>
        <!--        servlet类所在位置-->
        <servlet-class>com.nai.servlet.Login</servlet-class>
    </servlet>
    <!--    servlet类的映射-->
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/Login</url-pattern>
    </servlet-mapping>
    <!--    配置servlet类;-->
    <servlet>
        <!--        起别名-->
        <servlet-name>zhuce</servlet-name>
        <!--        servlet类所在位置-->
        <servlet-class>com.nai.servlet.ZhuCe</servlet-class>
    </servlet>
    <!--    servlet类的映射-->
    <servlet-mapping>
        <servlet-name>zhuce</servlet-name>
        <url-pattern>/zhuce</url-pattern>

    </servlet-mapping>

</web-app>

3.创建一个Zhuce类

package com.nai.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class ZhuCe extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Login-get...");
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charest=UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");

        try {
            Boolean zhucetest = zhucetest(username, password, phone, address);
            if (zhucetest) {
                resp.getWriter().write("<h1>注册成功</h1>");
            } else {
                resp.getWriter().write("<h1>注册失败</h1>");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private Boolean zhucetest(String username, String password, String phone, String address) throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myweb?useSSL=false&serverTimezone=UTC", "root", "root");
        String sql = "insert into t_user(username,password,phone,address) values(?,?,?,?)";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setObject(1, username);
        pstm.setObject(2, password);
        pstm.setObject(3, phone);
        pstm.setObject(4, address);
        int i = pstm.executeUpdate();
        if (i > 0) {
            if (pstm != null) {
                pstm.close();
            }
            if (connection != null) {
                connection.close();
            }
            return true;
        }else {
            return false;
        }
    }

}

 效果图:

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值