期末1
建的包
jar包
java
Dao
package Dao; /*信2005-1班 耿晴 20204010*/ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import DBUtil.DBUtil; import Javabean.*; public class Dao { public int searchPid(String username,String password) { Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; int pid=0; try { String sql="select pid from user where username= '"+username+"' and password='"+password+"'"; state = conn.createStatement(); rs = state.executeQuery(sql); while(rs.next()){ pid = rs.getInt("pid"); } } catch(SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return pid; } public boolean adduser(String tpid,String password,int pid) { Connection conn = DBUtil.getConn(); PreparedStatement pstmt = null; boolean f = false; int a=0; try { String sql = "insert into user(username,password,pid) value(?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, tpid); pstmt.setString(2, password); pstmt.setInt(3, pid); a = pstmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); } finally { DBUtil.close(pstmt, conn); } if(a>0) f=true; return f; } public boolean addhuiyishi(Meetroom meetroom) { Connection conn = DBUtil.getConn(); PreparedStatement pstmt = null; boolean f = false; int a=0; try { String sql = "insert into meetroom(meetroomid,meetroomname,address,readystate,status,capacity) value(?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, meetroom.getMeetroomid()); pstmt.setString(2, meetroom.getMeetroomname()); pstmt.setString(3, meetroom.getAddress()); pstmt.setString(4, meetroom.getReadystate()); pstmt.setString(5, meetroom.getStatus()); pstmt.setString(6, meetroom.getCapacity()); a = pstmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); } finally { DBUtil.close(pstmt, conn); } if(a>0) f=true; return f; } public String searchzhiyuanname(String zhiyuanid) { Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; String zhiyuanname=null; try { String sql="select zhiyuanname from zhiyuan where zhiyuanid= '"+zhiyuanid+"'"; state = conn.createStatement(); rs = state.executeQuery(sql); while(rs.next()){ zhiyuanname = rs.getString("zhiyuanname"); } } catch(SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return zhiyuanname; } public boolean addzhiyuan(Zhiyuan zhiyuan) { Connection conn = DBUtil.getConn(); PreparedStatement pstmt = null; boolean f = false; int a=0; try { String sql = "insert into zhiyuan(zhiyuanid,zhiyuanname,sex,department,phone,position) value(?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, zhiyuan.getZhiyuanid()); pstmt.setString(2, zhiyuan.getZhiyuanname()); pstmt.setString(3, zhiyuan.getSex()); pstmt.setString(4, zhiyuan.getDepartment()); pstmt.setString(5, zhiyuan.getPhone()); pstmt.setString(6, zhiyuan.getPosition()); a = pstmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); } finally { DBUtil.close(pstmt, conn); } if(a>0) f=true; return f; } public boolean addhuiyi(Meeting meeting) { Connection conn = DBUtil.getConn(); PreparedStatement pstmt = null; boolean f = false; int a=0; try { String sql = "insert into meeting(meeting,zhiyuanname,sex,department,phone,position) value(?,?,?,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, meeting.getMeetingid()); pstmt.setString(2, meeting.getMeetingname()); pstmt.setString(3, meeting.getMeetingcontent()); pstmt.setString(4, meeting.getMeetingbegin()); pstmt.setString(5, meeting.getMeetingend()); pstmt.setString(6, meeting.getMeetingnum()); pstmt.setString(7, meeting.getParticipants()); pstmt.setString(8, meeting.getMeetroomid()); pstmt.setString(9, meeting.getUserid()); pstmt.setString(10, meeting.getAauditstatus()); pstmt.setString(11, meeting.getAuditmind()); a = pstmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); } finally { DBUtil.close(pstmt, conn); } if(a>0) f=true; return f; } public List<Zhiyuan> liulanzhiyuan(String zhiyuanid) { String sql = "select * from zhiyuan where zhiyuanid= '"+zhiyuanid+"'"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; List<Zhiyuan> list = new ArrayList<>(); try { state = conn.createStatement(); rs = state.executeQuery(sql); Zhiyuan zhiyuan = null; while (rs.next()) { //int id = rs.getInt("id"); String zhiyuanid1 = rs.getString("zhiyuanid"); String zhiyuanname = rs.getString("zhiyuananme"); String sex = rs.getString("sex"); String department = rs.getString("department"); String phone = rs.getString("phone"); String position = rs.getString("position"); zhiyuan = new Zhiyuan(zhiyuanid1,zhiyuanname,sex,department,phone,position); list.add(zhiyuan); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public List<Meetroom> liulanhuiyishi() { String sql = "select * from meetroom"; List<Meetroom> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Meetroom bean = null; while (rs.next()) { int id = rs.getInt("id"); String meetroomid = rs.getString("meetroomid"); String meetroomname = rs.getString("meetroomname"); String address = rs.getString("address"); String readystate = rs.getString("readystate"); String status = rs.getString("status"); String capacity = rs.getString("capacity"); bean = new Meetroom(id,meetroomid,meetroomname,address,readystate,status,capacity); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public void updatemima(String password,String password1) { String sql="update user set password=? where username=?"; Connection con; try { con = DBUtil.getConn(); PreparedStatement pa=con.prepareStatement(sql); pa.setString(1,password1); pa.setString(2,password); pa.execute(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean updatehuiyishi(Meetroom meetroom) { String sql = "update meetroom set meetroomname='" + meetroom.getMeetroomname() + "', address='" + meetroom.getAddress() + "', readystate='" +meetroom.getReadystate() + "',status='" +meetroom.getStatus() + "', capacity='" + meetroom.getCapacity() +"'where meetroomid='" + meetroom.getMeetroomid() + "'"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); System.out.println("看看是不是执行了"); a = state.executeUpdate(sql); System.out.println(a); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } System.out.println(f); return f; } }
DBUtil
package DBUtil; /*信2005-1班 耿晴 20204010*/ import java.sql.*; /** * 数据库连接工具 * @author Hu * */ public class DBUtil { public static String url = "jdbc:mysql://localhost:3306/qimo?serverTimezone=UTC"; public static String user = "root"; public static String password = "152486"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 关闭连接 * @param state * @param conn */ public static void close (PreparedStatement preparedState, Connection conn) { if (preparedState != null) { try { preparedState.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, PreparedStatement preparedState, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (preparedState != null) { try { preparedState.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭连接 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement preparedStatement = null; ResultSet rs = null; String sql ="select * from qimo"; preparedStatement = conn.prepareStatement(sql); rs = preparedStatement.executeQuery(); if(rs.next()){ System.out.println("数据库为空"); } else{ System.out.println("数据库不为空"); } } }
Meeting
package Javabean; /*信2005-1班 耿晴 20204010*/ public class Meeting { private int id; private String mpid; private String meetingid; private String meetingname; private String meetingcontent; private String meetingbegin; private String meetingend; private String meetingnum; private String participants; private String meetroomid; private String userid; private String auditstatus; private String auditmind; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getMpid() { return mpid; } public void setMpid(String mpid) { this.mpid = mpid; } public String getAuditstatus() { return auditstatus; } public String getMeetingid() { return meetingid; } public void setMeetingid(String meetingid) { this.meetingid = meetingid; } public String getMeetingname() { return meetingname; } public void setMeetingname(String meetingname) { this.meetingname = meetingname; } public String getMeetingcontent() { return meetingcontent; } public void setMeetingcontent(String meetingcontent) { this.meetingcontent = meetingcontent; } public String getMeetingbegin() { return meetingbegin; } public void setMeetingbegin(String meetingbegin) { this.meetingbegin = meetingbegin; } public String getMeetingend() { return meetingend; } public void setMeetingend(String meetingend) { this.meetingend = meetingend; } public String getMeetingnum() { return meetingnum; } public void setMeetingnum(String meetingnum) { this.meetingnum = meetingnum; } public String getParticipants() { return participants; } public void setParticipants(String participants) { this.participants = participants; } public String getMeetroomid() { return meetroomid; } public void setMeetroomid(String meetroomid) { this.meetroomid = meetroomid; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getAauditstatus() { return auditstatus; } public void setAuditstatus(String auditstatus) { this.auditstatus = auditstatus; } public String getAuditmind() { return auditmind; } public void setAuditmind(String auditmind) { this.auditmind = auditmind; } public Meeting() {} public Meeting(int id,String meetingid,String meetingname,String meetingcontent,String meetingbegin,String meetingend,String meetingnum,String participants,String meetroomid,String userid,String auditstatus,String auditmind) { this.id = id; this.meetingid = meetingid; this.meetingname = meetingname; this.meetingcontent = meetingcontent; this.meetingbegin = meetingbegin; this.meetingend = meetingend; this.meetingnum = meetingnum; this.participants = participants; this.meetroomid = meetroomid; this.userid = userid; this.auditstatus = auditstatus; this.auditmind = auditmind; } public Meeting(String meetingid,String meetingname,String meetingcontent,String meetingbegin,String meetingend,String meetingnum,String participants,String meetroomid,String userid,String auditstatus,String auditmind) { //this.id = id; this.meetingid = meetingid; this.meetingname = meetingname; this.meetingcontent = meetingcontent; this.meetingbegin = meetingbegin; this.meetingend = meetingend; this.meetingnum = meetingnum; this.participants = participants; this.meetroomid = meetroomid; this.userid = userid; this.auditstatus = auditstatus; this.auditmind = auditmind; } }
Meetroom
package Javabean; /*信2005-1班 耿晴 20204010*/ public class Meetroom { private int id; private String meetroomid; private String meetroomname; private String address; private String readystate; private String status; private String capacity; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getMeetroomid() { return meetroomid; } public void setMeetroomid(String meetroomid) { this.meetroomid = meetroomid; } public String getMeetroomname() { return meetroomname; } public void setMeetroomname(String meetroomname) { this.meetroomname = meetroomname; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getReadystate() { return readystate; } public void setReadystate(String readystate) { this.readystate = readystate; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getCapacity() { return capacity; } public void setCapacity(String capacity) { this.capacity = capacity; } public Meetroom() {} public Meetroom(int id,String meetroomid,String meetroomname,String address,String readystate,String status,String capacity) { this.id = id; this.meetroomid = meetroomid; this.meetroomname = meetroomname; this.address = address; this.readystate = readystate; this.status = status; this.capacity = capacity; } public Meetroom(String meetroomid,String meetroomname,String address,String readystate,String status,String capacity) { //this.id = id; this.meetroomid = meetroomid; this.meetroomname = meetroomname; this.address = address; this.readystate = readystate; this.status = status; this.capacity = capacity; } }
Zhiyuan
package Javabean; /*信2005-1班 耿晴 20204010*/ public class Zhiyuan { private int id; private String zhiyuanid; private String zhiyuanname; private String sex; private String department; private String phone; private String position; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getZhiyuanid() { return zhiyuanid; } public void setZhiyuanid(String zhiyuanid) { this.zhiyuanid = zhiyuanid; } public String getZhiyuanname() { return zhiyuanname; } public void setZhiyuanname(String zhiyuanname) { this.zhiyuanname = zhiyuanname; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public Zhiyuan() {} public Zhiyuan(int id,String zhiyuanid,String zhiyuanname,String sex,String department,String phone,String position) { this.id = id; this.zhiyuanid = zhiyuanid; this.zhiyuanname = zhiyuanname; this.sex = sex; this.department = department; this.phone = phone; this.position = position; } public Zhiyuan(String zhiyuanid,String zhiyuanname,String sex,String department,String phone,String position) { //this.id = id; this.zhiyuanid = zhiyuanid; this.zhiyuanname = zhiyuanname; this.sex = sex; this.department = department; this.phone = phone; this.position = position; } }
qimoServlet
package qimoServlet; /*信2005-1班 耿晴 20204010*/ import java.io.IOException; import java.util.List; 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 Dao.Dao; import Javabean.*; @WebServlet("/qimoServlet") public class qimoServlet extends HttpServlet{ /** * 特有id号 */ private static final long serialVersionUID = 1L; Dao dao = new Dao(); /** * 方法选择 * @return * @throws IOException * @throws ServletException */ protected void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if("login".equals(method)) { login(req,resp); }else if("addhuiyishi".equals(method)) { addhuiyishi(req,resp); }else if("addzhiyuan".equals(method)) { addzhiyuan(req,resp); }else if("liulanzhiyuan".equals(method)) { liulanzhiyuan(req,resp); }else if("liulanhuiyishi".equals(method)) { liulanhuiyishi(req,resp); }else if("updatemima".equals(method)) { updatemima(req,resp); } } private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub req.setCharacterEncoding("utf-8"); String username = req.getParameter("username"); String password=req.getParameter("password"); int pid=dao.searchPid(username,password); System.out.println(pid); if(pid==1) { String zhiyuanname=dao.searchzhiyuanname(username); System.out.println(zhiyuanname); req.getSession().setAttribute("username", username); req.getSession().setAttribute("zhiyuanname", zhiyuanname); } if(pid==2) { // req.getSession().setAttribute("username1", username); } req.setAttribute("pid", pid); req.getRequestDispatcher("houtai.jsp").forward(req, resp); } private void addhuiyishi(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String meetroomid = req.getParameter("meetroomid"); String meetroomname = req.getParameter("meetroomname"); String address = req.getParameter("address"); String readystate = req.getParameter("readystate"); String status = req.getParameter("status"); String capacity = req.getParameter("capacity"); Meetroom meetroom=new Meetroom(meetroomid,meetroomname,address,readystate,status,capacity); if(dao.addhuiyishi(meetroom)) { req.setAttribute("meetroom",meetroom); req.setAttribute("message","添加成功" ); req.getRequestDispatcher("addhuiyishi.jsp").forward(req, resp); }else { req.setAttribute("message","添加失败,请重新输入" ); req.getRequestDispatcher("addhuiyishi.jsp").forward(req, resp); } } private void addzhiyuan(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String zhiyuanid = req.getParameter("zhiyuanid"); String zhiyuanname = req.getParameter("zhiyuanname"); String sex = req.getParameter("sex"); String department = req.getParameter("department"); String phone = req.getParameter("phone"); String position = req.getParameter("position"); int pid=1; String password = "123456"; Zhiyuan zhiyuan=new Zhiyuan(zhiyuanid,zhiyuanname,sex,department,phone,position); if(dao.addzhiyuan(zhiyuan)&&dao.adduser(zhiyuanid,password,pid)) { req.setAttribute("zhiyuan",zhiyuan); req.setAttribute("message","添加成功" ); req.getRequestDispatcher("addzhiyuan.jsp").forward(req, resp); }else { req.setAttribute("message","经纪人姓名重复,请重新输入" ); req.getRequestDispatcher("addzhiyuan.jsp").forward(req, resp); } } private void liulanzhiyuan(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String zhiyuanid=(String) req.getSession().getAttribute("username"); List<Zhiyuan> zhiyuans = dao.liulanzhiyuan(zhiyuanid); req.setAttribute("zhiyuans",zhiyuans); req.getRequestDispatcher("liulanzhiyuan.jsp").forward(req, resp); } private void liulanhuiyishi(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); List<Meetroom> meetrooms = dao.liulanhuiyishi(); req.setAttribute("meetrooms", meetrooms); req.getRequestDispatcher("liulanhuiyishi.jsp").forward(req, resp); } private void updatemima(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String password = req.getParameter("password"); String password1 = req.getParameter("password1"); dao.updatemima(password, password1); resp.sendRedirect("updatemima.jsp?updatemima=yes"); } private void updatehuiyishi(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String meetroomid=(String) req.getSession().getAttribute("username"); String meetroomname = req.getParameter("meetroomname"); String address = req.getParameter("address"); String readystate = req.getParameter("readystate"); String status = req.getParameter("status"); String capacity = req.getParameter("capacity"); Meetroom meetroom =new Meetroom(meetroomid,meetroomname,address,readystate,status,capacity); if(dao.updatehuiyishi(meetroom)) { req.setAttribute("message","修改成功" ); req.getRequestDispatcher("updatehuiyishi.jsp").forward(req, resp); } else { req.setAttribute("message","修改失败" ); req.getRequestDispatcher("updatehuiyishi.jsp").forward(req, resp); } } }