五月十二日

web实验

<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%>
<%@ page errorPage="Error.jsp"%>
<html>
<head>
 <title>学生信息管理系统</title>
 <link rel="stylesheet" type="text/css" href="css/Style.css">
</head>
<body>
<h1 style="width: 100%; font-family: 微软雅黑; color:#fff;">学生信息管理系统</h1>
<a href="Add.jsp">添加学生信息</a>
<br />
<br />
<table style="width: 50%;">
 <tr>
   <th>学号</th>
   <th>姓名</th>
   <th>性别</th>
   <th>生日</th>
   <th>管理</th>
 </tr>
 <%
   try {
     Class.forName("com.mysql.jdbc.Driver");
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ceshi?useUnicode=true&characterEncoding=utf-8", "root", "NN06280055");
     //使用Statement对象
     Statement stmt = con.createStatement();
     ResultSet rs = stmt.executeQuery("select * from imformation");
     while (rs.next()) {
       int id = rs.getInt(1);
       out.println("<tr><td>" + rs.getString(1) +"</td><td>" + rs.getString(2) + "</td><td>" + rs.getString(3) + "</td><td>"
               + rs.getString(4) + "</td><td>"+ rs.getString(5) +"</td><td><a href='edit.jsp?id=" + id
               + "'>修改</a>&nbsp;<a href='del.jsp?id=" + id + "'>删除</a></td></tr>");
     }
     rs.close();
     stmt.close();
     con.close();
   } catch (Exception e) {
     out.println("Exception:" + e.getMessage());
   }
 %>

</table>
<br />
<hr />
<div style="text-align: center; width: 100%; font-size: 12px; color: #333;">
 &copy;版权所有:石家庄铁道大学信息科学与技术学院&nbsp;&nbsp;<a href="Lab03.png" target="_blank">网站地图</a>
</div>
</body>
</html>
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="Error.jsp"%>
<html>
<head>
 <title>添加学生信息</title>
 <link rel="stylesheet" type="text/css" href="css/Style.css">
 <script type="text/javascript">
   function check() {
     var id = document.getElementById("id");
     var name = document.getElementById("name");
     var sex = document.getElementById("sex");
     var birthday= document.getElementById("birthday");
     
     //非空
     if(id.value == '') {
       alert('学号为空');
       name.focus();
       return false;
     }
     if(name.value == '') {
       alert('姓名为空');
       teacher.focus();
       return false;
     }
     if(sex.value == '') {
       alert('性别为空');
       classroom.focus();
       return false;
     }
     if(birthday.value == '') {
       alert('生日为空');
       classroom.focus();
       return false;
     }
   }
 </script type="text/javascript">
</head>
<body>
<%
 Object message = request.getAttribute("message");
 if(message!=null && !"".equals(message)){

%>
<script type="text/javascript">
 alert("<%=request.getAttribute("message")%>");
</script>
<%} %>
<form action="addsave.jsp" method="post" onsubmit="return check()">
 <h2>添加学生信息</h2>
 <table style="width: 50%">
   <tr>
     <th width="30%">学号</th>
     <td width="70%"><input name="id" type="text"></td>
   </tr>
   <tr>
     <th>姓名</th>
     <td><input name="name" type="text"></td>
   </tr>
   <tr>
     <th>性别</th>
     <td><input name="sex" type="text"></td>
   </tr>
   <tr>
     <th>生日</th>
     <td><input name="birthday" type="text"></td>
   </tr>
   <tr>
     <td colspan="2"><input type="submit" name="submit" value="添加"> <input type="reset" value="重置"></td>
   </tr>
 </table>
</form>

</body>
</html>
View Code
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="Error.jsp"%>
<html>
<head>
 <title>添加学生信息</title>
 <link rel="stylesheet" type="text/css" href="css/Style.css">
</head>
<body>
<%
 request.setCharacterEncoding("utf-8");
 String id = request.getParameter("id");
 String name = request.getParameter("name");
 String sex = request.getParameter("sex");
 String birthday = request.getParameter("birthday");
 Class.forName("com.mysql.jdbc.Driver");
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ceshi?useUnicode=true&characterEncoding=utf-8", "root", "NN06280055");
 PreparedStatement stmt = con.prepareStatement("insert into imformation(id,name,sex,birthday) values(?, ?,?,?)");
 stmt.setString(1, id);
 stmt.setString(2, name);
 stmt.setString(3, sex);
 stmt.setString(4, birthday);
 int i = stmt.executeUpdate();


 if (i == 1) {
   out.println("<h2>添加成功!</h2><br/>");
   out.println("<a href='Index.jsp'>返回首页</a>");
 } else {
   out.println("<h2>添加失败!</h2><br/>");
   out.println("<a href='Add.jsp'>重新添加</a>");
 }
 stmt.close();
 con.close();

%>
</body>
</html>
View Code
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" pageEncoding="utf-8"%>
<html>
<head>
 <title>删除学生信息</title>
 <link rel="stylesheet" type="text/css" href="css/Style.css">
</head>
<body>
<%
 request.setCharacterEncoding("utf-8");
 Class.forName("com.mysql.jdbc.Driver");
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ceshi?useUnicode=true&characterEncoding=utf-8", "root", "NN06280055");
 Statement stmt=con.createStatement();
 String id=request.getParameter("id");
 int i=stmt.executeUpdate("delete from imformation where id="+id);
 if(i==1)
 {
   out.println("<h2>删除成功!</h2><br/>");
 }
 else
 {
   out.println("<h2>删除失败!</h2><br/>");
 }
 out.println("<a href='Index.jsp'>返回首页</a>");
 stmt.close();
 con.close();

%>
</body>
</html>
View Code
<%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="Error.jsp"%>
<html>
<head>
   <title>修改学生信息</title>
   <link rel="stylesheet" type="text/css" href="css/Style.css">
</head>
<body>
<%
   request.setCharacterEncoding("utf-8");
   String id = request.getParameter("id");
   Class.forName("com.mysql.jdbc.Driver");

   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8", "root", "NN06280055");

   PreparedStatement stmt = con.prepareStatement("select * from imformation where id=?");
   stmt.setString(1, id);
   ResultSet rs = stmt.executeQuery();

   rs.next();
%>
<form action="editsave.jsp" method="post">
   <h2>修改学生信息</h2>
   <table style="width:50%">
       <tr>
           <th width="30%">学号:</th>
           <td width="70%"><input name="id" type="text"
                                  value="<%=rs.getString(1)%>"></td>
       </tr>
       <tr>
           <th>姓名:</th>
           <td><input name="name" type="text"
                      value="<%=rs.getString(2)%>"></td>
       </tr>
       <tr>
           <th>性别:</th>
           <td><input name="sex" type="text"
                      value="<%=rs.getString(3)%>"></td>
       </tr>
       <tr>
           <th>生日:</th>
           <td><input name="birthday" type="text"
                      value="<%=rs.getString(4)%>"></td>
       </tr>
       <tr>
           <td colspan="2"><input type="hidden" name="id" value="<%=id%>">
               <input type="submit" value="修改"> <input type="reset"
                                                         value="重置"></td>
       </tr>
   </table>
</form>
<%
   rs.close();
   stmt.close();
   con.close();
%>
</body>
</html>
View Code
<%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="Error.jsp"%>
<html>
<head>
   <title>修改完成</title>
   <link rel="stylesheet" type="text/css" href="css/Style.css">
</head>
<body>
<%
   request.setCharacterEncoding("utf-8");
   String id = request.getParameter("id");
   String name = request.getParameter("name");
   String sex = request.getParameter("sex");
   String birthday = request.getParameter("birthday");
  
   Class.forName("com.mysql.jdbc.Driver");
   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ceshi?useUnicode=true&characterEncoding=utf-8", "root", "NN06280055");
   Statement stmt = con.createStatement();
   String sql = "update info set id='"+id+"',name='" + name  + "',sex='" + sex+ "',birthday='" + birthday+ "'where id=" + id;
   int i = stmt.executeUpdate(sql);
   if (i == 1) {
       out.println("<h2 >修改成功!</h2><br/>");
       out.println("<a href='Index.jsp'>返回首页</a>");
   } else {
       out.println("<h2>修改失败!</h2><br/>");
       out.println("<a href='Edit.jsp?id='" + id + ">重新添加</a>");
   }
   stmt.close();
   con.close();
%>
</body>
</html>
View Code
<%@ page language="java" isErrorPage="true" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <title>error page</title>
</head>
<body>
错误信息为:<br/>
<%=exception.getMessage()%><br>
<%=exception.toString()%>
</body>
</html>
View Code

 

posted @ 2023-05-12 22:05  布吉岛???  阅读(7)  评论(0编辑  收藏  举报