为了账号安全,请及时绑定邮箱和手机立即绑定

javaweb版本的答答租车系统 (源代码一)

标签:
Java

这是前面发表的javaweb版本答答租车系统的源代码,因为发表文章有字数限制以及还有功能没扩展出来,因此这里只是一部分,后面还会有文章继续发表的
好了,首先javaweb项目是需要操作数据库的,因此需要建立对应的表来存放对应的数据,这里是四个表分别对应保存不同的数据(因为是源代码,所以直接是建表SQL语句了)
这是car_user表用来存放用户信息的

CREATE TABLE car_user (
id INT(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
username varchar(50) NOT NULL,
password varchar(50) NOT NULL,
sex varchar(10) NOT NULL,
tel varchar(50) NOT NULL,
email varchar(50) NOT NULL,
competence varchar(100) NOT NULL
)

这是car_commodity表用来存放商品信息的

CREATE TABLE car_commodity(
id INT(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(50) NOT NULL,
rent int(50) NOT NULL,
manned int(50) NOT NULL,
cargo int(50) NOT NULL,
owner varchar(100) NOT NULL
)

这是car_bill表用来存放用户账单的

CREATE TABLE car_bill(
id INT(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid int(10) NOT NULL,
carid int(10) NOT NULL,
quantity int(50) NOT NULL,
totalrent double(100) NOT NULL,
carstatus varchar(100) NOT NULL,
ordernumber varchar(100) NOT NULL,
cartime varchar(100) NOT NULL
)

这是car_account 表用来存放用户账户信息的

CREATE TABLE car_account (
id INT(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid int(10) not null,
money varchar(30) not null,
let_frequency int(20) not null,
out_frequency int(20) not null
)

数据库之后就是建立web项目CarSystem了,在项目中创建com.david包用来存放4个数据库对应的4个实体类,为了解决程序乱码问题这里创建com.david.code包用来存放CharacterEncodingFilter.java字符编码过滤器,创建com.david.dao包用来存放4个实体类对应的数据库操作类以及连接数据库文件ConnectSJK.java,最后创建com.david.servlet用来存放所有的Servlet。
这是用户实体类User

package com.david;
/**
 * 用户实体类
 * @author David
 */
public class User {
    private Integer id;             //用户id
    private String username;        //用户名
    private String password;        //密码
    private String sex;             //性别
    private String tel;             //电话
    private String email;           //邮箱
    private String competence;      //权限
    private Account account;        //关联类

    public User(){
        super();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCompetence() {
        return competence;
    }

    public void setCompetence(String competence) {
        this.competence = competence;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password="
                + password + ", sex=" + sex + ", tel=" + tel + ", email="
                + email + ", competence=" + competence + ", account=" + account
                + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((account == null) ? 0 : account.hashCode());
        result = prime * result
                + ((competence == null) ? 0 : competence.hashCode());
        result = prime * result + ((email == null) ? 0 : email.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result
                + ((password == null) ? 0 : password.hashCode());
        result = prime * result + ((sex == null) ? 0 : sex.hashCode());
        result = prime * result + ((tel == null) ? 0 : tel.hashCode());
        result = prime * result
                + ((username == null) ? 0 : username.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (account == null) {
            if (other.account != null)
                return false;
        } else if (!account.equals(other.account))
            return false;
        if (competence == null) {
            if (other.competence != null)
                return false;
        } else if (!competence.equals(other.competence))
            return false;
        if (email == null) {
            if (other.email != null)
                return false;
        } else if (!email.equals(other.email))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (sex == null) {
            if (other.sex != null)
                return false;
        } else if (!sex.equals(other.sex))
            return false;
        if (tel == null) {
            if (other.tel != null)
                return false;
        } else if (!tel.equals(other.tel))
            return false;
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }
}

这是商品实体类Commodity

package com.david;
/**
 * 商品实体类
 * @author David
 */
public class Commodity {
    private Integer id;             //商品编号
    private String name;            //车名
    private Integer rent;           //租金
    private Integer manned;         //载人量
    private Integer cargo;          //载货量
    private String owner;           //车主

    public Commodity(){
        super();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getRent() {
        return rent;
    }

    public void setRent(Integer rent) {
        this.rent = rent;
    }

    public Integer getManned() {
        return manned;
    }

    public void setManned(Integer manned) {
        this.manned = manned;
    }

    public Integer getCargo() {
        return cargo;
    }

    public void setCargo(Integer cargo) {
        this.cargo = cargo;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    @Override
    public String toString() {
        return "Commodity [id=" + id + ", name=" + name + ", rent=" + rent
                + ", manned=" + manned + ", cargo=" + cargo + ", owner="
                + owner + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((cargo == null) ? 0 : cargo.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((manned == null) ? 0 : manned.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((owner == null) ? 0 : owner.hashCode());
        result = prime * result + ((rent == null) ? 0 : rent.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Commodity other = (Commodity) obj;
        if (cargo == null) {
            if (other.cargo != null)
                return false;
        } else if (!cargo.equals(other.cargo))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (manned == null) {
            if (other.manned != null)
                return false;
        } else if (!manned.equals(other.manned))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (owner == null) {
            if (other.owner != null)
                return false;
        } else if (!owner.equals(other.owner))
            return false;
        if (rent == null) {
            if (other.rent != null)
                return false;
        } else if (!rent.equals(other.rent))
            return false;
        return true;
    }
}

这是账单实体类Biil

package com.david;
/**
 * 账单实体类
 * @author David
 */
public class Bill { 
    private Integer id;             //序号
    private Integer userid;         //用户id
    private Integer carid;          //车id
    private Integer quantity;       //租车数量
    private Double totalrent;       //总租金
    private String carstatus;       //租车状态
    private String cartime;         //租车时间
    private String ordernumber;     //账单编号

    private User user;

    public Bill(){
        super();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public Integer getCarid() {
        return carid;
    }

    public void setCarid(Integer carid) {
        this.carid = carid;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public Double getTotalrent() {
        return totalrent;
    }

    public void setTotalrent(Double totalrent) {
        this.totalrent = totalrent;
    }

    public String getCarstatus() {
        return carstatus;
    }

    public void setCarstatus(String carstatus) {
        this.carstatus = carstatus;
    }

    public String getCartime() {
        return cartime;
    }

    public void setCartime(String cartime) {
        this.cartime = cartime;
    }

    public String getOrdernumber() {
        return ordernumber;
    }

    public void setOrdernumber(String ordernumber) {
        this.ordernumber = ordernumber;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "Bill [id=" + id + ", userid=" + userid + ", carid=" + carid
                + ", quantity=" + quantity + ", totalrent=" + totalrent
                + ", carstatus=" + carstatus + ", cartime=" + cartime
                + ", ordernumber=" + ordernumber + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((carid == null) ? 0 : carid.hashCode());
        result = prime * result
                + ((carstatus == null) ? 0 : carstatus.hashCode());
        result = prime * result + ((cartime == null) ? 0 : cartime.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result
                + ((ordernumber == null) ? 0 : ordernumber.hashCode());
        result = prime * result
                + ((quantity == null) ? 0 : quantity.hashCode());
        result = prime * result
                + ((totalrent == null) ? 0 : totalrent.hashCode());
        result = prime * result + ((userid == null) ? 0 : userid.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Bill other = (Bill) obj;
        if (carid == null) {
            if (other.carid != null)
                return false;
        } else if (!carid.equals(other.carid))
            return false;
        if (carstatus == null) {
            if (other.carstatus != null)
                return false;
        } else if (!carstatus.equals(other.carstatus))
            return false;
        if (cartime == null) {
            if (other.cartime != null)
                return false;
        } else if (!cartime.equals(other.cartime))
            return false;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (ordernumber == null) {
            if (other.ordernumber != null)
                return false;
        } else if (!ordernumber.equals(other.ordernumber))
            return false;
        if (quantity == null) {
            if (other.quantity != null)
                return false;
        } else if (!quantity.equals(other.quantity))
            return false;
        if (totalrent == null) {
            if (other.totalrent != null)
                return false;
        } else if (!totalrent.equals(other.totalrent))
            return false;
        if (userid == null) {
            if (other.userid != null)
                return false;
        } else if (!userid.equals(other.userid))
            return false;
        return true;
    }
}

这是账户实体类Account

package com.david;
/**
 * 账户实体类
 * @author David
 */
public class Account {
    private Integer id;                 //账户编号
    private Integer userid;             //用户id
    private Double money;               //账户余额
    private Integer let_frequency;      //租车次数
    private Integer out_frequency;      //出租次数

    public Account(){
        super();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserid() {
        return userid;
    }

    public void setUserid(Integer userid) {
        this.userid = userid;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    public Integer getLet_frequency() {
        return let_frequency;
    }

    public void setLet_frequency(Integer let_frequency) {
        this.let_frequency = let_frequency;
    }

    public Integer getOut_frequency() {
        return out_frequency;
    }

    public void setOut_frequency(Integer out_frequency) {
        this.out_frequency = out_frequency;
    }

    @Override
    public String toString() {
        return "Account [id=" + id + ", userid=" + userid + ", money=" + money
                + ", let_frequency=" + let_frequency + ", out_frequency="
                + out_frequency + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result
                + ((let_frequency == null) ? 0 : let_frequency.hashCode());
        result = prime * result + ((money == null) ? 0 : money.hashCode());
        result = prime * result
                + ((out_frequency == null) ? 0 : out_frequency.hashCode());
        result = prime * result + ((userid == null) ? 0 : userid.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Account other = (Account) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (let_frequency == null) {
            if (other.let_frequency != null)
                return false;
        } else if (!let_frequency.equals(other.let_frequency))
            return false;
        if (money == null) {
            if (other.money != null)
                return false;
        } else if (!money.equals(other.money))
            return false;
        if (out_frequency == null) {
            if (other.out_frequency != null)
                return false;
        } else if (!out_frequency.equals(other.out_frequency))
            return false;
        if (userid == null) {
            if (other.userid != null)
                return false;
        } else if (!userid.equals(other.userid))
            return false;
        return true;
    }

}

这是用户数据库操作类UserDao

package com.david.dao;

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

import com.david.User;

/**
 * 用户数据库操作类
 * @author David
 */
public class UserDao {
    /**
     * 新增用户
     * @param User 用户
     * @author David
     */
    public void saveUser(User user){
        //获取数据库连接Connection对象
        Connection conn = ConnectSJK.getConnection();
        String sql = "insert into car_user(username,password,sex,tel,email,competence) value (?,?,?,?,?,?)";
        try {
            //获取preparedstatement对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //对sql语句占位符参数进行动态赋值
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getSex());
            ps.setString(4, user.getTel());
            ps.setString(6, user.getEmail());
            ps.setString(7, user.getCompetence());
            //执行更新操作
            ps.executeUpdate();
            //释放此preparedstatement对象的数据库和JDBC资源
            ps.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally{
            //关闭数据库连接
            ConnectSJK.closeConnection(conn);
        }
    }

    /**
     * 用户登录
     * @param username 用户名
     * @param passwrod 密码
     * @author David
     * @return 用户对象
     */
    public User login(String username,String password){
        //实例化一个对象
        User user = new User();
        //获取数据库连接Connection对象
        Connection conn = ConnectSJK.getConnection();
        //根据用户名和密码查询用户信息
        String sql = "select * from car_user where username = ? and password = ?";
        try {
            //获取preparedStatement对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //对sql语句的占位符参数进行动态赋值
            ps.setString(1, username);
            ps.setString(2, password);
            //执行查询获取结果集
            ResultSet rs = ps.executeQuery();
            //判断结果集是否有效
            if(rs.next()){
                //对用户对象属性赋值
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setSex(rs.getString("sex"));
                user.setTel(rs.getString("tel"));
                user.setEmail(rs.getString("email"));
                user.setCompetence(rs.getString("competence"));
            }
            //释放此ResultSet对象的数据库和JDBC资源
            rs.close();
            //释放此preparedStatement对象的数据库和JDBC资源
            ps.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally{
            //关闭数据库连接
            ConnectSJK.closeConnection(conn);
        }
        return user;
    }

    /**
     * 判断用户在数据库中是否存在
     * @param username 用户名
     * @author David
     * @return 布尔值
     */
    public boolean userIsExsit(String username){
        //获取数据库连接Connection对象
        Connection conn = ConnectSJK.getConnection();
        //根据指定用户名查询用户信息
        String sql = "select * from car_user where username=?";
        try {
            //获取preparedstatement对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //对sql占位符参数进行动态赋值
            ps.setString(1, username);
            //执行查询获取结果集
            ResultSet rs = ps.executeQuery();
            //判断结果集是否有效
            if(!rs.next()){
                //如果无效则证明可用
                return true;
            }
            // 释放此 ResultSet 对象的数据库和 JDBC 资源
            rs.close();
            // 释放此 PreparedStatement 对象的数据库和 JDBC 资源
            ps.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally{
            //关闭数据库连接
            ConnectSJK.closeConnection(conn);
        }
        return false;
    }

    /**
     * 修改资料
     * @param user 用户
     * @author David
     */
    public void modifyUser(User user){
        //获取数据库连接Connection对象
        Connection conn = ConnectSJK.getConnection();
        //插入修改资料的sql语句
        String sql = "update car_user set sex=?,tel=?,email=? where username=?";
        try {
            //获取preparedstatement对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //对用户对象进行赋值
            ps.setString(1, user.getSex());
            ps.setString(2, user.getTel());
            ps.setString(3, user.getEmail());
            ps.setString(4, user.getUsername());
            //执行更新操作
            ps.executeUpdate();
            //释放此preparedstatement对象的数据库和JDBC资源
            ps.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally{
            //关闭数据库连接
            ConnectSJK.closeConnection(conn);
        }
    }
    /**
     * 根据用户名查id
     *  @param username 用户名
     * @author David
     * @return 整数id
     */
    public int userid(String username){
        //定义变量保存用户id
        int userid = 0;
        //获取数据库连接Connection对象
        Connection conn = ConnectSJK.getConnection();
        //根据用户名查询用户id
        String sql = "select id from car_user where username=?";
        try {
            //获取preparedstatement对象
            PreparedStatement ps = conn.prepareStatement(sql);
            //对sql占位符参数进行动态赋值
            ps.setString(1, username);
            //执行查询获取结果集
            ResultSet rs = ps.executeQuery();
            //判断结果集是否有效
            if(rs.next()){
                //将结果赋值给变量userid
                userid = rs.getInt("id");
            }
            // 释放此 ResultSet 对象的数据库和 JDBC 资源
            rs.close();
            // 释放此 PreparedStatement 对象的数据库和 JDBC 资源
            ps.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally{
            //关闭数据库连接
            ConnectSJK.closeConnection(conn);
        }
        return userid;
    }
}

因为有2W字数限制,后面的会在另一篇文章中继续更新的!

点击查看更多内容
19人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
87
获赞与收藏
633

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消