在Jsp页面把注册信息添加到数据库使用到了Servlet类的doPost方法,下面这个小Demo将演示最简单的数据添加到数据库。ps:这里用到的数据库是SQLServer

1.首先创建数据库和数据表

--创建数据库
create database reg
go

--引用数据库reg
use reg
go

--创建数据表
create table regInfo(
	userName varchar(20) not null,
	userPwd varchar(20) not null,
	userAge int not null,
	userSex varchar(2) not null,
	userAddr varchar(50) not null
)
go

2.然后我们需要在Jsp文件中创建出注册表单

类似这样的表单:

在这里得注意表单标签form里面的action元素和method元素,action里面的链接必须是跳转到Servlet的,method里面的是post

3.创建链接数据库工具类

package cc.ryanc.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
	//静态语句块加载驱动
	static{
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 数据库连接的方法
	 * @return
	 */
	public Connection getConn(){
		Connection conn=null;
		try {
			//创建数据库连接对象
			conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;database=reg","sa","123456");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	/**
	 * 关闭连接的方法
	 * @param rs
	 * @param ptmt
	 * @param conn
	 */
	public void getOver(ResultSet rs, PreparedStatement ptmt, Connection conn){
		try {
			if(rs!=null){
				rs.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		try {
			if(ptmt!=null){
				ptmt.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		try {
			if(conn!=null){
				conn.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}

4.实体类的创建

package cc.ryanc.entity;
	
public class RegInfo {
	//封装属性
	private String userName;
	private String userPwd;
	private int userAge;
	private String userSex;
	private String userAddr;
	//get和set方法
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public int getUserAge() {
		return userAge;
	}
	public void setUserAge(int userAge) {
		this.userAge = userAge;
	}
	public String getUserSex() {
		return userSex;
	}
	public void setUserSex(String userSex) {
		this.userSex = userSex;
	}
	public String getUserAddr() {
		return userAddr;
	}
	public void setUserAddr(String userAddr) {
		this.userAddr = userAddr;
	}
	/**
	 * 无参数的构造方法
	 */
	public RegInfo() {
		super();
		// TODO Auto-generated constructor stub
	}
	/**
	 * 有参数的构造方法
	 * @param userName
	 * @param userPwd
	 * @param userAge
	 * @param userSex
	 * @param userAddr
	 */
	public RegInfo(String userName, String userPwd, int userAge,
			String userSex, String userAddr) {
		super();
		this.userName = userName;
		this.userPwd = userPwd;
		this.userAge = userAge;
		this.userSex = userSex;
		this.userAddr = userAddr;
	}
}

5.访问数据层

	package cc.ryanc.dao;
	
	import java.sql.Connection;
	import java.sql.PreparedStatement;
	import java.sql.ResultSet;
	import java.sql.SQLException;
	
	import cc.ryanc.entity.RegInfo;
	import cc.ryanc.util.DBUtil;
	
	public class RegInfoDao {
		//定义常用的接口
		Connection conn = null;
		PreparedStatement ptmt=null;
		//创建DButil对象
		DBUtil db = new DBUtil();
		/**
		 * 添加数据的方法
		 * @return
		 */
		public boolean insert(RegInfo ri){
			boolean flag=false;
			//调用方法连接数据库
			conn = db.getConn();
			//创建sql语句
			try {
				ptmt=conn.prepareStatement("insert into regInfo values(?,?,?,?,?)");
				ptmt.setString(1,ri.getUserName());
				ptmt.setString(2,ri.getUserPwd());
				ptmt.setInt(3,ri.getUserAge());
				ptmt.setString(4,ri.getUserSex());
				ptmt.setString(5,ri.getUserAddr());
				int row=ptmt.executeUpdate();
				if(row>0){
					flag=true;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally{
				db.getOver(null, ptmt, conn);
			}
			return flag;
		}
	}

6.servlet类的创建

package cc.ryanc.servlet;
	
import java.io.IOException;
import java.io.PrintWriter;
	
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
	
import cc.ryanc.dao.RegInfoDao;
import cc.ryanc.entity.RegInfo;
	
public class GetServlet extends HttpServlet{
	//创建Reginfo对象
	RegInfo ri = new RegInfo();
	//创建访问数据层对象
	RegInfoDao dao = new RegInfoDao();
	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	}
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//处理乱码
		req.setCharacterEncoding("utf-8");
		//获得数据并储存在实体类对象中
		ri.setUserName(req.getParameter("userName"));
		ri.setUserPwd(req.getParameter("userPwd"));
		ri.setUserAge(Integer.parseInt(req.getParameter("userAge")));
		ri.setUserSex(req.getParameter("userSex"));
		ri.setUserAddr(req.getParameter("userAddr"));
		//调用dao类添加数据 返回布尔值
		if(dao.insert(ri)){
			System.out.println("添加成功!");
		}else{
			System.out.println("添加失败");
		}
	}
}

Finally.测试

控制台显示添加成功

查询数据库是否添加成功

2017-03-02 16:49:51 星期四 @RYAN0UP