【java】兴唐第二十九节课作业

12 篇文章 0 订阅
12 篇文章 0 订阅

将用户在网页填写的信息输入数据库

数据库:

create table user_infer(
	id int(2) not null auto_increment primary key,
	user_name varchar(12), 
	password varchar(64) not null,
	real_name varchar(8) not null,
	age int(3) 
);

JAAVEE

stuList

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import = "java.sql.*" %>

<!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>学生信息列表</title>
</head>
<body>
<%
	Class.forName("com.mysql.jdbc.Driver");
	String url = "jdbc:mysql://localhost:3306/system";
	Connection conn = DriverManager.getConnection(url,"root","root");
	Statement stat = null;
	ResultSet rs = null;
	try{
		stat = conn.createStatement();
		rs = stat.executeQuery("select * from user_infer");	
%>

	<div align = "center">
		<h3>学生信息列表</h3>
		<hr />
		<table border = "1px" width = "70%" bordercolor = "red" cellSpacing = "0px" >
			<tr>
				<th>学生编号</th><th>学生姓名</th><th>真实姓名</th><th>年龄</th>
			</tr>
			<%
			while(rs.next()){
			%>
			
			
		
			<tr>
				<td><%out.print(rs.getInt(1)); %>  </td>
				<td><%out.print(rs.getString(2)); %>  </td>
				<td><%out.print(rs.getString(4)); %>  </td>
				<td><%out.print(rs.getInt(5)); %>  </td>
			</tr>
			
		<% 
			}
			}catch(Exception e){
		
	}finally{
		if(rs != null){
			rs.close();
		}
		if(stat != null){
			stat.close();
		}
		if(conn != null){
			conn.close();
		}
	}
	%>
		</table>
	</div>
</body>
</html>

AddStudent

<%@ 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>学生信息添加</title>
</head>
<body>
	<div align = "center">
	<h3>添加学生信息</h3>
	<hr>
	<form action = "doAddStu.jsp" method = "post">
	编号:<input type = "text" name = "id"><br/>
	网名:<input type = "text" name = "name"><br/>
	密码: <input type = "password" name = "password"><br/>
	真实姓名:<input type = "text" name = "real_name"><br>
	年龄:<input type = "text" name = "age"><br/>
	<input type = "submit" value = "提交">
	<input type = "reset" value = "重置">
	</form>
	<div>
</body>
</html>

DoAddStudent

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import = "java.sql.*"	
    %>
<!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>
<%
	request.setCharacterEncoding("utf-8");
	String id = request.getParameter("id");
	String name = request.getParameter("name");
	String password = request.getParameter("password");
	String real_name = request.getParameter("real_name");
	String age = request.getParameter("age");
	
	String sql = "insert into user_infer values (?,?,?,?,?)";
	Class.forName("com.mysql.jdbc.Driver");
	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/system","root","root");
	PreparedStatement ps = null;
	try{
		ps = conn.prepareStatement(sql);
		ps.setInt(1,Integer.parseInt(id));
		ps.setString(2,name);
		ps.setString(3,password);
		ps.setString(4,real_name);
		ps.setInt(5,Integer.parseInt(age));
		if(ps.executeUpdate() > 0){
			response.sendRedirect("stuList.jsp");
		}else{
			out.println("数据保存失败!!!");
		}
	}catch(SQLException e){
		e.printStackTrace();
	}finally{
		if(ps != null){
			ps.close();
		}
		
		if(conn != null){
			conn.close();
		}
		
	}
%>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值