将用户在网页填写的信息输入数据库
数据库:
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>