DruTool
package Mhl.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
* @author whlie(true){learn}
*/
public class DruTool {
private static DataSource ds;
private static String user;
private static String password;
private static String url;
private static String driver;
static {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("src\\druid.properties"));
user = properties.getProperty("user");
password = properties.getProperty("password");
url = properties.getProperty("url");
driver = properties.getProperty("driver");
ds = (DataSource) DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void close(ResultSet resultSet, Statement statement, Connection connection) throws SQLException {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static String getUser() {
return user;
}
public static String getPassword() {
return password;
}
public static String getUrl() {
return url;
}
public static String getDriver() {
return driver;
}
}
Utility
package Mhl.utils;
import java.util.Scanner;
@SuppressWarnings({"all"})
/**
工具类:处理各种情况的用户输入,并且能够按照程序员的需求,得到用户的控制台输入。
*/
public class Utility {
//静态属性。。。
private static Scanner scanner = new Scanner(System.in);
/**
* 功能:读取键盘输入的确认选项,Y或N
* 将小的功能,封装到一个方法中.
* @return Y或N
*/
public static char readConfirmSelection() {
System.out.print("是否确定退出(Y/N)");
char c;
for (; ; ) {//无限循环
//在这里,将接受到字符,转成了大写字母
//y => Y n=>N
//从键盘上读取内容 将小写字母转成了大写字母
String str = readKeyBoard().toUpperCase();
c = str.charAt(0); //返回str中的第一个字母
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c; //返回c的值
}
/**
* 功能: 读取一个字符串,判断字符串的长度
* @param blankReturn 如果为true ,表示 可以读空字符串。
* @return
*/
private static String readKeyBoard() {
//定义了字符串
String line = "";
//scanner.hasNextLine() 判断用户输入的数据有没有下一行
while (scanner.hasNextLine()) { //判断用户输入的数据有没有下一行,读取键盘上输入的全部内容
line = scanner.nextLine();//把从键盘上输入的字符付给Line
//如果line.length=0, 即用户没有输入任何内容,直接回车
if (line.length() == 0) {
return line;//直接可以返回空串
}
break;
}
return line;
}
}
Mysql
#员工表
CREATE TABLE employee(
id int PRIMARY KEY AUTO_INCREMENT,
empId VARCHAR(50) UNIQUE NOT NULL DEFAULT'',
pwd char(32) NOT NULL DEFAULT'',
`name` VARCHAR(32) NOT NULL DEFAULT'',
job VARCHAR(32) NOT NULL DEFAULT''
)CHARSET=utf8;
insert into employee VALUES(null,'01',MD5('123456'),'rick','经理');
insert into employee VALUES(null,'02',MD5('123456'),'morty','收银员');
insert into employee VALUES(null,'03',MD5('123456'),'jerry','服务员');
DROP TABLE employee
SELECT *from employee
#餐桌表
CREATE TABLE diningTable(
id int PRIMARY KEY AUTO_INCREMENT,
state VARCHAR(20) NOT NULL DEFAULT'',
ordername char(32) NOT NULL DEFAULT'',
ordertel VARCHAR(32) NOT NULL DEFAULT''
)CHARSET=utf8;
insert into diningTable VALUES(null,'空','','');
insert into diningTable VALUES(null,'空','','');
insert into diningTable VALUES(null,'空','','');
DROP TABLE diningTable
select id,state from diningTable
#菜单表
CREATE TABLE Menu(
id int PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL DEFAULT'',
type char(32) NOT NULL DEFAULT'',
price double not null DEFAULT 0
)CHARSET=utf8;
insert into Menu VALUES(null,'北京烤鸭','主食',300);
insert into Menu VALUES(null,'兰州拉面','主食',20);
insert into Menu VALUES(null,'热干面','主食',20);
insert into Menu VALUES(null,'串串香','小吃',20);
insert into Menu VALUES(null,'冰激凌','甜食',20);
insert into Menu VALUES(null,'鸡蛋汤','汤类',20);
SELECT *from Menu
#账单表
CREATE TABLE bill(
id int PRIMARY KEY AUTO_INCREMENT,
billId VARCHAR(50) NOT NULL DEFAULT'',
menuId int not null DEFAULT 0,
nums int not null DEFAULT 0,
price double not null DEFAULT 0,
diningTable int not null DEFAULT 0,
billDate datetime not null,
state VARCHAR(50) NOT NULL DEFAULT''
)CHARSET=utf8;
select *from bill
dao包下的每一个文件对应数据库中一张表
Bill
package Mhl.daomain;
import java.util.Date;
/**
* @author whlie(true){learn}
*/
public class Bill {
private Integer id;
private String billId;
private Integer menuId;
private Integer nums;
private Double price;
private Integer diningTable;
private Date billDate;
private String state;
public Bill() {
}
public Bill(Integer id, String billId, Integer menuId, Integer nums, Double price, Integer diningTable, Date billDate, String state) {
this.id = id;
this.billId = billId;
this.menuId = menuId;
this.nums = nums;
this.price = price;
this.diningTable = diningTable;
this.billDate = billDate;
this.state = state;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBillId() {
return billId;
}
public void setBillId(String billId) {
this.billId = billId;
}
public Integer getMenuId() {
return menuId;
}
public void setMenuId(Integer menuId) {
this.menuId = menuId;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Integer getDiningTable() {
return diningTable;
}
public void setDiningTable(Integer diningTable) {
this.diningTable = diningTable;
}
public Date getBillDate() {
return billDate;
}
public void setBillDate(Date billDate) {
this.billDate = billDate;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Integer getNums() {
return nums;
}
public void setNums(Integer nums) {
this.nums = nums;
}
@Override
public String toString() {
return id + "\t\t" +
menuId + "\t\t\t" +
nums+"\t\t\t"+
price + "\t\t" +
diningTable + "\t\t" +
billDate + "\t\t" +
state;
}
}
DiningTable
private Integer id;
private String state;
private String name;
private String phone;
Menu
private Integer id;
private String name;
private String type;
private double price;
Employee
private Integer id;
private String empId;
private String pwd;
private String name;
private String job;
Multiple
private Integer id;
private String billId;
private Integer menuId;
private Integer nums;
private Double price;
private Integer diningTable;
private Date billDate;
private String state;
//增加一个来自Menu表的列
private String name;