码迷,mamicode.com
首页 > 其他好文 > 详细

11.10 日志

时间:2020-11-16 13:48:19      阅读:12      评论:0      收藏:0      [点我收藏+]

标签:row   std   attr   类型   doget   btn   编译   加载   pen   

package com.edu.ia;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
//添加数据
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.edu.ia.DBUutils;
@WebServlet("/addServlet")
public class addServlet extends HttpServlet{
 
 private static final long serialVersionUID = 1L;
 /**
  * 添加数据
  *
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String hukou = request.getParameter("hukou");
  String leixing = request.getParameter("leixing");
  String Area= request.getParameter("Area");
  String hzName = request.getParameter("hzName");
  String Id = request.getParameter("Id");
  String sex = request.getParameter("sex");
  String Nation = request.getParameter("Nation");
  String Edu = request.getParameter("Edu");
  if (hukou != null && leixing != null && Area != null&& hzName != null && Id != null && sex != null&& Nation != null && Edu != null) {
   Connection conn = DBUutils.open(); /// 获得数据库连接
   String sql = "insert into user(hukou,leixing,Area,hzName,Id,sex,Nation,Edu) values(?,?,?,?,?,?,?,?,?);";
   try {
    PreparedStatement prtmt = conn.prepareStatement(sql); // 预编译
    prtmt.setString(1, hukou); // 给第一个? 传值
    prtmt.setString(2, leixing); // 给第二个? 传值
    prtmt.setString(3, Area); // 给第一个? 传值
    prtmt.setString(4, hzName); // 给第二个? 传值
    prtmt.setString(5, Id); // 给第一个? 传值
    prtmt.setString(6, sex); // 给第二个? 传值
    prtmt.setString(7, Nation); // 给第一个? 传值
    prtmt.setString(8, Edu); // 给第二个? 传值
    int resInt = prtmt.executeUpdate(); // 如果添加成功返回影响数
    request.setAttribute("resInt", resInt);
    request.getRequestDispatcher("/findAllServlet").forward(request, response);
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } finally {
    DBUutils.close(); // 关闭数据库连接
   }
  }
 }
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  response.setContentType("text/html;charset=utf-8");
  doGet(request, response);
 }
}
 
 
package com.edu.ia;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class DBUutils {
 private static String driver;
 private static String url;
 private static String username;
 private static String password;
 /*private static String hukou;
 private static String leixing;
 private static String Area;
 private static String hzName;
 private static String Id;
 private static String sex;
 private static String Nation;
 private static String Edu;
*/
 static Connection conn = null;
 static {
  Properties properties = new Properties(); // 创建一个properties对象
  Reader inReader; // 一个reader属性;
  try {
   String path = Thread.currentThread().getContextClassLoader().getResource("jdbc.properties").getPath();
   inReader = new FileReader(path); // 用这个方法获取properties配置文件;
   properties.load(inReader); // 加载load内部的地址;
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  // 通过properties.getProperty("driver");方法获取值
  driver = properties.getProperty("driver");
  url = properties.getProperty("url");
  username = properties.getProperty("username");
  password = properties.getProperty("password");
  /*hukou = properties.getProperty("hukou");
  leixing = properties.getProperty("leixing");
  Area = properties.getProperty("Area");
  hzName = properties.getProperty("hzName");
  Id = properties.getProperty("Id");
  sex = properties.getProperty("sex");
  Nation = properties.getProperty("Nation");
  Edu = properties.getProperty("Edu");*/
 }
 /**
  * 打开数据库连接
  *
  * @return
  */
 public static Connection open() {
  try {
   Class.forName(driver);
   System.out.println("连接成功......");
   return DriverManager.getConnection(url, username, password);
  } catch (Exception e) {
   System.out.println("连接数据库失败....");
  }
  return null;
 }
 /**
  * 关闭数据库连接
  *
  * @return
  */
 public static Connection close() {
  if (conn != null) {
   try {
    conn.close();// 关闭数据库
    System.out.println("关闭...");
   } catch (Exception e) {
    e.printStackTrace();
    System.out.println("数据库关闭失败...");
   }
  }
  return null;
 }
}
 
 
 
package com.edu.ia;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
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.edu.ia.DBUutils;
/**
 * Servlet implementation class DelServlet
 */
@WebServlet("/DelServlet")
public class DelServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 /**
  * 删除数据
  *
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String id = request.getParameter("id"); // 传入id;
  if (id != null) {
   Connection conn = DBUutils.open();// 获得数据连接
   String sql = "delete from user where id = ?";
   try {
    PreparedStatement prtmt = conn.prepareStatement(sql); // 预编译
    prtmt.setString(1, id); // 给id赋值
    int resInt = prtmt.executeUpdate(); // 执行sql返回影响行数;
    request.setAttribute("resInt", resInt);
    request.getRequestDispatcher("/findAllServlet").forward(request, response);
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } finally {
    DBUutils.close(); // 关闭数据库连接
   }
  }
 }
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  response.setContentType("text/html;charset=utf-8");
  doGet(request, response);
 }
}
 
 
 
package com.edu.ia;
import java.io.IOException;
import java.sql.Connection;
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.sql.PreparedStatement;
import java.sql.SQLException;
import com.edu.ia.DBUutils;
/**
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/UpdateServlet")
public class UpdateSevrlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 /**
  * 更新数据
  *
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String id = request.getParameter("id");
  String hukou = request.getParameter("hukou");
  String leixing = request.getParameter("leixing");
  String Area = request.getParameter("Area");
  String hzName = request.getParameter("hzName");
  String Id = request.getParameter("Id");
  String sex = request.getParameter("sex");
  String Nation = request.getParameter("Nation");
  String Edu = request.getParameter("Edu");
  if (hukou != null && leixing != null || Area != null || hzName != null && Id != null || sex != null || Nation != null && Edu != null) {
   // 获得数据库连接
   Connection conn = DBUutils.open();
   String sql = "update user set hukou=?,leixing=?,Area=?,hzName=?,Id=?,sex=?,Nation=?,Edu=? where id=?"; // sql语句
   try {
    PreparedStatement prtmt = conn.prepareStatement(sql); // 预编译语句
    prtmt.setString(1, hukou); // 给第一个? 传值
    prtmt.setString(2, leixing); // 给第二个? 传值
    prtmt.setString(3, Area); // 给第3个? 传值
    prtmt.setString(4, hzName); // 给第4个? 传值
    prtmt.setString(5, Id); // 给第5个? 传值
    prtmt.setString(6, sex); // 给第6个? 传值
    prtmt.setString(7, Nation); // 给第7个? 传值
    prtmt.setString(8, Edu); // 给第8个? 传值
    prtmt.setString(9, id); // 给第9个? 添加数据
    int resInt = prtmt.executeUpdate(); // 执行
    request.setAttribute("resInt", resInt); // 返回影响的行数
    request.getRequestDispatcher("/findAllServlet").forward(request, response);
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } finally {
    DBUutils.close(); // 关闭数据库连接
   }
  }
 }
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  response.setContentType("text/html;charset=utf-8");
  doGet(request, response);
 }
}
 
 
 
 
package com.edu.ia;
public class User {
 private String hukou;
 private String leixing;
 private String Area;
 private String Id;
 private String hzName;
 private String sex;
 private String Nation;
 private String Edu;
 public String getHukou() {
  return hukou;
 }
 public void setHukou(String hukou) {
  this.hukou = hukou;
 }
 public String getLeixing() {
  return leixing;
 }
 public void setLeixing(String leixing) {
  this.leixing = leixing;
 }
 public String getArea() {
  return Area;
 }
 public void setArea(String area) {
  Area = area;
 }
 public String getId() {
  return Id;
 }
 public void setId(String id) {
  Id = id;
 }
 public String getHzName() {
  return hzName;
 }
 public void setHzName(String hzName) {
  this.hzName = hzName;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public String getNation() {
  return Nation;
 }
 public void setNation(String nation) {
  Nation = nation;
 }
 public String getEdu() {
  return Edu;
 }
 public void setEdu(String edu) {
  Edu = edu;
 }
}
 
 
 
 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
 String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="<%=path%>  /resource/bs/js/jquery-1.11.1.js"></script> <!-- 引入jquery -->
<link rel="stylesheet" href="<%=path%>/resource/bs/css/bootstrap.css"> <!-- 引入bootstrap.css -->
<script src="<%=path%>  /resource/bs/js/bootstrap.min.js"></script><!-- 引入bootstrap.min.js -->
<title>用户列表</title>
</head>
<body>
 <section class="container">
  <div class="row">
   <div class="col-md-3">
    <button class="btn btn-danger" data-toggle="modal"
     data-target="#addModal">新增</button>
   </div>
  </div>
<!-- <br>
1、请选择户口类型:
 <form method= "post" action = "">
 <input type = "radio" name = "hukou">家庭户
 <input type = "radio" name = "hukou">集体户
 </form>
<br>
2、请选择住房类型:
 <form method= "post" action = "">
 <input type = "radio" name = "leixing">家庭住宅
 <input type = "radio" name = "leixing">集体住所
 <input type = "radio" name = "leixing">工作地住所
 <input type = "radio" name = "leixing">其他住宅
 <input type = "radio" name = "leixing">无住宅
 </form>
<br>
3、请输入本户现住房面积:
 <input οnkeyup="value=value.replace(/[^\d]/g,‘‘) "
 onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))"
 ID="Text1" NAME="Text1">
<br>
4、请输入本户现住房间数:
 <input οnkeyup="value=value.replace(/[^\d]/g,‘‘) "
 onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))"
 ID="Text2" NAME="Text2">
<br>
5、
 <tr>
    <td>户主姓名:<input type="text" name="hzname"/>
    </td>
    </tr>
<br>
6、
 <tr>
    <td>身份证号(13位):<input type="text" name="teacher"/>
    </td>
    </tr>
<br>
7、请选择性别:
 <form method= "post" action = "">
 <input type = "radio" name = "sex">男
 <input type = "radio" name = "sex">女
 </form>
<br>
8、请输入身份证号:
 <input οnkeyup="value=value.replace(/[^\d]/g,‘‘) "
 onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))"
 ID="Text3" NAME="Text3">
<br>
9、请选择受教育程度:
 <form method= "post" action = "">
 <input type = "radio" name = "leixing">家庭住宅
 <input type = "radio" name = "leixing">集体住所
 <input type = "radio" name = "leixing">工作地住所
 <input type = "radio" name = "leixing">其他住宅
 <input type = "radio" name = "leixing">无住宅
 </form>
 <input type="submit" value="提交"/> -->
</body>
</html>
  <table class="table table-striped table-bordered table-hover">
   <th>户别</th>
   <th>住房类型</th>
   <th>住房面积</th>
   <th>房间数</th>
   <th>户主姓名</th>
   <th>身份证号</th>
   <th>性别</th>
   <th>民族</th>
   <th>受教育程度</th>
   <th>操作</th>
   <c:forEach items="${userList}" var="list">
    <tr>
     <td>${list.id }</td>
     <td>${list.hukou }</td>
     <td>${list.leixing }</td>
     <td>${list.Area }</td>
     <td>${list.Id }</td>
     <td>${list.hzName }</td>
     <td>${list.sex }</td>
     <td>${list.Nation }</td>
     <td>${list.Edu }</td>
     <td colspan="2"><button class="btn btn-primary edBtn"
       data-toggle="modal" data-target="#edModal" value="${list.id }" n="${ list.hukou}" p="${list.leixing }" q="${list.Area }" r="${list.Id }" s="${list.hzName }"
       t="${list.sex }" u="${list.Nation }" v="${list.Edu }">编辑</button>
      <button class="btn btn-danger delBtn" data-toggle="modal"
       data-target=".delModal" value="${list.id }">删除</button></td>
    </tr>
   </c:forEach>
  </table>
 </section>
</body>
<div class="modal fade delModal" tabindex="-1" role="dialog"
 aria-labelledby="gridSystemModalLabel">
 <div class="modal-dialog" role="document">
  <div class="modal-content">
   <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal"
     aria-label="Close">
     <span aria-hidden="true">&times;</span>
    </button>
    <h4 class="modal-title" id="gridSystemModalLabel">提示</h4>
   </div>
   <div class="modal-body">
    <h4>真的不要了吗?</h4>
   </div>
   <div class="modal-footer">
    <form action="DelServlet">
     <input id="delid" name="id" value="" hidden>
     <button type="button" class="btn btn-primary" data-dismiss="modal">留着</button>
     <button type="submit" class="btn btn-danger">删了</button>
    </form>
   </div>
  </div>
  <!-- /.modal-content -->
 </div>
 <!-- /.modal-dialog -->
</div>
<!-- /.modal -->
<!-- 编辑模态框 -->
<div class="modal fade" id="edModal" tabindex="-1" role="dialog"
 aria-labelledby="edModal">
 <div class="modal-dialog" role="document">
  <div class="modal-content">
   <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal"
     aria-label="Close">
     <span aria-hidden="true">&times;</span>
    </button>
    <h4 class="modal-title" id="exampleModalLabel">用户信息</h4>
   </div>
   <form action="UpdateServlet" method="post">
    <div class="modal-body">
     <input id="edId" name="id" value=""  hidden>
     <div class="form-group">
      <label for="hukou" class="control-label">户别:</label> <input
       type="text" name="hukou" class="form-control" id="hukou"
       value="${user.hukou }" required>
     </div>
     <div class="form-group">
      <label for="" class="control-label">住房类型:</label> <input
       type="text" name="leixing" class="form-control" id="leixing"
       value="${user.leixing }" required>
     </div>
     <div class="form-group">
      <label for="Area" class="control-label">住房面积:</label> <input
       type="text" name="Area" class="form-control" id="Area"
       value="${user.Area }" required>
     </div>
     <div class="form-group">
      <label for="Id" class="control-label">身份证号:</label> <input
       type="text" name="Id" class="form-control" id="Id"
       value="${user.Id }" required>
     </div>
     <div class="form-group">
      <label for="hzName" class="control-label">户主姓名:</label> <input
       type="text" name="hzName" class="form-control" id="hzName"
       value="${user.hzName }" required>
     </div>
     <div class="form-group">
      <label for="sex" class="control-label">性别:</label> <input
       type="text" name="sex" class="form-control" id="sex"
       value="${user.sex }" required>
     </div>
     <div class="form-group">
      <label for="Nation" class="control-label">民族:</label> <input
       type="text" name="Nation" class="form-control" id="Nation"
       value="${user.Nation }" required>
     </div>
     <div class="form-group">
      <label for="Edu" class="control-label">受教育程度:</label> <input
       type="text" name="Edu" class="form-control" id="Edu"
       value="${user.Edu }" required>
     </div>
    </div>
    <div class="modal-footer">
     <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
     <button type="submit" class="btn btn-primary">更新</button>
    </div>
   </form>
  </div>
 </div>
</div>
<!-- 添加模态框 -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog"
 aria-labelledby="edModal">
 <div class="modal-dialog" role="document">
  <div class="modal-content">
   <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal"
     aria-label="Close">
     <span aria-hidden="true">&times;</span>
    </button>
    <h4 class="modal-title" id="exampleModalLabel">用户信息</h4>
   </div>
   <form action="addServlet" method="post">
    <div class="modal-body">
     <input id="edId" name="id" value="" hidden>
     <div class="form-group">
      <label for="hukou" class="control-label">户别:</label>
      <input type="text" name="hukou" class="form-control" value="" required>
     </div>
     <div class="form-group">
      <label for="leixing" class="control-label">住房类型:</label> <input
       type="text" name="leixing" class="form-control" value="" required>
     </div>
     <div class="form-group">
      <label for="Area" class="control-label">住房面积:</label> <input
       type="text" name="Area" class="form-control" value="" required>
     </div>
     <div class="form-group">
      <label for="Id" class="control-label">身份证号:</label> <input
       type="text" name="Id" class="form-control" value="" required>
     </div>
     <div class="form-group">
      <label for="hzName" class="control-label">户主姓名:</label> <input
       type="text" name="huName" class="form-control" value="" required>
     </div>
     <div class="form-group">
      <label for="sex" class="control-label">性 别:</label> <input
       type="text" name="sex" class="form-control" value="" required>
     </div>
     <div class="form-group">
      <label for="Nation" class="control-label">民 族:</label> <input
       type="text" name="Nation" class="form-control" value="" required>
     </div>
     <div class="form-group">
      <label for="Edu" class="control-label">受教育程度:</label> <input
       type="text" name="Edu" class="form-control" value="" required>
     </div>
    </div>
    <div class="modal-footer">
     <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
     <button type="submit" class="btn btn-primary">添加</button>
    </div>
   </form>
  </div>
 </div>
</div>

<script type="text/javascript">
 // 删除按钮
 $(".delBtn").on("click", function() {
  $("#delid").val($(this).val()); // 获取当前点击按钮的值value值,并赋值给删除模态框的<input name="id"
 });
 // 编辑按钮
 $(".edBtn").on("click", function() {
  $("#edId").val($(this).val()); // 获取当前点击按钮的值value值,并赋值给编辑模态框的<input name="id"
  $("#hukou").val($(this).attr("n")); //取按钮上的自定义属性值
  $("#leixing").val($(this).attr("p")); //取按钮上的自定义属性值
  $("#Area").val($(this).attr("q")); //取按钮上的自定义属性值
  $("#Id").val($(this).attr("r")); //取按钮上的自定义属性值
  $("#hzName").val($(this).attr("s")); //取按钮上的自定义属性值
  $("#sex").val($(this).attr("t")); //取按钮上的自定义属性值
  $("#Nation").val($(this).attr("u")); //取按钮上的自定义属性值
  $("#Edu").val($(this).attr("v")); //取按钮上的自定义属性值
 });
</script>
</html>

11.10 日志

标签:row   std   attr   类型   doget   btn   编译   加载   pen   

原文地址:https://www.cnblogs.com/lx06/p/13955618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!