通过前面的练习发现自己对于增删改查的知识仍然不够熟练,今天进行一个基于课程管理的增删改查功能的实现。
Dbutil:
package com.zh.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.sql.SQLException; public class DButil { public static Connection getConnection(){ Connection conn = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url="jdbc:mysql://localhost:3306/teacher_class?serverTimezone=UTC"; conn=DriverManager.getConnection(url, "root", "hao19990507."); }catch (Exception e){ e.printStackTrace(); } return conn; } public static void close(Statement sta,Connection con){ if (sta != null) { try { sta.close(); } catch (SQLException e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close(ResultSet rs,Statement sta,Connection con){ if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (sta != null) { try { sta.close(); } catch (SQLException e) { e.printStackTrace(); } } if (con != null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
bean:
package com.zh.entity; public class bean { private String place; private String teacher_name; private String class_name; public String get_place() { return place; } public void set_place(String place) { this.place=place; } public String get_teacher_name() { return teacher_name; } public void set_teacher_name(String teacher_name) { this.teacher_name=teacher_name; } public String get_class_name() { return class_name; } public void set_class_name(String class_name) { this.class_name=class_name; } public bean() {}; public bean(String place,String teacher_name,String class_name) { this.place=place; this.teacher_name=teacher_name; this.class_name=class_name; } }
data_dao:
package com.zh.dao; import java.util.*; import java.sql.*; import com.zh.entity.*; import com.zh.util.*; import java.sql.*; public class data_dao { /*增加 * */ public void add(bean Bean) { String sql="insert into class_message values('"+Bean.get_place()+"','"+Bean.get_teacher_name()+"','"+Bean.get_class_name()+"')"; Connection conn = DButil.getConnection(); Statement state = null; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } } /*删除 * */ public void deleteByteacher_name(String teacher_name) { boolean f=false; String sql="delete from class_message where teacher_name='"+teacher_name+"'"; Connection conn=DButil.getConnection(); Statement state = null; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } } /* 修改 * */ public void updata(bean b) { String sql="updata class_message set place='"+b.get_place()+"', teacher_name='"+b.get_teacher_name()+"',class_name='"+b.get_class_name()+"' where teacher_name='"+b.get_teacher_name()+"'"; Connection conn=DButil.getConnection(); Statement state = null; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } } /** * 验证课程名称是否唯一 */ public List<bean> isOnly_class_name(String class_name) { List list=new ArrayList<bean>(); String sql="select name from class_message where class_name='"+class_name+"'"; Connection conn=DButil.getConnection(); Statement state=null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while(rs.next()) { String class_name1=rs.getString("class_name"); String teacher_name=rs.getString("teacher_name"); String place=rs.getString("place"); bean b=new bean(place,teacher_name,class_name1); list.add(b); } } catch (SQLException e) { e.printStackTrace(); } return list; } /** * 通过teacher_name获得一个bean对象 */ public bean findByteacher_name(String teacher_name) { Connection conn=null; Statement state=null; try { conn=DButil.getConnection(); String sql="select*from class_message where teacher_name='"+teacher_name+"'"; state=conn.createStatement(); ResultSet rs=state.executeQuery(sql); bean u=new bean();; while(rs.next()) { String t_name=rs.getString("teacher_name"); String c_name=rs.getString("class_name"); String place=rs.getString("place"); u.set_class_name(c_name); u.set_place(place); u.set_teacher_name(t_name); return u; } }catch (Exception e) { e.printStackTrace(); } return null; } /* * 显示全部 */ public List<bean> findAll(){ Connection conn=DButil.getConnection(); List<bean> list=new ArrayList<bean>(); bean b=null; try { Statement sta=conn.createStatement(); String sql="select * from class_message"; ResultSet rs=sta.executeQuery(sql); while(rs.next()) { b=new bean(); String t_name,c_name,place; t_name=rs.getString("teacher_name"); c_name=rs.getString("c_name"); place=rs.getString("place"); b.set_class_name(c_name); b.set_place(place); b.set_teacher_name(t_name); list.add(b); } }catch (Exception e) { e.printStackTrace(); } for(int i=0;i<list.size();i++) { bean br=(bean)list.get(i); System.out.println(br.get_teacher_name()); } return list; } }
Userservice:
package com.zh.service; import java.util.*; import com.zh.dao.*; import com.zh.entity.*; public class Userservice { public boolean insertUser(String place,String teacher_name,String class_name) { data_dao dao=new data_dao(); bean b=new bean(); b.set_class_name(class_name); b.set_place(place); b.set_teacher_name(teacher_name); try { dao.add(b); return true; }catch (Exception e) { e.printStackTrace(); return false; } } public boolean deleteUser(String teacher_name) { data_dao dao=new data_dao(); bean b=dao.findByteacher_name(teacher_name); if(b!=null) { dao.deleteByteacher_name(teacher_name); return true; }else return false; } public boolean updataByteacher_name(String place,String class_name,String teacher_name) { data_dao dao=new data_dao(); bean b=new bean(place,class_name,teacher_name); try { dao.updata(b); return true; }catch (Exception e) { e.printStackTrace(); return false; } } public boolean isOnly_classname(String class_name) { data_dao dao=new data_dao(); try { List list=dao.isOnly_class_name(class_name); if(list.size()>1) return true; else return false; }catch (Exception e) { e.printStackTrace(); return false; } } public List<bean> findAll_user(){ data_dao dao=new data_dao(); List list=dao.findAll(); return list; } }
DeleteServlet:
package com.zh.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zh.service.*; @WebServlet("/DeleteServlet") public class DeleteServlet extends HttpServlet { protected void service(HttpServletRequest arg0,HttpServletResponse arg1) throws ServletException ,IOException{ arg1.setContentType("text/html"); arg0.setCharacterEncoding("GBK"); arg1.setCharacterEncoding("GBK"); String name=arg0.getParameter("teacher_name"); Userservice service =new Userservice(); service.deleteUser(name); try { arg0.getRequestDispatcher("/query").forward(arg0, arg1); }catch (Exception e) { e.printStackTrace(); } } }
InsertServlet:
package com.zh.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zh.service.*; @WebServlet("/InsertServlet") public class InsertServlet extends HttpServlet { protected void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); req.setCharacterEncoding("GBK"); res.setCharacterEncoding("GBK"); String place=req.getParameter("place"); String teacher_name=req.getParameter("teacher_name"); String class_name=req.getParameter("class_name"); PrintWriter pw=res.getWriter(); Userservice service=new Userservice(); try { service.insertUser(place, teacher_name, class_name); pw.println("新增信息成功!"); req.getRequestDispatcher("/query").forward(req, res);; }catch (Exception e) { e.printStackTrace(); req.getRequestDispatcher("/insert.html").forward(req, res); } } }
QueryServlet:
package com.zh.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; import com.zh.entity.*; import com.zh.service.*; import java.util.*; @WebServlet("/QueryServlet") public class QueryServlet extends HttpServlet { protected void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); req.setCharacterEncoding("GBK"); res.setCharacterEncoding("GBK"); PrintWriter pw=res.getWriter(); Userservice service=new Userservice(); List list=service.findAll_user(); pw.println("<html>"); pw.println("<head>"); pw.println("</head>"); pw.println("<body>"); pw.println("<table border='1' bordercolor='red' align='center'"); pw.println("<tr>"); pw.println("<td>"); pw.println("地点"); pw.println("</td>"); pw.println("<td>"); pw.println("教师名"); pw.println("</td>"); pw.println("<td>"); pw.println("课程名"); pw.println("</td>"); pw.println("</tr>"); for(int i=0;i<list.size();i++) { bean b=(bean) list.get(i); pw.println("<tr>"); pw.println("<td>"); pw.println(b.get_place()); pw.println("</td>"); pw.println("<td>"); pw.println(b.get_teacher_name()); pw.println("</td>"); pw.println("<td>"); pw.println(b.get_class_name()); pw.println("/<td>"); pw.println("<a href='/Web_operation1/delete?teacher_name="+b.get_teacher_name()+"'> 删 除 </a>||<a href='/Web_operation1/updata.html'>修改信息</a>" ); pw.println("</td>"); pw.println("</tr>"); } pw.println("</table>"); pw.println("</body>"); pw.println("</html>"); pw.flush(); pw.close(); } }
UpdateServlet:
package com.zh.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.PrintWriter; import com.zh.service.*; @WebServlet("/UpdataServlet") public class UpdateServlet extends HttpServlet { protected void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); req.setCharacterEncoding("GBK"); res.setCharacterEncoding("GBK"); PrintWriter pw=res.getWriter(); Userservice service=new Userservice(); String place=req.getParameter("place"); String teacher_name=req.getParameter("teacher_name"); String class_name=req.getParameter("class_name"); try { if (service.updataByteacher_name(place, class_name, teacher_name)) { res.sendRedirect("/Web_operation1/query"); } }catch (Exception e) { e.printStackTrace(); } } }
insert.html:
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta charset="UTF-8"> <title>Insert </title> </head> <body> <center> <form action="/Web_operation1/insert"medthod="post"> <table border ="1" bordercolor="red"align="center"> <tr> <td> 上课地点: </td> <td> <input type="text" name="place"/> </td> </tr> <tr> <td> 任课教师: </td> <td> <input type="text" name="teacher_name"/> </td> </tr> <tr> <td> 课程名称: </td> <td> <input type="text" name="class_name"/>; </td> </tr> </table> </form> </center> </body> </html>
update.html:
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta charset="UTF-8"> <title>update</title> </head> <body> <center> <form action="/Web_operation1/update" method="post"> <table border="1"bordercolor="red" align="center"> <tr> <td> 上课地点:</td> <td> <input type="text" name="place"> </td> </tr> <tr> <td> 任课教师:</td> <td> <input type="text" name="teacher_name"> </td> </tr> <tr> <td> 课程名称:</td> <td> <input type="text"name="class_name"> </td> </tr> <tr> <td colspan="2"align="center"> <input type="button" value="确认"> </td> </tr> </table> </form> </body> </html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <display-name>Web_operation1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>query</servlet-name> <servlet-class>com.zh.servlet.QueryServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>query</servlet-name> <url-pattern>/query</url-pattern> </servlet-mapping> <servlet> <servlet-name>delete</servlet-name> <servlet-class>com.zh.servlet.DeleteServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>delete</servlet-name> <url-pattern>/delete</url-pattern> </servlet-mapping> <servlet> <servlet-name>update</servlet-name> <servlet-class>com.zh.servlet.UpdateServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>update</servlet-name> <url-pattern>/update</url-pattern> </servlet-mapping> <servlet> <servlet-name>insert</servlet-name> <servlet-class>com.zh.servlet.InsertServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>insert</servlet-name> <url-pattern>/insert</url-pattern> </servlet-mapping> </web-app>