赞
踩
- <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
- <!DOCTYPE html>
- <html>
- <head>
- <title>会议室使用情况</title>
- <script src="js/jquery-1.12.4.js"></script>
- <style>
- div{
- width: 500px;
- margin: 0px auto;
- text-align: right;
- }
- table{
- width: 500px;
- text-align: center;
- }
- a{
- color: deepskyblue;
- }
- table,tr,th,td{
- border: 1px solid black;
- }
- tr:nth-of-type(1){
- background: darkgray;
- }
- </style>
- </head>
- <body>
- <div>
- <table>
- <tr>
- <th>预定编号</th>
- <th>会议室</th>
- <th>预定人</th>
- <th>日期</th></tr>
- </table>
- <a href="index1.jsp">预定会议室</a>
- </div>
- </body>
- <script>
- $(function (){
- $.ajax({
- url:"cha",
- type:"post",
- data:"",
- dataType:"text",
- success: function(result){
- if(result.length>0){
- $("table").append(result);
- }else{
- alert("显示异常");
- }
- },
- error: function(){
- alert("异步请求异常");
- }
- })
- })
- </script>
- </html>
第二个html页面
- <%--
- Created by IntelliJ IDEA.
- User: Administrator
- Date: 2023/7/24
- Time: 23:10
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>会议室预订</title>
- <script src="js/jquery-1.12.4.js"></script>
- <style>
- div{
- width: 450px;
- margin: 0px auto;
- }
- table{
- width: 450px;
- }
- tr:nth-of-type(1){
- background: darkgray;
- }
- h3{
- text-align: center;
- }
-
- th{
- text-align: right;
- }
- td{
- text-align: left;
- }
- table,tr,th,td{
- border: 1px solid black;
- }
- #aa{
- text-align: center;
- }
- #bb{
- text-align: center;
- }
- </style>
- </head>
- <body>
- <div>
- <form action="index.jsp" method="post">
- <table>
- <tr><th colspan="2" id="bb"><span>会议室预定</span></th></tr>
- <tr><th>会议室:</th><td><select name="" id="hys">
- <option>一号会议室</option>
- <option>二号会议室</option>
- <option>三号会议室</option>
- </select></td></tr>
- <tr><th>申请人:</th><td><input type="text" name="" id="sqr"></td></tr>
- <tr><th>预定日期:</th><td><input type="text" name="" id="ydrq">日期格式yyyy-mm-dd</td></tr>
- <tr><th colspan="2" id="aa"><span><input type="submit" value="申请"> <input type="reset" value="重置"></span></th></tr>
- </table>
- </form>
- </div>
- </body>
- <script>
- $(function (){
- $("form").submit(function (){
- var hys = $("#hys").val();
- var sqr = $("#sqr").val();
- var ydrq = $("#ydrq").val();
- var rq = /^\d{4}\-\d{1,2}\-\d{1,2}$/
- if(sqr==null || sqr==""||
- ydrq=="" || ydrq==null){
- alert("请填写完整信息")
- return false;
- }else{
- if(!rq.test(ydrq)){
- alert("日期格式有误")
- return false;
- }else{
- $.ajax({
- url:"add",
- type:"post",
- data:{
- hys : $("#hys").val(),
- sqr : $("#sqr").val(),
- ydrq : $("#ydrq").val(),
- },
- dataType:"text",
- })
- }
- }
- })
- })
- </script>
- </html>
Dao包
basedao.java
数据库的连接跟关闭
- package dao;
-
- import java.sql.*;
-
- public class BaseDao {
- Connection conn;
- PreparedStatement pstmt;
- ResultSet rs;
- static{
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- throw new RuntimeException(e);
- }
- }
- public Connection LianJie(){
- if(conn==null){
- try {
- conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test","root","123");
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- return conn;
- }
- public void GuanBi(){
- try {
- if(rs!=null){
- rs.close();
- }
- if(pstmt!=null){
- pstmt.close();
- }
- if(conn!=null){
- conn.close();
- }
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- }
- }
操作数据库的增查
- package dao;
-
- import entity.MeetingRoom;
-
- import java.sql.*;
-
- public class MeetingRoomDao extends BaseDao{
- Connection conn;
- PreparedStatement pstmt;
- ResultSet rs;
- public String Cha(){
- String sql = "select * from MEETINGROOM";
- StringBuilder sb = new StringBuilder();
- if(conn==null){
- conn=LianJie();
- }
- try {
- pstmt = conn.prepareStatement(sql);
- rs = pstmt.executeQuery();
- while(rs.next()){
- int id = rs.getInt("id");
- String meetingName = rs.getString("meeting_name");
- Date meetingOrder = rs.getDate("meeting_order");
- String advanceName = rs.getString("advance_name");
- sb.append("<tr>" +
- "<th>"+id+"</th>" +
- "<th>"+meetingName+"</th>" +
- "<th>"+meetingOrder+"</th>" +
- "<th>"+advanceName+"</th>" +
- "</tr>");
- }
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- GuanBi();
- return sb.toString();
- }
- public int Tian(MeetingRoom m){
- String sql = "insert into MEETINGROOM values(default,?,?,?)";
- if(conn==null){
- conn=LianJie();
- }
- int row=0;
- try {
- pstmt = conn.prepareStatement(sql);
- pstmt.setString(1,m.getMeeting_name());
- pstmt.setString(2, m.getMeeting_order());
- pstmt.setString(3,m.getAdvance_name());
- row = pstmt.executeUpdate();
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- return row;
- }
- }
entity包内存放实体类
- package entity;
-
- public class MeetingRoom {
- private String meeting_name;
- private String meeting_order;
- private String advance_name;
-
- public MeetingRoom() {
- }
-
- public MeetingRoom(String meeting_name, String meeting_order, String advance_name) {
- this.meeting_name = meeting_name;
- this.meeting_order = meeting_order;
- this.advance_name = advance_name;
- }
-
- public String getMeeting_name() {
- return meeting_name;
- }
-
- public void setMeeting_name(String meeting_name) {
- this.meeting_name = meeting_name;
- }
-
- public String getMeeting_order() {
- return meeting_order;
- }
-
- public void setMeeting_order(String meeting_order) {
- this.meeting_order = meeting_order;
- }
-
- public String getAdvance_name() {
- return advance_name;
- }
-
- public void setAdvance_name(String advance_name) {
- this.advance_name = advance_name;
- }
- }
- package servlet;
-
- import dao.MeetingRoomDao;
- import entity.MeetingRoom;
-
- 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.IOException;
-
- @WebServlet("/add")
- public class AddServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- MeetingRoomDao mrd = new MeetingRoomDao();
- String hys = req.getParameter("hys");
- String sqr = req.getParameter("sqr");
- String ydrq = req.getParameter("ydrq");
- int row = mrd.Tian(new MeetingRoom(hys,ydrq,sqr));
- if(row>0){
- System.out.println("添加成功");
- }else{
- System.out.println("添加失败");
- }
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("utf-8");
- doGet(req,resp);
- }
- }
- package servlet;
-
- import dao.MeetingRoomDao;
-
- 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.IOException;
- import java.io.PrintWriter;
-
- @WebServlet("/cha")
- public class ChaServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- MeetingRoomDao mrd = new MeetingRoomDao();
- String cha = mrd.Cha();
- resp.setContentType("text/html");
- resp.setCharacterEncoding("utf-8");
- PrintWriter out = resp.getWriter();
- out.print(cha);
- out.close();
- }
-
- @Override
- protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- req.setCharacterEncoding("utf-8");
- doGet(req,resp);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。