• 公司授课管理系统


    /* 时间:2020/12/7 19:27开始
    *
    *
    * */
    package dao;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.*;

    import DBUtil.DBUtil;
    import domain.User;

    public class myDao {

    /**
    * ���
    * @param course
    * @return
    */
    public boolean manager_longin(User course) {
    String sql = "insert into longin(ID,name,password,type) values('"+ course.getID() + "','"+ course.getName() +"','"+ course.getPassword()+ "','" + course.getType() + "')";
    Connection conn = DBUtil.getConn();
    Statement state = null;
    boolean f = false;
    int a = 1;

    try {
    state = conn.createStatement();
    state.executeUpdate(sql);
    } catch (Exception e) {
    e.printStackTrace();
    a=0;
    } finally {
    DBUtil.close(state, conn);
    }

    if (a > 0) {
    f = true;
    }
    return f;
    }
    public boolean delete(String x) {
    String sql = "delete from longin where ID='"+x+"'";
    Connection conn = DBUtil.getConn();
    Statement state = null;
    boolean f = false;
    int a = 1;
    try {
    state = conn.createStatement();
    state.executeUpdate(sql);
    } catch (Exception e) {
    e.printStackTrace();
    a=0;
    } finally {
    DBUtil.close(state, conn);
    }

    if (a > 0) {
    f = true;
    }
    return f;
    }
    public static List<User> list(){//查询所有方法
    String sql="select * from longin order by ID ASC";
    Connection conn=DBUtil.getConn();
    Statement st=null;
    List<User> list=new ArrayList<>();
    ResultSet rs=null;
    User bean=null;
    try {
    st=conn.createStatement();
    st.executeQuery(sql);
    rs=st.executeQuery(sql);
    while(rs.next()) {
    String ID=rs.getString("ID");
    String name = rs.getString("name");
    String type = rs.getString("type");
    String password=rs.getString("password");
    //System.out.println("dao内ID"+ID);
    bean=new User(ID,name,password,type);
    list.add(bean);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    finally {
    DBUtil.close(rs, st, conn);
    }
    return list;
    }
    public static List<User> IDlist(String str){//查询ID
    String sql="select * from longin where(ID like '%"+str+"%')";
    Connection conn=DBUtil.getConn();
    Statement st=null;
    List<User> list=new ArrayList<>();
    ResultSet rs=null;
    User bean=null;
    try {
    st=conn.createStatement();
    st.executeQuery(sql);
    rs=st.executeQuery(sql);
    while(rs.next()) {
    String ID=rs.getString("ID");
    String name = rs.getString("name");
    String type = rs.getString("type");
    String password=rs.getString("password");
    // System.out.println("dao内ID"+ID);
    bean=new User(ID,name,password,type);
    list.add(bean);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    finally {
    DBUtil.close(rs, st, conn);
    }
    return list;
    }
    public static List<User> namelist(String str){//查询姓名
    String sql="select * from longin where(name like '%"+str+"%')";
    Connection conn=DBUtil.getConn();
    Statement st=null;
    List<User> list=new ArrayList<>();
    ResultSet rs=null;
    User bean=null;
    try {
    st=conn.createStatement();
    st.executeQuery(sql);
    rs=st.executeQuery(sql);
    while(rs.next()) {
    String ID=rs.getString("ID");
    String name = rs.getString("name");
    String type = rs.getString("type");
    String password=rs.getString("password");
    // System.out.println("dao内ID"+ID);
    bean=new User(ID,name,password,type);
    list.add(bean);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    finally {
    DBUtil.close(rs, st, conn);
    }
    return list;
    }
    //修改列名 alter table student change column sname stuname varchar(20);
    public static boolean change_project(String oldname,String newname) {//更新方法


    String sql=" alter table information change column "+oldname+" "+newname+" varchar(20) ";
    //alter table 表名 add column 列名 varchar(30);
    Connection conn=DBUtil.getConn();
    boolean f=false;
    Statement st=null;
    try {
    st=conn.createStatement();
    st.executeUpdate(sql);
    f=true;
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return f;
    }
    public static boolean delete_project(String x) {//更新方法


    String sql="alter table information drop column "+x+" ";
    //alter table 表名 add column 列名 varchar(30);
    Connection conn=DBUtil.getConn();
    boolean f=false;
    Statement st=null;
    try {
    st=conn.createStatement();
    st.executeUpdate(sql);
    f=true;
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return f;
    }
    public static boolean add_project(String x) {//更新方法


    String sql="alter table information add column "+x+" varchar(20)";
    //alter table 表名 add column 列名 varchar(30);
    Connection conn=DBUtil.getConn();
    boolean f=false;
    Statement st=null;
    try {
    st=conn.createStatement();
    st.executeUpdate(sql);
    f=true;
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return f;
    }
    public static boolean update(User bean) {//更新方法
    System.out.println("开始准备更新");
    String sql="update longin set ID='"+bean.getID()+"',name='"+bean.getName()+"',type='"+bean.getType()+"'where ID='"+bean.getID()+"'";
    Connection conn=DBUtil.getConn();
    boolean f=false;
    Statement st=null;
    try {
    st=conn.createStatement();
    st.executeUpdate(sql);
    f=true;
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return f;
    }
    public User searchID(String x){//查询密码
    String sql="select * from longin where ID='"+x+"'";
    Connection conn=DBUtil.getConn();
    Statement st=null;
    List<User> list=new ArrayList<>();
    ResultSet rs=null;
    User bean=null;
    try {
    st=conn.createStatement();

    rs=st.executeQuery(sql);
    rs.next();
    String ID=rs.getString("ID");
    String name = rs.getString("name");
    String password = rs.getString("password");
    String type=rs.getString("type");

    System.out.println("数据库密码"+password);
    bean=new User(ID,name,password,type);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    finally {
    DBUtil.close(rs, st, conn);
    }
    return bean;
    }

    }

    package DBUtil;

    import java.sql.*;


    public class DBUtil {

    public static String db_url = "jdbc:mysql://localhost:3306/company?serverTimezone=GMT%2B8&useSSL=false";
    public static String db_user = "root";
    public static String db_pass = "1669268823";

    public static Connection getConn () {
    Connection conn = null;

    try {
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection(db_url, db_user, db_pass);
    } catch (Exception e) {
    e.printStackTrace();
    }
    System.out.println("Hollow Tomcent");

    return conn;
    }//end getConn

    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 pstmt = null;
    ResultSet rs = null;
    String sql ="select * from longin";
    pstmt = conn.prepareStatement(sql);
    rs = pstmt.executeQuery();
    if(rs.next()){
    System.out.println("连接成功");
    }else{
    System.out.println("连接失败");
    }
    }
    }

    package domain;

    public class User {

    private String ID;
    private String name;
    private String password;
    private String type;
    public String getID() {
    return ID;
    }
    public void setID(String iD) {
    ID = iD;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getPassword() {
    return password;
    }
    public void setPassword(String password) {
    this.password = password;
    }
    public String getType() {
    return type;
    }
    public void setType(String type) {
    this.type = type;
    }
    @Override
    public String toString() {
    return "User [ID=" + ID + ", name=" + name + ", password=" + password + ", type=" + type + "]";
    }
    public User(String iD, String name, String password, String type) {
    super();
    ID = iD;
    this.name = name;
    this.password = password;
    this.type = type;
    }

    }

    package servlet;

    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.text.SimpleDateFormat;
    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 javax.servlet.http.HttpSession;

    import dao.myDao;
    import domain.User;

    /**
    * Servlet implementation class AddServlet
    */
    @WebServlet("/addServlet")
    public class doServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#HttpServlet()
    */
    public doServlet() {
    super();
    // TODO Auto-generated constructor stub
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void manager_longin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    request.setCharacterEncoding("utf-8");
    String ID =request.getParameter("ID");
    String password =request.getParameter("password");
    String name = request.getParameter("name");
    String type =request.getParameter("type");
    System.out.println("学号是"+ID+"姓名"+name+"密码"+password+"类型"+type);
    User user = new User(ID,name,password,type);
    myDao cd = new myDao();
    if(cd.manager_longin(user)) {
    request.setAttribute("message","成功");
    request.getRequestDispatcher("manager_login.jsp").forward(request,response);
    }
    else
    {
    request.setAttribute("message","失败");
    request.getRequestDispatcher("manager_login.jsp").forward(request,response);
    }

    }
    private void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("utf-8");
    HttpSession session = request.getSession();
    String ID=(String)session.getAttribute("ID");
    System.out.println("存入的ID为"+ID);
    String cxfs=request.getParameter("cxfs");
    String value=request.getParameter("value");
    System.out.println("cxfs为"+cxfs+"value"+value);
    if(cxfs.equals(""))
    {
    List<User> list=myDao.list();
    System.out.println(list.toString());
    request.setAttribute("list", list);
    }
    else if(cxfs.equals("ID"))
    {
    List<User> list=myDao.IDlist(value);
    request.setAttribute("list", list);
    }
    else if(cxfs.equals("姓名"))
    {
    List<User> list=myDao.namelist(value);
    request.setAttribute("list", list);
    }
    request.getRequestDispatcher("list.jsp").forward(request,response);
    }
    private void searchMyInformation(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("utf-8");
    HttpSession session = request.getSession();
    String ID=(String)session.getAttribute("ID");
    String myType=(String)session.getAttribute("type");
    System.out.println("存入的ID为"+ID);
    myDao cd = new myDao();
    User user = cd.searchID(ID);
    List<User> list=new ArrayList<>();
    list.add(user);
    request.setAttribute("list", list);

    System.out.println("成功");
    request.getRequestDispatcher("list_information.jsp").forward(request,response);

    }
    private void back(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("utf-8");
    HttpSession session = request.getSession();

    String myType=(String)session.getAttribute("type");
    System.out.println("type为"+myType);
    request.setAttribute("message","返回主页");
    if(myType.equals("1.员工"))
    request.getRequestDispatcher("person.jsp").forward(request,response);
    else if(myType.equals("2.教师"))
    request.getRequestDispatcher("tercher.jsp").forward(request,response);
    else
    request.getRequestDispatcher("manager.jsp").forward(request,response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("utf-8");
    String ID = request.getParameter("ID");
    String name = request.getParameter("name");
    String password = request.getParameter("password");
    String type = request.getParameter("type");
    User bean=new User(ID,name,password,type);
    System.out.println("ID"+ID+"姓名"+name);
    if(myDao.update(bean))
    {
    request.setAttribute("message", "修改成功");
    if(type.equals("1.员工"))
    {
    if(type.equals("1.员工"))
    request.getRequestDispatcher("person.jsp").forward(request,response);
    else if(type.equals("2.教师"))
    request.getRequestDispatcher("tercher.jsp").forward(request,response);
    else
    request.getRequestDispatcher("manager.jsp").forward(request,response);
    }
    }
    else
    {
    request.setAttribute("message", "修改失败");
    if(type.equals("1.员工"))
    request.getRequestDispatcher("person.jsp").forward(request,response);
    else if(type.equals("2.教师"))
    request.getRequestDispatcher("tercher.jsp").forward(request,response);
    else
    request.getRequestDispatcher("manager.jsp").forward(request,response);
    }

    }
    private void searchID(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("utf-8");
    HttpSession session = request.getSession();
    String ID =request.getParameter("ID");
    String mypassword =request.getParameter("password");
    String myType=request.getParameter("type");
    session.setAttribute("ID", ID);
    session.setAttribute("type", myType);
    System.out.println(ID+" "+mypassword);
    myDao cd = new myDao();
    User list = cd.searchID(ID);
    String rightPassword = list.getPassword();
    System.out.println(rightPassword);
    if(mypassword.equals(rightPassword)&&myType.equals(list.getType()))
    {
    request.setAttribute("message","密码正确");
    if(myType.equals("1.员工"))
    request.getRequestDispatcher("person.jsp").forward(request,response);
    else if(myType.equals("2.教师"))
    request.getRequestDispatcher("tercher.jsp").forward(request,response);
    else
    request.getRequestDispatcher("manager.jsp").forward(request,response);
    }

    else
    {
    request.setAttribute("message","密码错误或身份错误");
    request.getRequestDispatcher("login.jsp").forward(request,response);
    }
    }
    private void add_project(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    String project=request.getParameter("project");
    System.out.println("添加的课程为"+project);
    myDao cd=new myDao();
    if(cd.add_project(project))
    {
    request.setAttribute("message", "添加成功");
    request.getRequestDispatcher("manager.jsp").forward(request, response);
    }
    else
    {
    request.setAttribute("message", "添加失败");
    request.getRequestDispatcher("addProject.jsp").forward(request, response);
    }
    }
    private void change_project(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    String oldname=request.getParameter("oldname");
    String newname=request.getParameter("newname");
    System.out.println("修改课程"+oldname+"为"+newname);
    myDao cd=new myDao();
    if(cd.change_project(oldname,newname))
    {
    request.setAttribute("message", "修改成功");
    request.getRequestDispatcher("manager.jsp").forward(request, response);
    }
    else
    {
    request.setAttribute("message", "修改失败");
    request.getRequestDispatcher("change_project.jsp").forward(request, response);
    }
    }
    private void delete_project(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    String project=request.getParameter("project");

    System.out.println("删除课程"+project);
    myDao cd=new myDao();
    if(cd.delete_project(project))
    {
    request.setAttribute("message", "删除成功");
    request.getRequestDispatcher("manager.jsp").forward(request, response);
    }
    else
    {
    request.setAttribute("message", "删除失败");
    request.getRequestDispatcher("change_project.jsp").forward(request, response);
    }
    }
    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    String ID=request.getParameter("ID");
    System.out.println("删除ID"+ID);
    myDao cd=new myDao();
    if(cd.delete(ID))
    {
    request.setAttribute("message", "删除成功");
    request.getRequestDispatcher("list.jsp").forward(request, response);
    }
    else
    {
    request.setAttribute("message", "删除失败");
    request.getRequestDispatcher("list.jsp").forward(request, response);
    }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("utf-8");
    HttpSession session = request.getSession();
    String method =request.getParameter("method");
    if(method.equals("manager_login"))
    {
    manager_longin(request, response);
    }
    else if(method.equals("login"))
    {
    try {
    searchID(request,response);
    } catch (Exception e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    }
    else if(method.equals("update"))
    {
    try {
    update(request,response);
    } catch (Exception e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    }
    else if(method.equals("list"))
    {
    try {
    list(request,response);
    } catch (Exception e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    }
    else if(method.equals("searchMyInformation"))
    {
    try {
    searchMyInformation(request,response);
    } catch (Exception e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    }
    else if(method.equals("back"))
    {
    try {
    back(request,response);
    } catch (Exception e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    }
    else if(method.equals("delete"))
    {
    try {
    delete(request,response);
    } catch (Exception e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    }
    else if(method.equals("add_project"))
    {
    try {
    add_project(request,response);
    } catch (Exception e) {
    // TODO 自动生成的 catch 块
    e.printStackTrace();
    }
    }

    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);

    }

    }

  • 相关阅读:
    [LeetCode#252] Meeting Rooms
    [LeetCode#247] Strobogrammatic Number II
    [LeetCode#265] Paint House II
    [LeetCode#256] Paint House
    [LeetCode#279] Perfect Squares
    [LeetCode#259] 3Sum Smaller
    [LeetCode#261] Graph Valid Tree
    [LeetCode#271] Encode and Decode Strings
    xfsdump的选项-L和-M
    Centos7 报错welcome to emergency mode
  • 原文地址:https://www.cnblogs.com/wrx166/p/14165593.html
Copyright © 2020-2023  润新知