《注册V1.0》

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

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值