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>