《注册V1.1》——————加入了查询数据库中的学生表,解决了中文乱码问题,以及一个Servlet解决多个请求的问题
程序员文章站
2024-03-20 13:51:10
...
1.准备工作
在数据库中创建数据库和注册表,导入jar包,c3p0的配置文件,以及c3p0和日期转换工具类,登录的里面都已经上传过c3p0的配置文件,以及c3p0和日期转换工具类的代码。
2.注册页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="StudentServlet" method="post">
<input type="hidden" name="fun" value="add" />
<table width="70%" align="center">
<tr>
<td>name</td>
<td>
<input type="text" name="name"/>
</td>
</tr>
<tr>
<td>age</td>
<td>
<input type="text" name="age"/>
</td>
</tr>
<tr>
<td>birthday</td>
<td>
<input type="text" name="birthday"/>
</td>
</tr>
<tr>
<td colspan=2>
<input type="submit" value="register"/>
</td>
</tr>
</table>
</form>
</body>
</html>
注册结果跳转的页面
2.1成功
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>注册成功</h1>
</body>
</html>
2.2失败
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>注册成功</h1>
</body>
</html>
3.查询页面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="StudentServlet?fun=findall">查询全部学生</a>
</body>
</html>
查询结果展示
package com.offcn.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.offcn.bean.Student;
public class ViewStudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Student> list = (List<Student>)request.getAttribute("studentList");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<table align='center' width='70%' cellspacing='0' border='1px'>");
out.print("<tr><th>ID</th><th>NAME</th><th>AGE</th><th>BIRTHDAY</th></tr>");
for(Student stu : list){
out.print("<tr><td>"+stu.getId()+"</td><td>"+stu.getName()+"</td><td>"+stu.getAge()+"</td><td>"+stu.getBirthday()+"</td></tr>");
}
out.print("</table>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
4.实体类
package com.offcn.bean;
import java.util.Date;
public class Student {
private int id;
private String name;
private int age;
private Date birthday;
public Student(){}
public Student(int id, String name, int age, Date birthday) {
super();
this.id = id;
this.name = name;
this.age = age;
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
5.操作数据库
package com.offcn.dao;
import java.util.List;
import com.offcn.bean.Student;
public interface StudentDao {
public int insertStudent(Student stu);
public List<Student> findAllStudent();
}
package com.offcn.dao.impl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.offcn.bean.Student;
import com.offcn.dao.StudentDao;
import com.offcn.utils.C3P0Utils;
public class StudentDaoImpl implements StudentDao {
public int insertStudent(Student stu) {
int result = 0;
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
String sql = "insert into Student values(null,?,?,?) ";
try {
result = qr.update(sql,new Object[]{stu.getName(),stu.getAge(),stu.getBirthday()});
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public List<Student> findAllStudent() {
List<Student> list = new ArrayList<>();
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
String sql = "select * from student";
try {
list = qr.query(sql, new BeanListHandler<>(Student.class));
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
}
6、程序入口
package com.offcn.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.offcn.bean.Student;
import com.offcn.dao.StudentDao;
import com.offcn.dao.impl.StudentDaoImpl;
import com.offcn.utils.DateUtil;
public class StudentServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String fun = request.getParameter("fun");
if ("add".equals(fun)) {
add(request, response);
}else if("findall".equals(fun)){
findall(request,response);
}
}
protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String age = request.getParameter("age");
String birthday = request.getParameter("birthday");
Student stu = new Student();
stu.setName(name);
stu.setAge(Integer.parseInt(age));
stu.setBirthday(DateUtil.stringToDate(birthday));
StudentDao dao = new StudentDaoImpl();
int result = dao.insertStudent(stu);
if (result > 0) {
request.getRequestDispatcher("success.html").forward(request, response);
} else {
//该需求重定向较好,因为转发地址栏不变,没刷新一次就存入一条数据到数据库
response.sendRedirect("fail.html");
}
}
protected void findall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentDao dao = new StudentDaoImpl();
List<Student> list = dao.findAllStudent();
request.setAttribute("studentList", list);
request.getRequestDispatcher("ViewStudentServlet").forward(request, response);
}
}
上一篇: web 登录
下一篇: DS顺序表的概念及动态顺序表接口的实现