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">
<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>
3.实体类
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;
}
}
2.操作数据库
package com.offcn.dao;
import com.offcn.bean.Student;
public interface StudentDao {
public int insertStudent(Student stu);
}
package com.offcn.dao.impl;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
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;
}
}
4.程序入口
package com.offcn.servlet;
import java.io.IOException;
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 {
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){
System.out.println("success");
}else{
System.out.println("fail");
}
}
}