这是前面发表的javaweb版本答答租车系统的源代码这是前面发表的javaweb版本答答租车系统的源代码String sql = "insert into car_bill(userid,carid,quantity,totalrent,carstatus,ordernumber,cartime) value (?,?,?,?,?,?,?)";
try {
//获取preparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//对sql语句的占位符参数进行动态赋值
ps.setInt(1, bill.getUserid());
ps.setInt(2, bill.getCarid());
ps.setInt(3, bill.getQuantity());
ps.setDouble(4, bill.getTotalrent());
ps.setString(5, bill.getCarstatus());
ps.setString(7, bill.getOrdernumber());
ps.setString(6, bill.getCartime());
//执行更新操作
ps.executeUpdate();
// 释放此 PreparedStatement 对象的数据库和 JDBC 资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库连接
ConnectSJK.closeConnection(conn);
}
}
/**
- 查询账单
- @author David
- @return 查询到的账单集合
/
public List<Bill> bill(){
//定义集合保存查询到的商品
List<Bill> billlist = new ArrayList<Bill>();
//获取数据库连接Connection对象
Connection conn = ConnectSJK.getConnection();
//查询所有账单信息
String sql = "select from car_bill";
try {
//获取preparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//执行查询获取结果集
ResultSet rs = ps.executeQuery();
//循环判断结果集是否有效
while(rs.next()){
//实例化一个账单对象
Bill bill = new Bill();
//对账单对象进行赋值
bill.setCarid(rs.getInt("carid"));
bill.setQuantity(rs.getInt("quantity"));
bill.setTotalrent(rs.getDouble("totalrent"));
bill.setCarstatus(rs.getString("carstatus"));
bill.setOrdernumber(rs.getString("ordernumber"));
bill.setCartime(rs.getString("cartime"));
//将结果添加到集合中
billlist.add(bill);
}
// 释放此 ResultSet 对象的数据库和 JDBC 资源
rs.close();
// 释放此 PreparedStatement 对象的数据库和 JDBC 资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库连接
ConnectSJK.closeConnection(conn);
}
return billlist;
}
/** - 结账
- @param bill 账单对象
- @param ordernumber 账单编号
- @author David
-
@return 查询到的租金集合
*/
public List<Bill> billplease(int userid, String carstatus){
//定义集合保存查询到的结账信息
List<Bill> billlist = new ArrayList<Bill>();
//连接数据库获取Connection对象
Connection conn = ConnectSJK.getConnection();
//插入查询需要结账的账单信息的SQL语句
String sql = "select totalrent from car_bill where userid=? and carstatus=?";
try {
//获取preparedstatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//对sql语句的占位符参数进行动态赋值
ps.setInt(1, userid);
ps.setString(2, carstatus);
//执行查询获取结果集
ResultSet rs = ps.executeQuery();
//循环判断结果集是否有效
while(rs.next()){
//实例化一个账单对象
Bill bill = new Bill();
//对账单对象进行赋值
bill.setTotalrent(rs.getDouble("totalrent"));
//将结果添加到集合中
billlist.add(bill);
}
// 释放此 ResultSet 对象的数据库和 JDBC 资源
rs.close();
// 释放此 PreparedStatement 对象的数据库和 JDBC 资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库连接
ConnectSJK.closeConnection(conn);
}
return billlist;
}/**
- 根据编号查账单
- @param ordernumber 账单编号
- @author David
- @return 账单对象
/
public Bill idBill(String ordernumber){
//实例化一个用户对象
Bill bill = new Bill();
//获取数据库连接Connection对象
Connection conn = ConnectSJK.getConnection();
//插入查询用户的sql语句
String sql = "select from tb_user where ordernumber=?";
try {
//获取preparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 对SQL语句的占位符参数进行动态赋 值
ps.setString(1, ordernumber);
//执行查询获取结果集
ResultSet rs = ps.executeQuery();
//判断结果集是否有效
if(rs.next()){
//对用户对象进行赋值
bill.setCarid(rs.getInt("carid"));
bill.setQuantity(rs.getInt("quantity"));
bill.setTotalrent(rs.getDouble("totalrent"));
bill.setCarstatus(rs.getString("carstatus"));
bill.setCartime(rs.getString("cartime"));
}
// 释放此 ResultSet 对象的数据库和 JDBC 资源
rs.close();
// 释放此 PreparedStatement 对象的数据库和 JDBC 资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
// 关闭数据库连接
ConnectSJK.closeConnection(conn);
}
return bill;
}
/** -
根据用户ID查个人账单
/
public List<Bill> personalbill(Integer userid){
//实例化一个账单id
List<Bill> billlist = new ArrayList<Bill>();
//获取Connection对象
Connection conn = ConnectSJK.getConnection();
//插入查询账单的sql语句
String sql = "select from car_bill where userid=?";
try {
//获取preparedstatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//对SQL占位符参数进行动态赋值
ps.setInt(1, userid);
//执行查询获取结果集
ResultSet rs = ps.executeQuery();
//判断结果集是否有效
while(rs.next()){
Bill bill = new Bill();
//对用户对象进行赋值
bill.setCarid(rs.getInt("carid"));
bill.setQuantity(rs.getInt("quantity"));
bill.setTotalrent(rs.getDouble("totalrent"));
bill.setCarstatus(rs.getString("carstatus"));
bill.setOrdernumber(rs.getString("ordernumber"));
bill.setCartime(rs.getString("cartime"));
//将查询到的结果添加到集合中
billlist.add(bill);
}
// 释放此 ResultSet 对象的数据库和 JDBC 资源
rs.close();
// 释放此 PreparedStatement 对象的数据库和 JDBC 资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库连接
ConnectSJK.closeConnection(conn);
}
return billlist;
}/**
- 退租
- @param bill 账单对象
- @param ordernumber 账单编号
- @author David
*/
public void surrender(Bill bill,String ordernumber){
//获取数据库连接Connection对象
Connection conn = ConnectSJK.getConnection();
//插入修改租车状态的SQL语句
String sql = "update car_bill set carstatus=? where ordernumber=?";
try {
//获取preparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//对账单对象进行赋值
ps.setString(1, bill.getCarstatus());
ps.setString(2, ordernumber);
// 执行更新操作
ps.executeUpdate();
// 释放此 PreparedStatement 对象的数据库和 JDBC 资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库连接
ConnectSJK.closeConnection(conn);
}
}
}
这是账户数据库操作类AccountDao
package com.david.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.david.Account;
import com.david.User;
/**
* 账户数据库操作类
* @author David
*/
public class AccountDao {
/**
* 新增账户
* @param account 账户
* @author David
*/
public void saveAccount(Account account){
//获取数据库连接Connection对象
Connection conn = ConnectSJK.getConnection();
//插入新增账户的sql语句
String sql = "insert into car_account(username,money,let_frequency,out_frequency) value(?,?,?,?)";
try {
//获取preparedstatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//对sql占位符参数进行动态赋值
ps.setInt(1, account.getUserid());
ps.setDouble(4, account.getMoney());
ps.setInt(2, account.getLet_frequency());
ps.setInt(3, account.getOut_frequency());
//执行更新操作
ps.executeUpdate();
// 释放此 PreparedStatement 对象的数据库和 JDBC 资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库连接
ConnectSJK.closeConnection(conn);
}
}
/**
* 更新账户余额
* @param user 用户
* @author David
*/
public void updatemoney(User user,Account account){
//获取数据库连接Connection对象
Connection conn = ConnectSJK.getConnection();
//插入修改数据的sql语句
String sql = "update car_account set money=? where userid=?";
try {
//获取preparedstatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//对sql占位符参数进行动态赋值
ps.setDouble(1, account.getMoney());
ps.setInt(2, user.getId());
//执行更新操作
ps.executeUpdate();
// 释放此 PreparedStatement 对象的数据库和 JDBC 资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库连接
ConnectSJK.closeConnection(conn);
}
}
/**
* 更新租车次数
* @param user 用户
* @author David
*/
public void updatelet_frequency(User user , Account account){
//获取数据库连接Connection对象
Connection conn = ConnectSJK.getConnection();
//插入修改数据的sql语句
String sql = "update car_account set let_frequency=? where userid=?";
try {
//获取preparedstatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//对sql占位符参数进行动态赋值
ps.setInt(1, account.getLet_frequency());
ps.setInt(2, user.getId());
//执行更新操作
ps.executeUpdate();
// 释放此 PreparedStatement 对象的数据库和 JDBC 资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库连接
ConnectSJK.closeConnection(conn);
}
}
/**
* 更新出租次数
* @param user 用户
* @author David
*/
public void updateout_frequency(User user,Account account){
//获取数据库连接Connection对象
Connection conn = ConnectSJK.getConnection();
//插入修改数据的sql语句
String sql = "update car_account set out_frequency=? where userid=?";
try {
//获取preparedstatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//对sql占位符参数进行动态赋值
ps.setInt(1, account.getOut_frequency());
ps.setInt(2, user.getId());
//执行更新操作
ps.executeUpdate();
// 释放此 PreparedStatement 对象的数据库和 JDBC 资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库连接
ConnectSJK.closeConnection(conn);
}
}
/**
* 查询账户
* @author David
* @return 账户集合
*/
public List<Account> checkaccount(){
//定义集合保存查询到的账户信息
List<Account> accountlist = new ArrayList<Account>();
//获取数据库连接Connection对象
Connection conn = ConnectSJK.getConnection();
//查询账户列表
String sql = "select * from car_account";
try {
//获取preparedstatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//执行查询获取结果集
ResultSet rs = ps.executeQuery();
//循环判断结果集是否有效
while(rs.next()){
//实例化一个账户对象
Account account = new Account();
//对账户对象进行赋值
account.setId(rs.getInt("id"));
account.setUserid(rs.getInt("userid"));
account.setMoney(rs.getDouble("money"));
account.setLet_frequency(rs.getInt("let_frequency"));
account.setOut_frequency(rs.getInt("out_frequency"));
//将结果添加到集合中
accountlist.add(account);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库
ConnectSJK.closeConnection(conn);
}
return accountlist;
}
/**
* 根据用户id查账户信息
*/
public Account useridaccount(Integer userid){
//实例化一个账户对象
Account account = new Account();
//获取数据库连接Connection对象
Connection conn = ConnectSJK.getConnection();
//根据用户id查询账户
String sql = "select * from car_account where userid=?";
try {
//获取preparedstatement对象
PreparedStatement ps = conn.prepareStatement(sql);
//对slq语句占位符参数进行动态赋值
ps.setInt(1, userid);
//执行查询获取结果集
ResultSet rs = ps.executeQuery();
//判断结果集是否有效
if(rs.next()){
//对账户对象进行赋值
account.setId(rs.getInt("id"));
account.setUserid(rs.getInt("userid"));
account.setMoney(rs.getDouble("money"));
account.setLet_frequency(rs.getInt("let_frequency"));
account.setOut_frequency(rs.getInt("out_frequency"));
}
//释放此ResultSet对象的数据库和JDBC资源
rs.close();
//释放此preparedStatement对象的数据库和JDBC资源
ps.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally{
//关闭数据库
ConnectSJK.closeConnection(conn);
}
return account;
}
}
这是字符编码过滤器
package com.david.code;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* 字符编码过滤器CharacterEncodingFilter
* @author
*/
public class CharacterEncodingFilter implements Filter{
protected String encoding = null;
protected FilterConfig filterConfig = null;
public void init(FilterConfig filterConfig) throws ServletException{
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
public void destroy(){
this.encoding = null;
this.filterConfig = null;
}
热门评论
楼主有完整的代码和数据库么,我想用来参考和学习下
楼主可以发我下完整源码吗 因为我也在做出租车管理系统的项目,但目前毫无头绪,想参考下具体思路