教师管理系统
增加和查询界面:
<%@page import="java.text.SimpleDateFormat"%> <%@page import="java.sql.*"%> <%@ 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>Insert title here</title> </head> <body> <table border="1" cellpadding="0" cellspacing="0" width="700" align="center" > <tr height="20"><td colspan="7" rowspan="2" width="700"><center><h2>教师信息表</h2></center></td> </tr> <tr height="20"></tr> <tr height="20" align="center"> <td width="100">TNO</td> <td width="100">TNAME</td> <td width="100">TSEX</td> <td width="100">TBIRTHDAY</td> <td width="100">PROF</td> <td width="100">DEPART</td> <td width="100">操作</td> </tr> </table> <% try { //连接数据库 Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","test","test"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from teacher"); if(rs!=null) { while(rs.next()) { SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); %> <table border="1" cellpadding="0" cellspacing="0" width="700" align="center" > <tr height="30" align="center"> <td width="100"><%=rs.getString("tno") %></td> <td width="100"><%=rs.getString("tname") %></td> <td width="100"><%=rs.getString("tsex") %></td> <td width="100"><%=sdf.format(rs.getDate("tbirthday"))%></td> <td width="100"><%=rs.getString("prof") %></td> <td width="100"><%=rs.getString("depart") %></td> <td width="100"><a href='UpdateT.jsp?tno=<%=rs.getString("tno") %>'>修改</a> <a href='DeleteTeacher?tno=<%=rs.getString("tno") %>'>删除</a></td> </tr> </table> <% } } con.close(); st.close(); rs.close(); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } %> <br><b>添加教师信息</b><br> <form action="SaveTeacher" method="post"> <table cellpadding="0" cellspacing="0" > <tr> <td>编号:</td> <td><input type="text" name="tno"></td> </tr> <tr> <td>姓名:</td> <td><input type="text" name="tname"></td> </tr> <tr> <td>性别:</td> <td><input type="text" name="tsex"></td> </tr> <tr> <td>生日:</td> <td><input type="text" name="tbirthday"></td> </tr> <tr> <td>职称:</td> <td><input type="text" name="prof"></td> </tr> <tr> <td>系别:</td> <td><input type="text" name="depart"></td> </tr> <tr> <td><input type="submit" value="提交"></td> </tr> </table> </form> </body> </html>
删除功能,servlet
package com.hanqi.web; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class DeleteTeacher */ public class DeleteTeacher extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DeleteTeacher() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); String tno=request.getParameter("tno"); if(tno!=null&&tno.trim().length()!=0) { try{ //处理,即保存 //连接数据库 //1.加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //2.得到连接 Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","test","test"); //插入数据 PreparedStatement ps=con.prepareStatement( "delete teacher where "+"tno=?"); ps.setString(1, tno); ps.executeUpdate(); ps.close(); con.close(); //跳转页面 response.getWriter().write("删除成功!"); response.setHeader("refresh", "3;teacher.jsp"); }catch (Exception e) { e.printStackTrace(); response.getWriter().write("删除失败!"); response.setHeader("refresh", "3;teacher.jsp"); } } else { response.getWriter().write("请正确提交数据!"); response.setHeader("refresh", "3;teacher.jsp"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
更新:
<%@page import="java.text.*"%> <%@page import="java.sql.*"%> <%@ 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>Insert title here</title> </head> <body> <% //接收主键,从数据库中读取最新数据 String tno=request.getParameter("tno"); String tname=""; String tsex=""; String tbirthday=""; String prof=""; String depart=""; if(tno!=null&&tno.trim().length()>0) { //读取最新数据 try{ //处理,即保存 //连接数据库 //1.加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //2.得到连接 Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","test","test"); PreparedStatement ps=con.prepareStatement("select * from teacher where tno = ?"); ps.setString(1, tno); ResultSet rs=ps.executeQuery(); if(rs!=null&&rs.next()) { tname=rs.getString("tname"); tsex=rs.getString("tsex"); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); tbirthday=sdf.format(rs.getDate("tbirthday")); prof=rs.getString("prof"); depart=rs.getString("depart"); }else { out.print("未查询到数据"); } rs.close(); ps.close(); con.close(); }catch (Exception e) { e.printStackTrace(); response.getWriter().write("保存失败!"); response.setHeader("refresh", "3;teacher.jsp"); } }else { out.print("请正确访问"); } %> <br><b>修改教师信息</b><br> <form action="SaveTeacher" method="post"> <table cellpadding="0" cellspacing="0" > <tr> <td>编号:</td> <td><input type="text" name="tno" value="<%=tno %>" readonly="readonly"></td> </tr> <tr> <td>姓名:</td> <td><input type="text" name="tname" value="<%=tname %>"></td> </tr> <tr> <td>性别:</td> <td><input type="text" name="tsex" value="<%=tsex %>"></td> </tr> <tr> <td>生日:</td> <td><input type="text" name="tbirthday" value="<%=tbirthday %>"></td> </tr> <tr> <td>职称:</td> <td><input type="text" name="prof" value="<%=prof %>"></td> </tr> <tr> <td>系别:</td> <td><input type="text" name="depart" value="<%=depart %>"></td> </tr> <tr> <td><input type="submit" value="提交"></td> <td><input type="hidden" name="isupdate" value="1"></td> </tr> </table> </form> </body> </html>
保存:
package com.hanqi.web; import java.io.IOException; import java.sql.*; import java.text.SimpleDateFormat; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class SaveTeacher */ public class SaveTeacher extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SaveTeacher() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); String tno=request.getParameter("tno"); String tname=request.getParameter("tname"); String tsex=request.getParameter("tsex"); String tbirthday=request.getParameter("tbirthday"); String prof=request.getParameter("prof"); String depart=request.getParameter("depart"); String isupdate=request.getParameter("isupdate"); if(tno!=null&&tno.trim().length()!=0) { try { //连接数据库 Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","test","test"); PreparedStatement ps=con.prepareStatement( "insert into teacher (tname,tsex,tbirthday,prof,depart,tno)"+" values (?,?,?,?,?,?)"); if(isupdate!=null&&isupdate.equals("1")) { ps=con.prepareStatement( "update teacher set tname=?,tsex=?,tbirthday=?,prof=?,depart=? where "+"tno=?"); } SimpleDateFormat adf=new SimpleDateFormat("yyyy-MM-dd"); java.util.Date bir =adf.parse(tbirthday); //使用long型的时间构造sql.Date Date sqlBir=new Date(bir.getTime()); ps.setString(1, tname); ps.setString(2, tsex); ps.setDate(3 ,sqlBir); ps.setString(4, prof); ps.setString(5, depart); ps.setString(6, tno); ps.executeUpdate(); ps.close(); con.close(); response.getWriter().write("保存成功!"); response.setHeader("refresh", "3;teacher.jsp"); } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); response.getWriter().write("保存失败!"); response.setHeader("refresh", "3;teacher.jsp"); } }else { response.getWriter().write("请正确提交数据!"); response.setHeader("refresh", "3;teacher.jsp"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
运行结果: