首页 > 其他 > 详细

客车网上售票系统02

时间:2020-07-17 15:06:03      阅读:35      评论:0      收藏:0      [点我收藏+]

  一、今日完成任务

  1. 登录
  2. 用户管理
  3. 留言管理
  4. 公告管理
  5. 密码修改

  二、核心源码

    该项目使用技术为JSP+Servlet,项目分包如下图

           技术分享图片

    (1) 工具类(DBUtil.java)

       此类中的方法包含,链接数据库、关闭链接、执行增删改的方法、执行查询的方法。代码:

技术分享图片
package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
    private static final String DRIVER = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://localhost:3306/cardb";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";
    private static PreparedStatement ps = null;
    private static ResultSet rs = null;
    private static Connection conn = null;
    /**
     * 初始化链接
     */
    private static void init() {
        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**
     * 查询方法
     * @param sql
     * @param obj
     * @return
     */
    public static ResultSet select(String sql, Object[] obj) {
        init();
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                ps.setObject(i + 1, obj[i]);
            }
            rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    /**
     * 增删改方法
     * @param sql
     * @param obj
     * @return
     */
    public static int update(String sql, Object[] obj) {
        init();
        int a = 0;
        try {
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < obj.length; i++) {
                ps.setObject(i + 1, obj[i]);
            }
            a = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return a;
    }
    /**
     * 关闭链接
     */
    private static void close() {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
View Code

    (2) 登录、用户管理、密码修改功能核心代码(留言管理、公告管理基本类似,此处省略)

技术分享图片
package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDao;
import entity.UserInfo;

public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = -384554213505312705L;
    private UserDao ud = new UserDao();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");
        if(action.equals("login")){
            login(req, resp);
        }else if(action.equals("showAll")){
            showAll(req, resp);
        }else if(action.equals("userAdd")){
            userAdd(req, resp);
        }else if(action.equals("userDel")){
            userDel(req, resp);
        }else if(action.equals("userShowOne")){
            userShowOne(req, resp);
        }else if(action.equals("userUpd")){
            userUpd(req, resp);
        }else if(action.equals("userPass")){
            userPass(req, resp);
        }else if(action.equals("userPassUpd")){
            userPassUpd(req, resp);
        }
    }
    /**
     *     密码修改
     */
    protected void userPassUpd(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id")); 
        String newPassword = req.getParameter("newPassword");
        int i = ud.userPassUpd(id,newPassword);
        if(i>0){
            req.getSession().removeAttribute("userInfo");
            resp.sendRedirect("afterpage/login.jsp");
        }
    }
    /**
     *    查询密码是否存在 
     */
    protected void userPass(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String upass = req.getParameter("oldPassword");
        boolean b = ud.userPass(upass);
        if(b){
            resp.getWriter().printf("true");
        }else{
            resp.getWriter().printf("false");
        }
    }
    
    /**
     *    用户修改 
     */
    protected void userUpd(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        String uname = req.getParameter("uname");
        String upass = req.getParameter("upass");
        String realname = req.getParameter("realname");
        String sex = req.getParameter("sex");
        int age = Integer.parseInt(req.getParameter("age"));
        String tel = req.getParameter("tel");
        String addr = req.getParameter("addr");
        int type = 2;
        UserInfo info = new UserInfo(id, uname, upass, realname, sex, age, tel, addr, type);
        int i = ud.userUpd(info);
        if(i > 0){
            showAll(req, resp);
        }
    }
    /**
     *    查询单条信息
     */
    protected void userShowOne(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String tab = req.getParameter("tab");
        int id = Integer.parseInt(req.getParameter("id"));
        UserInfo info = ud.userShowOne(id);
        req.getSession().setAttribute("info", info);
        if(tab.equals("see")){
            resp.sendRedirect("afterpage/userView.jsp");
        }else if(tab.equals("upd")){
            resp.sendRedirect("afterpage/userUpdate.jsp");
        }
        
    }
    /**
     *    删除用户 
     */
    protected void userDel(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        int i = ud.userDel(id);
        if(i > 0){
            showAll(req, resp);
        }
    }
    /**
     *    添加用户信息 
     */
    protected void userAdd(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String uname = req.getParameter("uname");
        String upass = req.getParameter("upass");
        String realname = req.getParameter("realname");
        String sex = req.getParameter("sex");
        int age = Integer.parseInt(req.getParameter("age"));
        String tel = req.getParameter("tel");
        String addr = req.getParameter("addr");
        UserInfo info = new UserInfo(uname, upass, realname, sex, age, tel, addr);
        int i = ud.userAdd(info);
        if(i > 0){
            showAll(req, resp);
        }
    }
    /**
     *  查询全部用户
     */
    protected void showAll(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        List<UserInfo> list = ud.showAll();
        req.getSession().setAttribute("userList", list);
        resp.sendRedirect("afterpage/userList.jsp");
    }
    /**
     *    管理员登录 
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String uname = req.getParameter("uname");
        String upass = req.getParameter("upass");
        UserInfo userInfo = ud.login(uname,upass);
        if(userInfo != null){
            req.getSession().setAttribute("userInfo", userInfo);
            resp.sendRedirect("afterpage/index.jsp");
        }else{
            resp.sendRedirect("afterpage/login.jsp");
        }
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}
View Code
技术分享图片
package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.DBUtil;
import entity.UserInfo;

public class UserDao {

    public UserInfo login(String uname, String upass) {
        UserInfo userInfo = null;
        String sql = "select * from userInfo where uname = ? and upass = ? and type = 1";
        Object[] obj = {uname,upass};
        ResultSet rs = DBUtil.select(sql, obj);
        try {
            if(rs.next()){
                Integer id = rs.getInt("id");
                String realname = rs.getString("realname");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                String tel = rs.getString("tel");
                String addr = rs.getString("addr");
                int type = rs.getInt("type");
                userInfo = new UserInfo(id, uname, upass, realname, sex, age, tel, addr, type);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userInfo;
    }
    public List<UserInfo> showAll() {
        List<UserInfo> list = new ArrayList<UserInfo>();
        String sql = "select * from userInfo where type = 2";
        Object[] obj = {};
        ResultSet rs = DBUtil.select(sql, obj);
        try {
            while(rs.next()){
                Integer id = rs.getInt("id");
                String uname = rs.getString("uname");
                String upass = rs.getString("upass");
                String realname = rs.getString("realname");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                String tel = rs.getString("tel");
                String addr = rs.getString("addr");
                int type = rs.getInt("type");
                UserInfo userInfo = new UserInfo(id, uname, upass, realname, sex, age, tel, addr, type);
                list.add(userInfo);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int userAdd(UserInfo info) {
        String sql = "insert into userInfo(uname,upass,realname,sex,age,tel,addr,type) values (?,?,?,?,?,?,?,2)";
        Object[] obj = {info.getUname(),info.getUpass(),info.getRealname(),info.getSex(),info.getAge(),info.getTel(),info.getAddr()};
        return DBUtil.update(sql, obj);
    }

    public int userDel(int id) {
        String sql = "delete from userInfo where id = ?";
        Object[] obj = {id};
        return DBUtil.update(sql, obj);
    }

    public UserInfo userShowOne(int id) {
        UserInfo userInfo = null;
        String sql = "select * from userInfo where id = ? and type = 2";
        Object[] obj = {id};
        ResultSet rs = DBUtil.select(sql, obj);
        try {
            if(rs.next()){
                String uname = rs.getString("uname");
                String upass = rs.getString("upass");
                String realname = rs.getString("realname");
                String sex = rs.getString("sex");
                int age = rs.getInt("age");
                String tel = rs.getString("tel");
                String addr = rs.getString("addr");
                int type = rs.getInt("type");
                userInfo = new UserInfo(id, uname, upass, realname, sex, age, tel, addr, type);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userInfo;
    }

    public int userUpd(UserInfo info) {
        String sql = "update userInfo set uname=?,upass=?,realname=?,sex=?,age=?,tel=?,addr=? where id=?";
        Object[] obj = {info.getUname(),info.getUpass(),info.getRealname(),info.getSex(),info.getAge(),info.getTel(),info.getAddr(),info.getId()};
        return DBUtil.update(sql, obj);
    }

    public boolean userPass(String upass) {
        String sql = "select * from userInfo where upass=? and type=1";
        Object[] obj = {upass};
        ResultSet rs = DBUtil.select(sql, obj);
        try {
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public int userPassUpd(int id,String newPassword) {
        String sql = "update userInfo set upass=? where id=?";
        Object[] obj = {newPassword,id};
        return DBUtil.update(sql, obj);
    }

}
View Code

    (3) 页面展现

      a、登录页面

      技术分享图片

       b、用户管理页面

      技术分享图片

       c、密码修改页面

      技术分享图片

  三、遇到的问题     

     (1)密码修改

  四、解决的办法

      (1)当输入框失去焦点时,使用ajax判断

      具体实现:

技术分享图片
<body>
<!--头部-->
    <header class="publicHeader">
        <h1>客车售票管理系统</h1>
        <div class="publicHeaderR">
            <p><span>下午好!</span><span style="color: #fff21b">${userInfo.realname}</span> , 欢迎你!</p>
            <a href="login.html">退出</a>
        </div>
    </header>
<!--时间-->
    <section class="publicTime">
        <span id="time">2015年1月1日 11:11  星期一</span>
        <a href="#">温馨提示:为了能正常浏览,请使用高版本浏览器!(IE10+)</a>
    </section>
<!--主体内容-->
    <section class="publicMian ">
        <div class="left">
            <h2 class="leftH2"><span class="span1"></span>功能列表 <span></span></h2>
            <nav>
                <ul class="list">
                    <li ><a href="../newsInfoServlet?action=showAll">公告管理</a></li>
                    <li><a href="../messageServlet?action=showAll">留言管理</a></li>
                    <li><a href="../userServlet?action=showAll">用户管理</a></li>
                    <li id="active"><a href="password.jsp">密码修改</a></li>
                    <li><a href="login.jsp">退出系统</a></li>
                </ul>
            </nav>
        </div>
        <div class="right">
            <div class="location">
                <strong>你现在所在的位置是:</strong>
                <span>密码修改页面</span>
            </div>
            <div class="providerAdd">
                <form action="../userServlet?action=userPassUpd" method="post" onsubmit="return userPassUpd()">
                    <!--div的class 为error是验证错误,ok是验证成功-->
                    <input type="hidden" value="${userInfo.id }" name="id">
                    <div class="">
                        <label for="oldPassword">旧密码:</label>
                        <input type="password" name="oldPassword" placeholder="请输入旧密码" id="oldPassword" required/>
                        <span id="sp1"></span>
                    </div>
                    <div>
                        <label for="newPassword">新密码:</label>
                        <input type="password" name="newPassword" placeholder="请输入新密码" id="newPassword" required/>
                    </div>
                    <div>
                        <label for="reNewPassword">确认新密码:</label>
                        <input type="password" name="reNewPassword" placeholder="确认密码" id="reNewPassword" required/>
                         <span id="sp2"></span>
                    </div>
                    <div class="providerAddBtn">
                        <!--<a href="#">保存</a>-->
                        <input type="submit" style="height: 40px" value="保存" />
                    </div>
                </form>
            </div>
        </div>
    </section>
    <footer class="footer">
    </footer>
<script src="js/time.js"></script>
</body>
View Code
技术分享图片
/**
     *     密码修改
     */
    protected void userPassUpd(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id")); 
        String newPassword = req.getParameter("newPassword");
        int i = ud.userPassUpd(id,newPassword);
        if(i>0){
            req.getSession().removeAttribute("userInfo");
            resp.sendRedirect("afterpage/login.jsp");
        }
    }
    /**
     *    查询密码是否存在 
     */
    protected void userPass(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String upass = req.getParameter("oldPassword");
        boolean b = ud.userPass(upass);
        if(b){
            resp.getWriter().printf("true");
        }else{
            resp.getWriter().printf("false");
        }
    }
View Code
技术分享图片
public boolean userPass(String upass) {
        String sql = "select * from userInfo where upass=? and type=1";
        Object[] obj = {upass};
        ResultSet rs = DBUtil.select(sql, obj);
        try {
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public int userPassUpd(int id,String newPassword) {
        String sql = "update userInfo set upass=? where id=?";
        Object[] obj = {newPassword,id};
        return DBUtil.update(sql, obj);
    }
View Code

  五、项目燃尽图更新

  技术分享图片

 

 

 

客车网上售票系统02

原文:https://www.cnblogs.com/jdx-1/p/13328610.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!