JavaWeb servlet + jdbc完成数据库用户添加功能

该文章展示了一个JavaWeb应用中使用JdbcConnectionUtils进行数据库连接的示例,以及RegistServlet如何处理GET请求来获取用户注册信息并保存至数据库。注册页面通过HTML表单提交数据,包括用户名、密码、性别和爱好,Servlet接收到请求后将数据插入数据库。
摘要由CSDN通过智能技术生成

数据库工具类JdbcConnectionUtils

public class JdbcConnectionUtils {
	String encoding = "";
	String names = "";
	String url = "jdbc:mysql://localhost:3306/test2?characterEncoding=UTF-8&&useUnicode=true";
	String user = "root";
	String password = "lizhi123";
	Connection conn= null;
	
	public Connection getConnection() {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn = DriverManager.getConnection(url,user,password);
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return conn;
	}
	public void closeAll(Connection conn,PreparedStatement pst) {
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}		
		if(pst != null) {
			try {
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

RegistServlet

@WebServlet(name="RegistServlet",urlPatterns = {"/register"})
public class RegistServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 接收注册页面的值
		String name = req.getParameter("username");
		String password = req.getParameter("password");
		String sex = req.getParameter("sex");
		String hiddenbutton = req.getParameter("button");
		if(hiddenbutton.equals("add")) {
			System.out.println("add");
		}else if(hiddenbutton.equals("up")) {
			System.out.println("up");
		}
		
		//获取多个爱好
		String[] hobbits = req.getParameterValues("hobbit");
		String love = "";
		for(String hobbit : hobbits) {
			System.out.println("爱好有:"+hobbit);
			love+=hobbit;
		}
		
		//获取页面多个请求参数的值
		Enumeration<String> enumerations = req.getParameterNames();
		while (enumerations.hasMoreElements()) {
			System.out.println(enumerations.nextElement());	
		}
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter writer = resp.getWriter();
//		writer.print(name);
		
		//保存到数据库
		JdbcConnectionUtils jdbc = new JdbcConnectionUtils();
		Connection conn = jdbc.getConnection();
		System.out.println(conn);
		PreparedStatement pst = null;
		//sql语句
		String sql = "insert into stu(username,password,sex,hobbit)values(?,?,?,?)";
		//pst
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			pst.setString(2, password);
			pst.setString(3, sex);
			pst.setString(4, love);
			
			int rows = pst.executeUpdate();
			if(rows>0) {
				writer.print("添加成功");
			}else {
				writer.print("添加失败");
			}
			
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		jdbc.closeAll(conn, pst);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//处理post方式提交乱码数据
		req.setCharacterEncoding("UTF-8");
		doGet(req, resp);
	}
	

}

注册页面register.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>成都文理注册页面</title>
</head>
<body>
	<center>
		<form action="register" method="get">
			<div style="background-color: gray; width: 60%; height: 400px">
				<p>
					用户名:<input type="text" name="username" value="" />
				</p>
				<p>
					密码:<input type="password" name="password" value="" />
				</p>
				<p>
					唱歌:<input type="checkbox" name="hobbit" value="唱歌" /> 
					跳舞:<input type="checkbox" name="hobbit" value="跳舞" />
				</p>

				<p>
					男:<input type="radio" name="sex" value="男" />
					女:<input type="radio" name="sex" value="女" />
				</p>
				<p>
					<input type="hidden" name="button" value="add">
				</p>
				<p>
					<input type="submit" value="注册">
				</p>
			</div>
		</form>
	</center>
</body>
</html>

 数据库字段

成功视频

20230309_155423

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值