package dao;
import bean.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class MySQLDAO implements
CustomerDAO, ManagerDAO, StationDAO, TicketPointDAO, TrainDAO, TrainOrderDAO, TrainScheduleDAO
{
@SuppressWarnings("WeakerAccess")
public ResultSet query(String sql){
return MySQLManager.getInstance().execute(sql).query();
}
@SuppressWarnings("WeakerAccess")
public int[] update(String sql){
return MySQLManager.getInstance().execute(sql).update();
}
@SuppressWarnings("WeakerAccess")
public ResultSet query(String sql, Object ... params){
if(params == null || params.length == 0) return query(sql);
return MySQLManager.getInstance().prepare(sql, params).query();
}
@SuppressWarnings("WeakerAccess")
public int[] update(String sql, Object ... params){
if(params == null || params.length == 0) return update(sql);
return MySQLManager.getInstance().prepare(sql, params).update();
}
private interface Parser<T> {
T parse(ResultSet res) throws SQLException;
}
private class Loader<T> {
private Parser<T> parser;
Loader(Parser<T> parser) {
this.parser = parser;
}
List<T> load(String sql, Object... params) {
List<T> rows = new ArrayList<>();
try {
try (ResultSet res = query(sql, params)) {
while (res.next()) {
rows.add(parser.parse(res));
}
}
return rows;
} catch (SQLException e) {
e.printStackTrace();
return rows;
}
}
T loadOne(String sql, Object... params) {
T row = null;
try {
try (ResultSet res = query(sql, params)) {
if (res.next()) {
row = parser.parse(res);
}
}
return row;
} catch (SQLException e) {
e.printStackTrace();
return row;
}
}
}
@Override
public void insertManager(Manager manager) {
update("insert into manager " +
"(point_id, username, password, name, sex, manager_type) values " +
"(?, ?, ?, ?, ?, ?)",
manager.getPointId(),
manager.getUsername(),
manager.getPassword(),
manager.getName(),
manager.getSex(),
manager.getManagerType()
);
}
@Override
public void deleteManager(Manager manager) {
update("delete from manager where manager_id = ?", manager.getManagerId());
}
@Override
public void updateManager(Manager manager) {
update("update manager set point_id = ?, username = ?, password = ?, name = ?, sex = ?, manager_type = ?" +
" where manager_id = ?",
manager.getPointId(), manager.getUsername(), manager.getPassword(), manager.getName(), manager.getSex(), manager.getManagerType(),
manager.getManagerId());
}
public void modifyPassword(Manager manager, String password) {
manager.setPassword(password);
updateManager(manager);
}
private Manager getManagerFromResult(ResultSet res) throws SQLException {
Manager manager = new Manager();
manager.setManagerId(res.getInt("manager_id"));
manager.setManagerType(res.getInt("manager_type"));
manager.setName(res.getString("name"));
manager.setPassword(res.getString("password"));
manager.setPointId(res.getInt("point_id"));
manager.setSex(res.getInt("sex"));
manager.setUsername(res.getString("username"));
return manager;
}
@Override
public Manager getManagerById(String manager_id) {
return new Loader<>(this::getManagerFromResult)
.loadOne("select * from manager where manager_id = ?", manager_id);
}
@Override
public List<Manager> getAllManagers() {
return new Loader<>(this::getManagerFromResult)
.load("select * from manager");
}
@Override
public List<Manager> searchManagers(String key) {
key = '%' + key + '%';
return new Loader<>(this::getManagerFromResult)
.load("select * from manager where manager_id = ? or name like ? or username like ?" +
" or point_id = ?",
key, key, key, key);
}
@SuppressWarnings("WeakerAccess")
public Manager getManagerByUsername(String username) {
return new Loader<>(this::getManagerFromResult)
.loadOne("select * from manager where username = ?", username);
}
@Override
public Manager login(String username, String password) {
Manager manager = getManagerByUsername(username);
if(manager != null && manager.getPassword().equals(password)){
return manager;
}
return null;
}
@Override
public void insertCustomer(Customer customer) {
update("insert into customer " +
"(name, sex, id_num, tel, customer_type) values " +
"(?, ?, ?, ?, ?)",
customer.getName(),
customer.getSex(),
customer.getIdNum(),
customer.getTel(),
customer.getCustomerType()
);
}
@Override
public void deleteCustomer(Customer customer) {
update("delete from customer where id_num = ?", customer.getIdNum());
}
@Override
public void updateCustomer(Customer customer) {
update("update customer set name = ?, sex = ?, tel = ?, customer_type = ?" +
" where id_num = ?",
customer.getName(), customer.getSex(), customer.getTel(), customer.getCustomerType(),
customer.getIdNum());
}
@Override
public void upsertCustomer(Customer customer) {
Customer c = getCustomerByIdNum(customer.getIdNum());
if (c == null) {
insertCustomer(customer);
} else {
updateCustomer(customer);
}
}
private Customer getCustomerFromResult(ResultSet res) throws SQLException {
Customer customer = new Customer();
customer.setIdNum(res.getString("id_num"));
customer.setName(res.getString("name"));
customer.setTel(res.getString("tel"));
customer.setSex(res.getInt("sex"));
customer.setCustomerType(res.getInt("customer_type"));
return customer;
}
@Override
public Customer getCustomerByIdNum(String id_num) {
return new Loader<>(this::getCustomerFromResult)
.loadOne("select * from customer where id_num = ?", id_num);
}
@Override
public List<Customer> searchCustomers(String key) {
key = '%' + key + '%';
return new Loader<>(this::getCustomerFromResult)
.load("select * from customer where id_num like ? or name like ? or tel = ?",
key, key, key);
}
@Override
public List<Customer> getAllCustomers() {
return new Loader<>(this::getCustomerFromResult)
.load("select * from customer");
}
public int getSexFromIdNum(String idNum) {
if (idNum.length() == 18) {
return (idNum.charAt(16) - '0') % 2;
}
return 0;
}
@Override
public void insertStation(Station station) {
update("insert into station " +
"(station_name) values " +
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JAVA GUI火车票管理系统(JAVA+MySQL) (161个子文件)
MySQLDAO.class 27KB
OrderPanel$ScheduleItem$OrderDialog.class 10KB
XPanel.class 9KB
TrainOrderPanel.class 8KB
OrderPanel.class 8KB
OrderPanel$ScheduleItem.class 7KB
TrainPanel.class 7KB
TrainOrderPanel$3.class 7KB
XDialog.class 6KB
ServerConnectSocketThread.class 6KB
TrainOrderPanel$2.class 5KB
LoginFrame.class 5KB
ModelPanel.class 5KB
TrainSchedule.class 5KB
TrainOrder.class 4KB
MySQLManager$StatementBuilder.class 4KB
OrderPanel$TrainInfoItem.class 4KB
ModifyPasswordPanel.class 4KB
CustomerPanel.class 4KB
ManagerPanel.class 4KB
TrainSchedulePanel.class 4KB
SellPointPanel.class 4KB
TrainSchedulePanel$3.class 3KB
WidgetUtils.class 3KB
Train.class 3KB
ManagerPanel$3.class 3KB
TrainPanel$3.class 3KB
TrainSchedulePanel$2.class 3KB
ManagerFrame.class 3KB
TrainTicketClient.class 3KB
TrainPanel$2.class 3KB
MySQLManagerTest.class 3KB
MySQLManager.class 3KB
ManagerPanel$2.class 3KB
CustomerPanel$3.class 3KB
MySQLDAO$Loader.class 2KB
HistoryPanel$1.class 2KB
SellPointPanel$3.class 2KB
CustomerPanel$2.class 2KB
OrderPanel$ScheduleItem$OrderDialog$1.class 2KB
Manager.class 2KB
SellPointPanel$2.class 2KB
XPasswordField.class 2KB
HistoryPanel.class 2KB
ListTableModel.class 2KB
XTextField.class 2KB
SellerFrame.class 2KB
TrainTicketServer.class 2KB
TrainOrderPanel$1.class 2KB
ModelPanel$1.class 2KB
Customer.class 2KB
SellerFrame$1.class 2KB
TrainSchedulePanel$1.class 2KB
Passby.class 2KB
OrderPanel$PayDialog.class 2KB
Constants$ColumnName.class 2KB
ManagerPanel$1.class 2KB
ManagerFrame$1.class 2KB
TrainPanel$3$1.class 1KB
SellPointPanel$1.class 1KB
Seat.class 1KB
CustomerPanel$1.class 1KB
TrainPanel$3$2.class 1KB
TrainPanel$1.class 1KB
SeatGroup.class 1KB
TicketPoint.class 1KB
MySQLManager$StatementState.class 1KB
ResultSetTableModel.class 1KB
Constants.class 1KB
XTable$MyCellRenderer.class 1KB
SeatAggregate.class 1KB
XPanel$1.class 1015B
LookUtils.class 972B
XPanel$3.class 951B
XPanel$2.class 944B
TrainOrderPanel$3$1.class 939B
TrainOrderPanel$2$1.class 939B
Sex.class 887B
TrainSchedule$Extra.class 855B
Station.class 835B
XTable.class 816B
WindowUtils.class 812B
TrainOrderDAO.class 777B
XPanel$ComponentScroll.class 705B
XDialog$1.class 631B
StringUtils.class 604B
TrainScheduleDAO.class 575B
ManagerDAO.class 560B
OrderPanel$SchedulesPanel.class 538B
TicketPointDAO.class 535B
CustomerDAO.class 529B
TrainDAO.class 497B
XPanel$MeasurableItem.class 488B
MySQLDAO$Parser.class 374B
StationDAO.class 315B
Measurable.class 150B
.classpath 1KB
mysql-connector-java-8.0.13.jar 2.03MB
swingx-1.6.1.jar 1.33MB
beautyeye_lnf.jar 524KB
共 161 条
- 1
- 2
资源评论
守得云开现月明
- 粉丝: 5
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功