考试一注册

staffDao

package zjf.makeupexam.jsp.dao;

import zjf.makeupexam.jsp.pojo.staff;

import java.sql.*;

public class staffDao {
    private Connection connection = null;
    private PreparedStatement preparedStatement = null;

    public int insertStaff(staff staff){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root", "root");
            preparedStatement=connection.prepareStatement("insert into staff values(null ,?,?,?,?,?)");
            preparedStatement.setString(1,staff.getSname());
            preparedStatement.setString(2,staff.getSage());
            preparedStatement.setString(3,staff.getSspecialty());
            preparedStatement.setString(4,staff.getSeducation());
            preparedStatement.setString(5,staff.getSgschool());
            int i = preparedStatement.executeUpdate();
            return i;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            return 0;
        }
        finally {
            try {
                connection.close();
                preparedStatement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}

Staff

package zjf.makeupexam.jsp.pojo;

public class staff {
    private int sid;
    private String sname;
    private String sage;
    private String sspecialty;
    private String seducation;
    private String sgschool;

    public staff() {
    }

    public staff(int sid, String sname, String sage, String sspecialty, String seducation, String sgschool) {
        this.sid = sid;
        this.sname = sname;
        this.sage = sage;
        this.sspecialty = sspecialty;
        this.seducation = seducation;
        this.sgschool = sgschool;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSage() {
        return sage;
    }

    public void setSage(String sage) {
        this.sage = sage;
    }

    public String getSspecialty() {
        return sspecialty;
    }

    public void setSspecialty(String sspecialty) {
        this.sspecialty = sspecialty;
    }

    public String getSeducation() {
        return seducation;
    }

    public void setSeducation(String seducation) {
        this.seducation = seducation;
    }

    public String getSgschool() {
        return sgschool;
    }

    public void setSgschool(String sgschool) {
        this.sgschool = sgschool;
    }

    @Override
    public String toString() {
        return "Staff{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", sage='" + sage + '\'' +
                ", sspecialty='" + sspecialty + '\'' +
                ", seducation='" + seducation + '\'' +
                ", sgschool='" + sgschool + '\'' +
                '}';
    }
}

staffServiceImpl

package zjf.makeupexam.jsp.service.impl;

import zjf.makeupexam.jsp.dao.staffDao;
import zjf.makeupexam.jsp.pojo.staff;
import zjf.makeupexam.jsp.service.staffService;

public class staffServiceImpl implements staffService {
    staffDao staffDao=new staffDao();
    @Override
    public boolean logon(staff staff) {
        if(staff!=null){
            return staffDao.insertStaff(staff)>0;
        }
        return false;
    }
}

staffService

package zjf.makeupexam.jsp.service;

import zjf.makeupexam.jsp.pojo.staff;

public interface staffService {
    boolean logon(staff staff);
}

logonServlet

package zjf.makeupexam.jsp.servlet;

import zjf.makeupexam.jsp.pojo.staff;
import zjf.makeupexam.jsp.service.impl.staffServiceImpl;
import zjf.makeupexam.jsp.service.staffService;

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("/login")
public class logonServlet extends HttpServlet {
    staffService staffService=new staffServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String sname = req.getParameter("sname");
        String sage = req.getParameter("sage");
        String sspecialty = req.getParameter("sspecialty");
        String seducation = req.getParameter("seducation");
        String sgschool = req.getParameter("sgschool");

        staff staff = new staff(0, sname, sage, sspecialty, seducation, sgschool);
        if (staffService.logon(staff)) {
            req.getRequestDispatcher("success.jsp").forward(req,resp);
        }
        else req.getRequestDispatcher("logon.jsp").forward(req,resp);
    }
}

Test

package zjf.makeupexam.jsp;

import org.junit.Test;
import zjf.makeupexam.jsp.pojo.staff;
import zjf.makeupexam.jsp.service.impl.staffServiceImpl;
import zjf.makeupexam.jsp.service.staffService;

public class test {
    @Test
    public void service_test(){
        staffService service=new staffServiceImpl();

        staff staff = new staff(0, "123", "123", "123", "123", "123");

        System.out.println(service.logon(staff));
    }
}

Web

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>logon</servlet-name>
    <servlet-class>zjf.makeupexam.jsp.servlet.logonServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>logon</servlet-name>
    <url-pattern>/logon</url-pattern>
  </servlet-mapping>

</web-app>

Index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<a href="logon.jsp">注册职工</a>
</body>
</html>

Logon

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="login">
    姓名:<input type="text" name="sname"><br>
    年龄:<input type="text" name="sage"><br>
    特长:<input type="text" name="sspecialty"><br>
    学历:<input type="text" name="seducation"><br>
    学校:<input type="text" name="sgschool"><br>
    <input type="submit" value="注册">
</form>
</body>
</html>

Success

成功

Pom

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
  </dependency>

  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
  </dependency>

  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
  </dependency>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
  </dependency>
</dependencies>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值