JSP向数据库插入数据(简单注册功能)

这篇博客为大家讲述,如何从jsp页面中注册一个账号,并插入到数据库。

首先在school数据库下建立user表,字段如下:

在这里插入图片描述

然后,创建web项目,搭建环境如下:

在这里插入图片描述

然后就开始我们的代码了。

1.首先把BaseDao.java粘贴到com.tao.dao包下,lib下粘贴mysql-connector-java-5.1.19.jar;在entity包下创建User类
package com.tao.entity;

public class User {
	private Integer id;
	private String name;
	private String pass;
	private Integer age;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
}

注意User下的对象要与数据库字段相匹配

2.创建UserDao类,继承BaseDao类,实现插入的方法,博主附带写了一个查询方法。
package com.tao.dao;

import com.tao.entity.User;

public class UserDao extends BaseDao{
	public User dologin(String name,String pass){ 
		User u=null;
		try {
			super.connect();
			String sql="select * from user where name=? and pass=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, name);
			pstmt.setString(2,pass);
			rs=pstmt.executeQuery();
			while(rs.next()){
				u=new User();
				u.setId(rs.getInt(1));
				u.setName(rs.getString(2));
				u.setPass(rs.getString(3));
				u.setAge(rs.getInt(4));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			super.closeAll();
		}
		return u;
	}
	public int insert(User u){
		int row=0;
		try {
			super.connect();
			String sql="insert into user(name,pass,age) values(?,?,?)";
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, u.getName());
			pstmt.setString(2,u.getPass());
			pstmt.setInt(3, u.getAge());
			row=pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			super.closeAll();
		}
		return row;
	}
}

3.创建注册页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="doregister.jsp" method="post" >

		用户名:<input name="username" >
		密码:<input name="password">
		年龄:<input name="age">
		<hr>
		<input type="submit" value="提交" >
		<input type="reset" >

</form>
</body>
</html>

4.对注册的数据做分析

<%@page import="com.tao.dao.UserDao"%>
<%@page import="com.tao.entity.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%   //获取姓名密码和年龄
	String name=request.getParameter("username");
 	String pass=request.getParameter("password");
 	int age=Integer.parseInt(request.getParameter("age"));
 	//创建User对象,赋值
 	User u=new User();
 	u.setName(name);
 	u.setPass(pass);
 	u.setAge(age);
 	UserDao udao=new UserDao();
 	//调用udao的方法实现插入功能
 	int x=udao.insert(u);
 	if(x>=1){ 
 	//转发,如果>1,跳转到success2.jsp页面
 		request.getRequestDispatcher("/success2.jsp").forward(request, response);
 	}
%>
</body>
</html>

5.注册成功页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
注册成功
</body>
</html>

大家有什么不懂得可以问博主哟

猜你喜欢

转载自blog.csdn.net/qq_42429369/article/details/84449200