一共调用了4个外界类库,
分别为:
mysql-connector-java-5.1.37
commons-dbutils-1.6
commons-dbcp-1.4
commons-pool-1.5.6
具体代码如下:
package com.ewarm.domain;
public class Car {
//JavaBean类
private int carid;
private String carname;
private int price;
private int takeman;
private int takecargo;
public Car() {}
public Car(int carid, String carname, int price, int takeman, int takecargo) {
super();
this.carid = carid;
this.carname = carname;
this.price = price;
this.takeman = takeman;
this.takecargo = takecargo;
}
public String toString() {
return "Car [carid=" + carid + ", carname=" + carname + ", price=" + price + ", takeman=" + takeman
+ ", takecargo=" + takecargo + "]";
}
public int getCarid() {
return carid;
}
public void setCarid(int carid) {
this.carid = carid;
}
public String getCarname() {
return carname;
}
public void setCarname(String carname) {
this.carname = carname;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getTakeman() {
return takeman;
}
public void setTakeman(int takeman) {
this.takeman = takeman;
}
public int getTakecargo() {
return takecargo;
}
public void setTakecargo(int takecargo) {
this.takecargo = takecargo;
}
}
package com.ewarm.tools;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class JDBCUtil {
/*
* 自制工具类
* 方便调用数据库连接池
*/
private static BasicDataSource datasource = new BasicDataSource();
private JDBCUtil(){}
static{
//初始化连接池设置
datasource.setDriverClassName("com.mysql.jdbc.Driver");
datasource.setUrl("jdbc:mysql://localhost:3306/ddzc");
datasource.setUsername("root");
datasource.setPassword("123");
}
//静态方法供外界调用
public static DataSource getBasicDataSource(){
return datasource;
}
}
package com.ewarm.app;
import com.ewarm.view.MainView;
public class DoMain {
/*
* 主方法入口
*/
public static void main(String[] args) {
new MainView().run();
}
}
package com.ewarm.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.ewarm.domain.Car;
import com.ewarm.tools.JDBCUtil;
public class Dao {
/*
* 数据层
*/
//声明使用连接池链接数据库的的结果集 qr
private QueryRunner qr = new QueryRunner(JDBCUtil.getBasicDataSource());
//查询数据库所有数据
public List<Car> selectAll() {
try{
String sql = "SELECT * FROM car";
//得到数据库每条数据封装到JavaBean对象中,再将这些对象封装到List集合中
List<Car> list = qr.query(sql, new BeanListHandler<>(Car.class));
return list;
}catch(SQLException ex){
System.out.println(ex);
throw new RuntimeException("数据查询异常");
}
}
//构建字符串,保证sql语句的完整性
public StringBuffer getStringBuffer(int[] arr){
StringBuffer sfd = new StringBuffer();
for(int i=0 ; i<arr.length ; i++){
if(i==(arr.length-1)){
sfd.append(arr[i]);
}else{
sfd.append(arr[i]+",");
}
}
return sfd;
}
//查询数据库指定编号的车辆的总金额
public Object selectCar_price(int[] arr) {
try{
StringBuffer sfd = getStringBuffer(arr);
String sql = "SELECT SUM(price) FROM car WHERE carid IN("+sfd+")";
//得到数据库的单数据
Object obj= qr.query(sql, new ScalarHandler<Object>());
return obj;
}catch(SQLException ex){
System.out.println(ex);
throw new RuntimeException("数据查询异常");
}
}
//查询数据库指定编号的车辆的总共载客数
public Object selectCar_takeman(int[] arr) {
try{
StringBuffer sfd = getStringBuffer(arr);
String sql = "SELECT SUM(takeman) FROM car WHERE carid IN("+sfd+")";
//得到数据库的单数据
Object obj = qr.query(sql, new ScalarHandler<Object>());
return obj;
}catch(SQLException ex){
System.out.println(ex);
throw new RuntimeException("数据查询异常");
}
}
//查询数据库指定编号的车辆的总共载货数
public Object selectCar_takecargo(int[] arr) {
try{
StringBuffer sfd = getStringBuffer(arr);
String sql = "SELECT SUM(takecargo) FROM car WHERE carid IN("+sfd+")";
//得到数据库的单数据
Object obj = qr.query(sql, new ScalarHandler<Object>());
return obj;
}catch(SQLException ex){
System.out.println(ex);
throw new RuntimeException("数据查询异常");
}
}
//查询数据库指定编号的客车总数 ,只要能载客就列入
public List<Car> selectCar_takemanCar(int[] arr) {
try{
StringBuffer sfd = getStringBuffer(arr);
String sql = "SELECT carname FROM car WHERE carid IN("+sfd+") AND takeman<>0";
//得到数据库每条数据封装到JavaBean对象中,再将这些对象封装到List集合中
List<Car> list = qr.query(sql, new BeanListHandler<>(Car.class));
return list;
}catch(SQLException ex){
System.out.println(ex);
throw new RuntimeException("数据查询异常");
}
}
//查询数据库指定编号的货车总数,只能载货物
public List<Car> selectCar_takecargoCar(int[] arr) {
try{
StringBuffer sfd = getStringBuffer(arr);
String sql = "SELECT carname FROM car WHERE carid IN("+sfd+") AND takeman=0";
//得到数据库每条数据封装到JavaBean对象中,再将这些对象封装到List集合中
List<Car> list = qr.query(sql, new BeanListHandler<>(Car.class));
return list;
}catch(SQLException ex){
System.out.println(ex);
throw new RuntimeException("数据查询异常");
}
}
}
package com.ewarm.view;
import java.util.List;
import java.util.Scanner;
import com.ewarm.dao.Dao;
import com.ewarm.domain.Car;
public class MainView {
/*
* 视图层
*/
private Dao dao = new Dao();
public void run(){
//创建键盘录入对象
Scanner sc = new Scanner(System.in);
while(true){
//主界面
System.out.println("欢迎使用哒哒租车系统:");
System.out.println("您是否要租车:1是 0否");
int choose = sc.nextInt();
switch (choose) {
case 1:
//进入租车系统
rentCar();
break;
case 0:
//退出系统
System.out.println("谢谢使用");
sc.close();
System.exit(0);
break;
default:
break;
}
}
}
public void rentCar(){
//显示可租车的类型及其价目表
showCar();
//选择租车型号与天数
chooseCar();
}
private void chooseCar() {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("请输入要租的车的数量:");
int count = sc.nextInt();
int[] arr = new int[count];
//获取选择车辆数组id;
for(int i=0;i<count;i++){
System.out.println("请输入第"+(i+1)+"辆车的序号:");
arr[i] = sc.nextInt();
}
//查询得到金钱总数的返回值
Object arr_price = dao.selectCar_price(arr);
//查询得到载人总数的返回值
Object arr_takeman = dao.selectCar_takeman(arr);
//查询得到载货总数的返回值
Object arr_takecargo = dao.selectCar_takecargo(arr);
//查询得到载客类车种集合
List<Car> list1 = dao.selectCar_takemanCar(arr);
//获得结果字符串
StringBuffer sbf1 = new StringBuffer();
for(Car car : list1){
if(car.getCarname()==null)
sbf1.append("无该类车辆");
sbf1.append(car.getCarname()+"\t");
}
//查询得到载货类车种集合
List<Car> list2 = dao.selectCar_takecargoCar(arr);
//获得结果字符串
StringBuffer sbf2 = new StringBuffer();
for(Car car : list2){
if(car.getCarname()==null)
sbf1.append("无该类车辆");
sbf2.append(car.getCarname()+"\t");
}
//总单价*租车天数得到总金额
System.out.println("请输入租车天数:");
int sentday = sc.nextInt();
int i = Integer.parseInt(arr_price.toString());
int allprice = sentday*i;
System.out.println("***账单***");
System.out.println("您选择的载客类车种为:"+sbf1);
System.out.println("您选择的载货类车种为:"+sbf2);
System.out.println("共载人:"+arr_takeman+"人");
System.out.println("***共载货物:"+arr_takecargo+"吨");
System.out.println("***租车总价格为:"+allprice);
//System.exit(0);
}
public void showCar(){
//调用dao类方法 查询数据库
List<Car> list = dao.selectAll();
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t\t容量");
//遍历查询数据库得到的集合list
for(Car car : list){
if(car.getTakecargo()==0){
System.out.println(car.getCarid()+"\t"+car.getCarname()+"\t"+car.getPrice()+"元/天\t\t载人:"+car.getTakeman()+"人");
}else if(car.getTakeman()==0){
System.out.println(car.getCarid()+"\t"+car.getCarname()+"\t"+car.getPrice()+"元/天\t\t载货:"+car.getTakecargo()+"吨");
}else{
System.out.println(car.getCarid()+"\t"+car.getCarname()+"\t"+car.getPrice()+"元/天\t\t载人:"+car.getTakeman()+"人 "+car.getTakecargo()+"吨");
}
}
}
}
数据库初始化语句如下:
CREATE DATABASE ddzc;
USE ddzc;
CREATE TABLE car(
carid INT PRIMARY KEY AUTO_INCREMENT,
carname VARCHAR(10),
price INT,
takeman INT,
takecargo INT
);
SELECT * FROM car;
INSERT INTO car(carname,price,takeman,takecargo) VALUE('奥迪A4',500,4,0);
INSERT INTO car(carname,price,takeman,takecargo) VALUE('马自达6',400,4,0);
INSERT INTO car(carname,price,takeman,takecargo) VALUE('皮卡雪6',450,4,2);
INSERT INTO car(carname,price,takeman,takecargo) VALUE('金龙',800,20,0);
INSERT INTO car(carname,price,takeman,takecargo) VALUE('松花江',400,0,4);
INSERT INTO car(carname,price,takeman,takecargo) VALUE('依维柯',1000,0,20);
具体控制台如下: