1、学生登陆页面 login.jsp代码如下:
<html>
<head>
<title>登陆页面</title>
<link rel="stylesheet" href="css/bootstrap.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/bootstrap.bundle.js"></script>
<style>
.container{
width:30%;
height:20%;
margin-top:10%;
}
</style>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<p align="center">学生登录</p>
<form role="form" method="post" action="index.jsp">
<div class="form-group">
<label>用户名:</label><input class="form-control" name="username" type="text" />
</div>
<div class="form-group">
<label>密码:</label><input class="form-control" name="password" type="password" />
</div>
<button class="btn btn-default" type="submit">提交</button>
</form>
</div>
</div>
</div>
</body>
</html>
运行结果截图:
2、对学生信息进行增删改查
add.jsp代码如下(增):
<jsp:useBean id="user" class="com.media.bean.UserBean"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/yxl";
Connection connection = DriverManager.getConnection(url,"root","root");
connection.setAutoCommit(false);
String sql = "insert into user (username,password.gender.age) values (?,?,?,?)";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1,user.getUsername());
ps.setString(2,user.getPassword());
ps.setInt(3,user.getGender());
ps.setInt(4,user.getAge());
ps.setInt(5,0);
ps.addBatch();
ps.setString(1,user.getUsername());
ps.setString(2,user.getPassword());
ps.setInt(3,user.getGender());
ps.setInt(4,user.getAge());
ps.setInt(5,0);
ps.addBatch();
int[] count = {0};
try {
count=ps.executeBatch();
connection.commit();
}catch (SQLException e){
connection.rollback();
e.printStackTrace();
}
if(count[0] > 0){
out.print("添加成功");
}else{
out.print("添加失败");
}
response.setHeader("refresh","3;url=home.jsp");
%>
运行结果截图:
delete.jsp代码如下(删):
<html>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/yxl";
Connection connection= null;
connection = DriverManager.getConnection(url,"root","root");
String sql="delete from user where id=?";
PreparedStatement ps=connection.prepareStatement(sql);
int id=Integer.parseInt(request.getParameter("id"));
ps.setInt(1,id);
int count=ps.executeUpdate();
if(count>0){
out.print("删除成功");
}else{
out.print("删除失败");
}
%>
</html>
运行结果截图:
update.jsp代码如下(改):
<jsp:useBean id="user" class="com.media.bean.UserBean"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/yxl";
Connection connection= DriverManager.getConnection(url,"root","root");
String sql="update user set username=?,password=?,gender=?,age=? where id=? ";
PreparedStatement ps=connection.prepareStatement(sql);
String ids = (String) request.getParameter("id");
ps.setString(1,request.getParameter("username"));
ps.setString(2,request.getParameter("password"));
ps.setInt(3,Integer.parseInt(request.getParameter("gender")));
ps.setInt(4,Integer.parseInt(request.getParameter("age")));
ps.setInt(5,(Integer) request.getSession().getAttribute("id"));
int count=ps.executeUpdate();
if(count>0){
out.print("修改成功");
}else{
out.print("修改失败");
}
response.setHeader("refresh","3;url=home.jsp");
%>
运行结果截图:
edit.jsp代码如下(查):
<html>
<head>
<title>201819101023</title>
<link rel="stylesheet" href="css/bootstrap.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/bootstrap.bundle.js"></script>
<style>
.container{
width: 50%;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<form method="post" action="update.jsp">
<h3 style="margin-top:20px;" align="center">学生编辑</h3>
<%
String idStr = request.getParameter("id");
int id = Integer.parseInt(idStr);
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/yxl";
Connection connection = DriverManager.getConnection(url,"root","root");
String sql = "select * from user where id = ?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next()){
request.getSession().setAttribute("id",id);
out.print("<input type='hidden' name='id' values='"+id+"'>");
out.print("用户名:<input type='text' class='form-control' name='username' values="+rs.getString("username")+"><br>");
out.print("密码:<input type ='text' name='password' class=\"form-control\" value="+rs.getString("password")+"><br>");
out.print("性别:<input type ='text' name='gender' class=\"form-control\" value="+rs.getString("gender")+"><br>");
out.print("年龄:<input type ='text' name='age' class=\"form-control\" value="+rs.getString("age")+"><br>");
}
%>
<button type="submit" style="width: 100%" class="btn btn-info">提交</button>
</form>
</div>
</div>
</div>
</body>
</html>