Java web

上次我们已经搞完了jsp的操作。现在该是后台的配置了。

在dao包里面进行数据链接:DBConn.java

1/** 2 *  3 */ 4/** 5 * @author Administrator 6 * 7 */ 8package dao; 9 10import java.sql.*; 11public class DBConn { 12 /** 13  * 链接数据库 14  * @return 15  */ 16     public static Connection getConnection(){ 17      Connection conn=null; 18      try { 19     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 20     conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBaseName=EstateDB","sa","123456"); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } 24  return conn; 25     } 26}

BuildingDao.java

1package dao; 2 3import java.sql.Connection; 4import java.sql.PreparedStatement; 5import java.sql.ResultSet; 6import java.sql.SQLException; 7import java.sql.Statement; 8import java.util.ArrayList; 9import java.util.List; 10 11import entity.Building; 12 13 14 15 16public class BuildingDao { 17  /** 18   * 操作数据库命令链接 19   * 数据访问类 20   */ 21 private Connection conn; 22 private Statement state; 23 private ResultSet rs; 24 private PreparedStatement pre; 25 /** 26  * 查询全部 27  * @return 28  * @throws SQLException 29  */ 30 public List<Building> fill() throws SQLException { 31 List<Building> list = new ArrayList<Building>(); 32 String sql = "select * from T_building"; 33 conn = DBConn.getConnection(); 34 state = conn.createStatement(); 35 rs = state.executeQuery(sql); 36 Building p = null; 37 while (rs.next()) { 38= new Building(); 39 p.setId(rs.getString("Id")); 40 p.setCompany(rs.getString("Company")); 41 p.setPhone(rs.getString("Phone")); 42 p.setDescription(rs.getString("Description")); 43 p.setStatus(rs.getString("Status")); 44 list.add(p); 45 } 46 rs.close(); 47 state.close(); 48 conn.close(); 49 return list; 50 } 51 /** 52  * 根据Id查询 53  * @param Id 54  * @return 55  * @throws SQLException 56  */ 57public Building fill(String Id) throws SQLException{ 58 59 conn = DBConn.getConnection(); 60 String sql="select * from T_building where Id=?"; 61 pre = conn.prepareStatement(sql); 62 pre.setString(1, Id); 63 rs=pre.executeQuery(); 64 Building p = null; 65 if(rs.next()){ 66= new Building(); 67 p.setId(rs.getString("Id")); 68 p.setCompany(rs.getString("Company")); 69 p.setPhone(rs.getString("Phone")); 70 p.setDescription(rs.getString("Description")); 71 p.setStatus(rs.getString("Status")); 72 } 73 rs.close(); 74 pre.close(); 75 conn.close(); 76 return p; 77 } 78/** 79 * 添加 80 * @param building 81 * @return 82 * @throws SQLException 83 */ 84 public int add(Building building) throws SQLException { 85 86 String sql = "insert  T_building values ('" + building.getId() + "','" 87 + building.getCompany() + "','" + building.getPhone() + "','" 88 + building.getDescription() + "','" + building.getStatus() 89 + "')"; 90 System.out.println(sql); 91 conn = DBConn.getConnection(); 92 state = conn.createStatement(); 93 int result = state.executeUpdate(sql); 94 state.close(); 95 conn.close(); 96 return result; 97 98 } 99 /** 100  * 修改 101  * @param building 102  * @return 103  * @throws SQLException 104  */ 105 public int  update(Building  building) throws SQLException { 106 String sql="UPDATE T_building SET Company=?,Phone =?,"+"Description=?, Status=? WHERE Id=?"; 107 conn=DBConn.getConnection(); 108 pre = conn.prepareStatement(sql); 109 pre.setString(1,  building.getCompany()); 110 pre.setString(2,  building.getPhone()); 111 pre.setString(3,  building.getDescription()); 112 pre.setString(4,  building.getStatus()); 113 pre.setString(5,  building.getId()); 114 int count=pre.executeUpdate(); 115 pre.close(); 116 conn.close(); 117 return count; 118 // TODO Auto-generated method stub 119 120 } 121/** 122 * 根据ID删除一项 123 * @param Id 124 * @throws SQLException 125 */ 126 public void  delete(String Id) throws SQLException { 127 String sql="delete from  T_building where  Id=?"; 128 conn=DBConn.getConnection(); 129 pre = conn.prepareStatement(sql); 130 pre.setString(1,Id); 131 pre.executeUpdate(); 132 pre.close(); 133 conn.close(); 134 // TODO Auto-generated method stub 135 } 136 /** 137  * 多项选择Id删除 138  * @param Id 139  * @throws SQLException 140  */ 141 public void  delete(String[] Id) throws SQLException { 142 conn = DBConn.getConnection(); 143 String ids="'"+Id[0]+"'"; 144 for(int i=1;i<Id.length;i++) { 145 ids=ids+",'"+Id[i]+"'"; 146 } 147 String sql="delete from  T_building where  Id  in ("+ids+")"; 148 pre = conn.prepareStatement(sql); 149 pre.executeUpdate(); 150 pre.close(); 151 conn.close(); 152 // TODO Auto-generated method stub 153 } 154}

对啦,忘记创建实体类了。在entity包里面建实体类

Building.java

1/** 2 *  3 */ 4/** 5 * @author Administrator 6 * 7 */ 8package entity; 9public class Building { 10 /** 11  * 实体类 12  * 定义get ,set 属性 13  */ 14    private String Id; 15    private String Company; 16    private String Phone; 17    private String Description; 18    private String Status; 19 public String getId() { 20 return Id; 21 } 22 public void setId(String id) { 23 Id = id; 24 } 25 public String getCompany() { 26 return Company; 27 } 28 public void setCompany(String company) { 29 Company = company; 30 } 31 public String getPhone() { 32 return Phone; 33 } 34 public void setPhone(String phone) { 35 Phone = phone; 36 } 37 public String getDescription() { 38 return Description; 39 } 40 public void setDescription(String description) { 41 Description = description; 42 } 43 public String getStatus() { 44 return Status; 45 } 46 public void setStatus(String status) { 47 Status = status; 48 } 49 50     51}

service服务

BuildingService.java

1/** 2 *  3 */ 4/** 5 * @author Administrator 6 * 7 */ 8package service; 9 10import java.sql.SQLException; 11import java.util.List; 12 13import dao.BuildingDao; 14import entity.Building; 15 16 17public class BuildingService{ 18 /** 19  * 添加 20  * @param building 21  * @return 22  * @throws SQLException 23  */ 24 public int add(Building building) throws SQLException { 25 BuildingDao dao=new BuildingDao(); 26 return dao.add(building); 27 } 28 /** 29  * 查询 30  * @return 31  * @throws SQLException 32  */ 33 public  List<Building>  fill() throws SQLException{ 34 BuildingDao dao=new BuildingDao(); 35 return dao.fill(); 36 } 37 public  Building  fill(String  Id) throws SQLException{ 38 BuildingDao dao=new BuildingDao(); 39 return dao.fill(Id); 40 } 41 /** 42  * 修改 43  * @param building 44  * @return 45  * @throws SQLException 46  */ 47 public int update(Building building) throws SQLException{ 48 BuildingDao dao=new BuildingDao(); 49 return dao.update(building); 50 } 51 /** 52  *  删除 53  * @param Id 54  * @throws SQLException 55  */ 56 public void delete(String Id) throws SQLException{ 57 BuildingDao dao=new BuildingDao(); 58  dao.delete(Id);; 59 } 60 61 public void delete(String[] Id) throws SQLException { 62 BuildingDao dao=new BuildingDao(); 63 dao.delete(Id); 64 } 65}

在action包里建servlet

BuildingServlet.java

1/** 2 *  3 */ 4/** 5 * @author Administrator 6 * 7 */ 8package action; 9 10import java.io.IOException; 11import java.io.PrintWriter; 12import java.sql.SQLException; 13import java.util.List; 14import javax.servlet.ServletException; 15import javax.servlet.http.HttpServletRequest; 16import javax.servlet.http.HttpServletResponse; 17import javax.swing.JApplet; 18import service.BuildingService; 19import entity.Building; 20 21 22public class BuildingServlet extends javax.servlet.http.HttpServlet implements 23 javax.servlet.Servlet { 24 25 static final long serialVersionUID = 1L; 26 27 public BuildingServlet() { 28 super(); 29 } 30 31 @Override 32 protected void doGet(HttpServletRequest request, 33 HttpServletResponse response) throws ServletException, IOException { 34 // TODO Auto-generated method stub 35 36 response.setCharacterEncoding("utf-8"); 37 try { 38 start(request, response); 39 } catch (Exception e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 44 } 45 46 @Override 47 protected void doPost(HttpServletRequest request, 48 HttpServletResponse response) throws ServletException, IOException { 49 // TODO Auto-generated method stub 50 request.setCharacterEncoding("utf-8"); 51 try { 52 start(request, response); 53 } catch (Exception e) { 54 // TODO Auto-generated catch block 55 e.printStackTrace(); 56 } 57 58 } 59 60 private void start(HttpServletRequest request, HttpServletResponse response) 61 throws Exception { 62 63 response.setCharacterEncoding("GBK"); 64 response.setContentType("text/html;charset=utf-8"); 65 BuildingService service = new BuildingService(); 66 String action = request.getParameter("action"); 67 String id = request.getParameter("id"); 68 /** 69  * 添加 70  */ 71 if (action.equals("add")) { 72 response.setContentType("text/html;charset=utf-8"); 73 String Id = request.getParameter("Id"); 74 String Company = request.getParameter("Company"); 75 String Phone = request.getParameter("Phone"); 76 String Description = request.getParameter("Description"); 77 String Status = request.getParameter("Status"); 78 Building b = new Building(); 79 b.setId(Id); 80 b.setCompany(Company); 81 b.setPhone(Phone); 82 b.setDescription(Description); 83 b.setStatus(Status); 84 BuildingService buildingService = new BuildingService(); 85 try { 86 buildingService.add(b); 87 PrintWriter out = response.getWriter(); 88 out.print("添加成功"); 89 } catch (SQLException e) { 90 PrintWriter out = response.getWriter(); 91 out.print("添加失败"); 92 e.printStackTrace(); 93 } 94 } 95 /** 96  * 查詢 97  */ 98 else if (action.equals("list")) { 99 try { 100 List<Building> buildingList = service.fill(); 101 request.setAttribute("buildingList", buildingList); 102 request.getRequestDispatcher("buildingList.jsp").forward( 103 request, response); 104 } catch (Exception e) { 105 // TODO Auto-generated catch block 106 e.printStackTrace(); 107 } 108 } else if (action.equals("list2")) { 109 String id1 = request.getParameter("id"); 110 try { 111 Building building = service.fill(id1); 112 request.setAttribute("building", building); 113 request.getRequestDispatcher("buildingList.jsp").forward( 114 request, response); 115 } catch (Exception e) { 116 e.printStackTrace(); 117 } 118 } 119 /** 120  * 修改 121  */ 122 else if (id != null&&action.equals("update")) { 123 try { 124 Building building = service.fill(id); 125 request.setAttribute("building", building); 126 request.getRequestDispatcher("buildingUpdate.jsp").forward( 127 request, response); 128 } catch (Exception e) { 129 e.printStackTrace(); 130 } 131 } else if(action.equals("update2")){ 132 String Id = request.getParameter("Id"); 133 String Company = request.getParameter("Company"); 134 String Phone = request.getParameter("Phone"); 135 String Description = request.getParameter("Description"); 136 String Status = request.getParameter("Status"); 137 Building b = new Building(); 138 b.setId(Id); 139 b.setCompany(Company); 140 b.setPhone(Phone); 141 b.setDescription(Description); 142 b.setStatus(Status); 143 BuildingService buildingService = new BuildingService(); 144 try { 145 buildingService.update(b); 146 PrintWriter out = response.getWriter(); 147 out.print("修改成功"); 148 149 } catch (Exception e) { 150 // TODO Auto-generated catch block 151 e.printStackTrace(); 152 } 153 } 154 /** 155  * 删除 156  */ 157  if(action.equals("delete")) { 158  try { 159 List<Building> buildingDelete = service.fill(); 160 request.setAttribute("buildingDelete", buildingDelete); 161 request.getRequestDispatcher("buildingDelete.jsp").forward( 162 request, response); 163 } catch (Exception e) { 164 // TODO Auto-generated catch block 165 e.printStackTrace(); 166 } 167   } 168   else if(action.equals("delete2")) { 169   String[] ids=request.getParameterValues("Id"); 170  // String id1=request.getParameter("id"); 171   try { 172 //service.delete(id1); 173 service.delete(ids); 174   response.sendRedirect("BuildingServlet?action=delete"); 175 } catch (Exception e) { 176 // TODO Auto-generated catch block 177 e.printStackTrace(); 178 } 179   } 180   else if(action.equals("delete3")) {    181   String id1=request.getParameter("id"); 182   try { 183 service.delete(id1); 184   response.sendRedirect("BuildingServlet?action=delete"); 185 } catch (Exception e) { 186 // TODO Auto-generated catch block 187 e.printStackTrace(); 188 } 189   } 190 191 } 192}
点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java加密解密_____MD5加密(用户名映射(用户名和密码)串)唯一性

package com.cnse.module.security.md5Security;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;/  @author Administrator md5加

SpringBoot报错:Invalid bound statement (not found)

错误原因:没有发现Mybatis配置文件的路径解决方法:1.检查Mapper包名与xml文件<mapper标签的namespace数据名称是否相同<mappernamespace"com.tuyrk._161_java_socket.project6.dao.FileMapper"</mapper2.Mapper中定义的方法

JSP+Servlet+JavaBean实现数据库的增删改查

基本思想:JSP文件显示页面,使用form或href超链接传值到Servlet中方法,在Servlet方法中调用Dao层的类对象,实现对数据库里的数据的增删改查,之后重新返回到JSP输出操作完的结果。共分为四个包和对应的JSP文件:1.DB包:建立连接数据库的方法,以及关闭操作数据库的方法。2.Servlet包:①接受来自JSP页面的参数,将这些

Java爬虫之JSoup使用教程

title:Java爬虫之JSoup使用教程date:201812248:00:000800update:201812248:00:000800author:mecover:https://imgblog.csdnimg.cn/20181224144920712(https://www.oschin

SpringBoot报错:Invalid bound statement (not found)

错误原因:没有发现Mybatis配置文件的路径解决方法:1.检查Mapper包名与xml文件<mapper标签的namespace数据名称是否相同<mappernamespace"com.tuyrk._161_java_socket.project6.dao.FileMapper"</mapper2.Mapper中定义的方法