汽车销售管理系统[Java课程设计]
程序员文章站
2022-04-11 12:57:41
...
汽车销售管理系统
文章目录
背景
Java基础课程设计
介绍
利用Java和MySQL语言,通过MVC模式和GUI编程,实现基于CS架构的汽车销售信息管理,实现增删改查功能
软件架构
Java原生MVC
环境
- 数据库:MySQL8.0
- 开发环境:JDK1.8
- 开发工具:MyEclipse
项目结构
数据库模型
项目详情
dao层
CarDao.java
汽车信息dao层
package com.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import com.entity.Car;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.utils.DBUtil;
public class CarDao {
// 添加表信息
public void addCar(Car car)throws Exception {
Connection con = (Connection) DBUtil.getConnection();
String sql="insert into car_info"
+"(car_id,model,color,manufactory,factory_date,price)"
+"values(?,?,?,?,?,?)";
PreparedStatement psmt = con.prepareStatement(sql);
psmt.setInt(1, car.getCar_id());
psmt.setString(2, car.getModel());
psmt.setString(3, car.getColor());
psmt.setString(4, car.getManufactory());
psmt.setString(5, car.getFactory_date());
psmt.setString(6, car.getPrice());
try {
psmt.executeUpdate();
JOptionPane.showMessageDialog(null, "数据插入成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
JOptionPane.showMessageDialog(null, "数据插入失败 ","tips",JOptionPane.PLAIN_MESSAGE);
}
con.close();
}
//删除汽车信息
public void delCar(int car_id ) throws SQLException {
Connection con=(Connection) DBUtil.getConnection();
String sql="" +
"DELETE FROM car_info "+
"WHERE car_id = ?";
// 预编译sql语句
PreparedStatement psmt = con.prepareStatement(sql);
psmt.setInt(1, car_id);
try {
psmt.execute();
JOptionPane.showMessageDialog(null, "数据删除成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据删除失败","tips",JOptionPane.PLAIN_MESSAGE);
}
}
// 修改Car信息
public void changeCar(Car car) throws SQLException {
// System.out.println(car.getCar_id()+car.getColor()+car.getModel()+car.getManufactory()+car.getFactory_date()+car.getPrice());
String model = car.getModel();
String color = car.getColor();
String manufactory = car.getManufactory();
String factory_date= car.getFactory_date();
String price = car.getPrice();
Integer car_id = car.getCar_id();
Connection con = (Connection) DBUtil.getConnection();
String sql = "update car_info "
+ "set model=\""+model+"\","
+ "color=\""+color+"\","
+"manufactory=\""+manufactory+"\","
+"factory_date=\""+factory_date+"\","
+ "price=\""+price+"\" "
+"where car_id ="+car_id+";";
Statement psmt = (Statement) con.createStatement();
try {
psmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "数据修改成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据修改失败","tips",JOptionPane.PLAIN_MESSAGE);
}
}
// 查询表信息
public List<Car> query() throws Exception{
Connection con=(Connection) DBUtil.getConnection();
Statement stmt=(Statement) con.createStatement();
ResultSet rs= stmt.executeQuery("select car_id,model,color,manufactory,"+
"factory_date,price from car_info");
List<Car> carList = new ArrayList<Car>();
Car car=null;
while (rs.next()) {
car = new Car();
car.setCar_id(Integer.parseInt(rs.getString("car_id")));
car.setModel(rs.getString("model"));
car.setColor(rs.getString("color"));
car.setManufactory(rs.getString("manufactory"));
car.setFactory_date(rs.getString("factory_date"));
car.setPrice(rs.getString("price"));
carList.add(car);
}
return carList;
}
}
GuestDao.java
客户信息dao层
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import com.entity.Guest;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import com.utils.DBUtil;
public class GuestDao {
// 增加客户信息
public void addGuest(Guest guest) throws Exception {
Connection con = (Connection) DBUtil.getConnection();
String sql = "insert into guest_info" + "(guest_name,contact_inf,address,business_re)" + "values(?,?,?,?)";
PreparedStatement psmt = (PreparedStatement) con.prepareStatement(sql);
psmt.setString(1, guest.getGuest_name());
psmt.setString(2, guest.getContact_information());
psmt.setString(3, guest.getAddress());
psmt.setString(4, guest.getBusiness_record());
try {
psmt.execute();
JOptionPane.showMessageDialog(null, "数据添加成功", "tips", JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据添加失败", "tips", JOptionPane.PLAIN_MESSAGE);
}
}
//删除客户信息
public void delGuest(String guest_name) throws SQLException {
Connection con = (Connection) DBUtil.getConnection();
String sql = "delete from guest_info where guest_name = ?";
PreparedStatement psmt = (PreparedStatement) con.prepareStatement(sql);
psmt.setString(1, guest_name);
try {
psmt.execute();
JOptionPane.showMessageDialog(null, "数据删除成功", "tips", JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据删除失败", "tips", JOptionPane.PLAIN_MESSAGE);
}
}
// 修改客户信息
public void changeGuest(Guest guest) throws SQLException {
String contact_inf = guest.getContact_information();
String address = guest.getAddress();
String business_re = guest.getBusiness_record();
String guest_name = guest.getGuest_name();
Connection con = (Connection) DBUtil.getConnection();
String sql = "update guest_info" + " set contact_inf=\"" + contact_inf + "\"," + "address=\"" + address + "\","
+ "business_re=\"" + business_re + "\"" + " where guest_name=\"" + guest_name + "\";";
Statement stm = (Statement) con.createStatement();
try {
stm.execute(sql);
JOptionPane.showMessageDialog(null, "数据修改成功", "tips", JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据修改失败", "tips", JOptionPane.PLAIN_MESSAGE);
}
}
// 查询表信息
public List<Guest> query() throws Exception {
Connection con = (Connection) DBUtil.getConnection();
Statement stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery("select guest_name,contact_inf,address,business_re" + " from guest_info");
List<Guest> carList = new ArrayList<Guest>();
Guest guest = null;
while (rs.next()) {
guest = new Guest();
guest.setGuest_name(rs.getString("guest_name"));
guest.setContact_information(rs.getString("contact_inf"));
guest.setAddress(rs.getString("address"));
guest.setBusiness_record(rs.getString("business_re"));
carList.add(guest);
}
return carList;
}
}
SaleDao.java
销售信息dao层
package com.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import com.entity.Sale;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.utils.DBUtil;
public class SaleDao {
//添加销售信息
public void addSale(Sale sale) throws Exception {
Connection con = (Connection) DBUtil.getConnection();
String sql="insert into sales_info"
+"(sale_date,car_type,color,number,handler)"
+"values(?,?,?,?,?)";
PreparedStatement psmt = con.prepareStatement(sql);
psmt.setString(1, sale.getSale_date());
psmt.setString(2, sale.getCar_type());
psmt.setString(3, sale.getColor());
psmt.setInt(4, sale.getNumber());
psmt.setString(5, sale.getHandler());
try {
psmt.execute();
JOptionPane.showMessageDialog(null, "数据添加成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据添加失败","tips",JOptionPane.PLAIN_MESSAGE);
}
}
//删除销售信息
public void delSale(String sale_date) throws SQLException{
Connection con=(Connection) DBUtil.getConnection();
String sql="" +
"DELETE FROM sales_info "+
"WHERE sale_date = ?";
// 预编译sql语句
PreparedStatement psmt = con.prepareStatement(sql);
psmt.setString(1, sale_date);
try {
psmt.execute();
JOptionPane.showMessageDialog(null, "数据删除成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据删除失败","tips",JOptionPane.PLAIN_MESSAGE);
}
}
//修改销售信息
public void changeSale(Sale sale)throws SQLException {
String sale_date=sale.getSale_date();
String car_type =sale.getCar_type();
String color = sale.getColor();
Integer number = sale.getNumber();
String handler=sale.getHandler();
Connection con=(Connection) DBUtil.getConnection();
String sql= "update sales_info"
+" set sale_date=\""+sale_date+"\","
+ "car_type=\""+car_type+"\","
+ "color=\""+color+"\","
+ "number=\""+number+"\""
+ " where handler=\""+handler+"\";";
Statement stm = (Statement) con.createStatement();
try {
stm.execute(sql);
JOptionPane.showMessageDialog(null, "数据修改成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据修改失败","tips",JOptionPane.PLAIN_MESSAGE);
}
}
// 查询表信息
public List<Sale> query() throws Exception {
Connection con = (Connection) DBUtil.getConnection();
Statement stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery("select sale_date,car_type,color,number,handler from sales_info");
List<Sale> saleList = new ArrayList<Sale>();
Sale sale = null;
while (rs.next()) {
sale = new Sale();
sale.setSale_date(rs.getString("sale_date"));
sale.setCar_type(rs.getString("car_type"));
sale.setColor(rs.getString("color"));
sale.setNumber(rs.getInt("number"));
sale.setHandler(rs.getString("handler"));
saleList.add(sale);
}
return saleList;
}
}
UserDao.java
管理员信息dao层
package com.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.entity.User;
import com.utils.DBUtil;
public class UserDao {
public static List<User> query() throws Exception{
Connection con = DBUtil.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select id,user,password from user_info");
List<User> userList = new ArrayList<User>();
User user = null;
// 如果对象中有数据,就会循环打印出来
while (rs.next()){
user = new User(); // 调用模型层
user.setID(rs.getInt("id"));
user.setUserName(rs.getString("user"));
user.setPassword(rs.getString("password"));
userList.add(user);
}
return userList;
}
}
WorkerDao.java
员工信息dao层
package com.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import com.entity.Worker;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.utils.DBUtil;
public class WorkerDao {
//添加员工信息
public void addWorker(Worker worker) throws Exception {
Connection con= (Connection) DBUtil.getConnection();
String sql="insert into worker_info"
+"(worker_id,name,sex,age,origin,education)"
+"values(?,?,?,?,?,?)";
PreparedStatement psmt = con.prepareStatement(sql);
psmt.setInt(1, worker.getWorker_id());
psmt.setString(2, worker.getName());
psmt.setString(3, worker.getSex());
psmt.setString(4, worker.getAge());
psmt.setString(5, worker.getOrigin());
psmt.setString(6, worker.getEducation());
try {
psmt.execute();
JOptionPane.showMessageDialog(null, "数据添加成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据添加失败","tips",JOptionPane.PLAIN_MESSAGE);
}
}
//删除员工信息
public void delWorker(int worker_id) throws SQLException {
Connection con=(Connection) DBUtil.getConnection();
String sql=
"delete from worker_info where worker_id = ?";
PreparedStatement psmt=con.prepareStatement(sql);
psmt.setInt(1,worker_id);
try {
psmt.execute();
JOptionPane.showMessageDialog(null, "数据删除成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据删除失败","tips",JOptionPane.PLAIN_MESSAGE);
}
}
//更新员工信息
public void changeWorker(Worker worker) throws SQLException {
String name = worker.getName();
String sex= worker.getSex();
String age = worker.getAge();
String origin = worker.getOrigin();
String education = worker.getEducation();
Integer worker_id = worker.getWorker_id();
Connection con = (Connection) DBUtil.getConnection();
String sql ="update worker_info"
+" set name =\""+name+"\","
+ "sex = \""+sex+"\","
+ "age = \""+age+"\","
+ "origin =\""+origin+"\","
+ "education =\""+education+"\""
+" where worker_id ="+worker_id+";";
Statement smt = (Statement) con.createStatement();
try {
smt.execute(sql);
JOptionPane.showMessageDialog(null, "数据更新成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据更新失败","tips",JOptionPane.PLAIN_MESSAGE);
}
}
// 查询表信息
public List<Worker> query() throws Exception {
Connection con = (Connection) DBUtil.getConnection();
Statement stmt = (Statement) con.createStatement();
ResultSet rs = stmt.executeQuery("select worker_id,name,sex,age,origin,education from worker_info");
List<Worker> workerList = new ArrayList<Worker>();
Worker worker = null;
while (rs.next()) {
worker = new Worker();
worker.setWorker_id(Integer.parseInt(rs.getString("worker_id")));
worker.setName(rs.getString("name"));
worker.setSex(rs.getString("sex"));
worker.setAge(rs.getString("age"));
worker.setOrigin(rs.getString("origin"));
worker.setEducation(rs.getString("education"));
workerList.add(worker);
}
return workerList;
}
}
controller层
CarController.java
汽车信息控制层
package com.controller;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JTextField;
import com.dao.CarDao;
import com.entity.Car;
public class CarController {
//添加car信息
public void addCarInformation(JTextField textFieldCarID, // 汽车ID
JTextField textFieldModel, // 汽车型号
JTextField textFieldColor, // 汽车颜色
JTextField textFieldManuFactory, // 生产厂商
JTextField textFieldFactoryDate, // 出厂日期
JTextField textFieldPrice) throws Exception{
CarDao carDao = new CarDao();
Car car = new Car();
int car_id=Integer.parseInt(textFieldCarID.getText());
car.setCar_id(car_id);//强制类型转换
car.setModel(textFieldModel.getText());
car.setColor(textFieldColor.getText());
car.setManufactory(textFieldManuFactory.getText());
car.setFactory_date(textFieldFactoryDate.getText());
car.setPrice(textFieldPrice.getText());
carDao.addCar(car);
}
//删除car信息
public void delCarInformation(int car_id) throws SQLException {
CarDao carDao = new CarDao();
// Car car = new Car();
// car.setCar_id(car_id);
//删除汽车信息
carDao.delCar(car_id);
}
//修改car信息
public void changeCarInformation(JTextField textFieldCarID, // 汽车ID
JTextField textFieldModel, // 汽车型号
JTextField textFieldColor, // 汽车颜色
JTextField textFieldManuFactory, // 生产厂商
JTextField textFieldFactoryDate, // 出厂日期
JTextField textFieldPrice//汽车价钱
)throws Exception {
CarDao carDao = new CarDao();
Car car = new Car();
car.setCar_id(Integer.parseInt(textFieldCarID.getText()));
car.setModel(textFieldModel.getText());
car.setColor(textFieldColor.getText());
car.setManufactory(textFieldManuFactory.getText());
car.setFactory_date(textFieldFactoryDate.getText());
car.setPrice(textFieldPrice.getText());
//修改汽车信息
carDao.changeCar(car);
// System.out.println(car.getCar_id()+car.getColor()+car.getModel()+car.getManufactory()+car.getFactory_date()+car.getPrice());
}
//查询表信息并放入二维数组中保存
public Object[][] query(String[] columnNames) throws Exception{
// TODO Auto-generated method stub
CarDao carDao=new CarDao();
List<Car> list = carDao.query();
Object[][] results= new Object[list.size()][columnNames.length];
for (int i = 0; i < list.size(); i++) {
Car car = (Car)list.get(i);
results[i][0] = car.getCar_id();
results[i][1] = car.getModel();
results[i][2]=car.getColor();
results[i][3]=car.getManufactory();
results[i][4]=car.getFactory_date();
results[i][5]=car.getPrice();
}
return results;
}
}
GuestController.java
客户信息控制层
package com.controller;
import java.util.List;
import javax.swing.JTextField;
import com.dao.GuestDao;
import com.entity.Guest;
public class GuestController {
//增加客户信息
public void addGuestInformation(
JTextField guestNameField,
JTextField contactInformationField,
JTextField addressField,
JTextField businessRecordField) throws Exception{
GuestDao guestDao = new GuestDao();
Guest guest = new Guest();
guest.setGuest_name(guestNameField.getText());
guest.setContact_information(contactInformationField.getText());
guest.setAddress(addressField.getText());
guest.setBusiness_record(businessRecordField.getText());
guestDao.addGuest(guest);
}
//删除客户信息
public void delGuestInformation(String guest_name)throws Exception {
GuestDao guestDao = new GuestDao();
guestDao.delGuest(guest_name);
}
//修改客户信息
public void changeGuestInformation(JTextField guestNameField,
JTextField contactInformationField,
JTextField addressField,
JTextField businessRecordField) throws Exception{
GuestDao guestDao = new GuestDao();
Guest guest = new Guest();
guest.setGuest_name(guestNameField.getText());
guest.setContact_information(contactInformationField.getText());
guest.setAddress(addressField.getText());
guest.setBusiness_record(businessRecordField.getText());
guestDao.changeGuest(guest);
}
//查询表信息并放入二维数组中保存
public Object[][] query(String[] columnNames) throws Exception{
// TODO Auto-generated method stub
GuestDao guestDao = new GuestDao();
List<Guest> list = guestDao.query();
Object[][] results= new Object[list.size()][columnNames.length];
for (int i = 0; i < list.size(); i++) {
Guest car = (Guest)list.get(i);
results[i][0] = car.getGuest_name();
results[i][1] = car.getContact_information();
results[i][2]=car.getAddress();
results[i][3]=car.getBusiness_record();
}
return results;
}
}
SaleController.java
销售信息控制层
package com.controller;
import java.util.List;
import javax.swing.JTextField;
import com.dao.SaleDao;
import com.entity.Sale;
public class SaleController {
//添加销售信息
public void addSaleInformation(JTextField saleDateField,
JTextField carTypeField,
JTextField colorField,
JTextField numberField,
JTextField handlerField)throws Exception {
SaleDao saleDao = new SaleDao();
Sale sale = new Sale();
sale.setSale_date(saleDateField.getText());
sale.setCar_type(carTypeField.getText());
sale.setColor(colorField.getText());
sale.setNumber(Integer.parseInt(numberField.getText()));
sale.setHandler(handlerField.getText());
saleDao.addSale(sale);
}
//删除销售信息
public void delSaleInformation(String sale_date)throws Exception {
SaleDao saleDao = new SaleDao();
saleDao.delSale(sale_date);
}
//修改销售信息
public void changeSaleInformation(JTextField saleDateField,
JTextField carTypeField,
JTextField colorField,
JTextField numberField,
JTextField handlerField) throws Exception{
SaleDao saleDao = new SaleDao();
Sale sale = new Sale();
sale.setSale_date(saleDateField.getText());
sale.setCar_type(carTypeField.getText());
sale.setColor(colorField.getText());
sale.setNumber(Integer.parseInt(numberField.getText()));
sale.setHandler(handlerField.getText());
saleDao.changeSale(sale);
}
//查询表信息并放入二维数组中保存
public Object[][] query(String[] columnNames) throws Exception{
// TODO Auto-generated method stub
SaleDao saleDao = new SaleDao();
List<Sale> list = saleDao.query();
Object[][] results= new Object[list.size()][columnNames.length];
for (int i = 0; i < list.size(); i++) {
Sale sale= (Sale)list.get(i);
results[i][0] = sale.getSale_date();
results[i][1] = sale.getCar_type();
results[i][2]=sale.getColor();
results[i][3]=sale.getNumber();
results[i][4]=sale.getHandler();
}
return results;
}
}
UserController.java
管理员控制层
package com.controller;
import java.util.List;
import com.dao.UserDao;
import com.entity.User;
public class UserController {
public String login(String username, String passwd) {
String result = null;
try {
List<User> userList = UserDao.query();
for (User user : userList) {
if (user.getUserName().equals(username) && user.getPassword().equals(passwd)) {
result = "1";
} else if(username.equals("") || passwd.equals("")) {
result = "2";
}
else result="3";
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
WorkerController.java
员工信息控制层
package com.controller;
import java.util.List;
import javax.swing.JTable;
import javax.swing.JTextField;
import com.dao.WorkerDao;
import com.entity.Worker;
public class WorkerController {
//添加员工信息
public void addWorkerInformation(JTextField workerIDField,
JTextField nameField,
JTextField sexField,
JTextField ageField,
JTextField originField,
JTextField educationField) throws Exception{
WorkerDao workerDao = new WorkerDao();
Worker worker=new Worker();
worker.setWorker_id(Integer.parseInt(workerIDField.getText()));
worker.setName(nameField.getText());
worker.setSex(sexField.getText());
worker.setAge(ageField.getText());
worker.setOrigin(originField.getText());
worker.setEducation(educationField.getText());
workerDao.addWorker(worker);
}
//删除员工信息
public void delWorkerInformation(int worker_id) throws Exception {
WorkerDao workerDao = new WorkerDao();
workerDao.delWorker(worker_id);
}
//修改员工信息
public void changeWorkerinformation(
JTextField nameField,
JTextField sexField,
JTextField ageField,
JTextField originField,
JTextField educationField, JTable table)throws Exception {
WorkerDao workerDao = new WorkerDao();
Worker worker =new Worker();
int selRow = table.getSelectedRow();
int worker_id= Integer.parseInt(table.getValueAt(selRow, 0).toString());
worker.setWorker_id(worker_id);
worker.setName(nameField.getText());
worker.setSex(sexField.getText());
worker.setAge(ageField.getText());
worker.setOrigin(originField.getText());
worker.setEducation(educationField.getText());
workerDao.changeWorker(worker);
}
//查询表信息并放入二维数组中保存
public Object[][] query(String[] columnNames) throws Exception{
// TODO Auto-generated method stub
WorkerDao workerDao = new WorkerDao();
List<Worker> list = workerDao.query();
Object[][] results= new Object[list.size()][columnNames.length];
for (int i = 0; i < list.size(); i++) {
Worker worker= (Worker)list.get(i);
results[i][0] = worker.getWorker_id();
results[i][1] = worker.getName();
results[i][2]=worker.getSex();
results[i][3]=worker.getAge();
results[i][4]=worker.getOrigin();
results[i][5]=worker.getEducation();
}
return results;
}
}
view层(界面)
CarView.java
汽车信息视图层
package com.view;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import com.controller.CarAction;
import com.utils.BackgroundImage;
import com.utils.FrameOption;
import com.utils.MenuBar;
import com.utils.SetTable;
public class carView {
JFrame main = new JFrame("Car Sale System");
Container container = main.getContentPane();
JButton buttonAdd, buttonDel, buttonChange, buttonReset;
JTextField textFieldCarID, // 汽车ID
textFieldModel, // 汽车型号
textFieldColor, // 汽车颜色
textFieldManuFactory, // 生产厂商
textFieldFactoryDate, // 出厂日期
textFieldPrice;// 价格
JTable table;// 表格
// 显示表格的滚动面板
JScrollPane scrollPane;
CarAction carAction;
public carView() {
// TODO Auto-generated constructor stub
main.setLayout(null);
new BackgroundImage(main, container, "CarBackground.jpg");
new FrameOption(main);
new MenuBar(main);
// 实例化标签和文本框
JLabel JLabelCarID = new JLabel("CarID");
JLabelCarID.setForeground(Color.green);
JLabelCarID.setBounds(75, 350, 100, 20);
JTextField textFieldCarID = new JTextField();
textFieldCarID.setBounds(175, 350, 100, 20);
JLabel JLabelModel = new JLabel("Model");
JLabelModel.setForeground(Color.green);
JLabelModel.setBounds(350, 350, 100, 20);
JTextField textFieldModel = new JTextField();
textFieldModel.setBounds(450, 350, 100, 20);
JLabel JLabelColor = new JLabel("Color");
JLabelColor.setForeground(Color.green);
JLabelColor.setBounds(625, 350, 100, 20);
JTextField textFieldColor = new JTextField();
textFieldColor.setBounds(725, 350, 100, 20);
JLabel JLabelManuFactory = new JLabel("ManuFactory");
JLabelManuFactory.setForeground(Color.green);
JLabelManuFactory.setBounds(75, 400, 100, 20);
JTextField textFieldManuFactory = new JTextField();
textFieldManuFactory.setBounds(175, 400, 100, 20);
JLabel JLabelFactorydate = new JLabel("FactoryDate");
JLabelFactorydate.setForeground(Color.green);
JLabelFactorydate.setBounds(350, 400, 100, 20);
JTextField textFieldFactoryDate = new JTextField();
textFieldFactoryDate.setBounds(450, 400, 100, 20);
JLabel JLabelPrice = new JLabel("Price");
JLabelPrice.setForeground(Color.green);
JLabelPrice.setBounds(625, 400, 100, 20);
JTextField textFieldPrice = new JTextField();
textFieldPrice.setBounds(725, 400, 100, 20);
// 使文本框透明
textFieldCarID.setOpaque(false);
textFieldModel.setOpaque(false);
textFieldColor.setOpaque(false);
textFieldColor.setOpaque(false);
textFieldManuFactory.setOpaque(false);
textFieldFactoryDate.setOpaque(false);
textFieldPrice.setOpaque(false);
//修改输入字体颜色
textFieldCarID.setForeground(new Color(19, 190, 196));
textFieldModel.setForeground(new Color(19, 190, 196));
textFieldColor.setForeground(new Color(19, 190, 196));
textFieldManuFactory.setForeground(new Color(19, 190, 196));
textFieldFactoryDate.setForeground(new Color(19, 190, 196));
textFieldPrice.setForeground(new Color(19, 190, 196));
container.add(JLabelCarID);
container.add(JLabelModel);
container.add(JLabelColor);
container.add(JLabelManuFactory);
container.add(JLabelFactorydate);
container.add(JLabelPrice);
container.add(textFieldCarID);
container.add(textFieldModel);
container.add(textFieldColor);
container.add(textFieldManuFactory);
container.add(textFieldFactoryDate);
container.add(textFieldPrice);
// 实例化按钮
buttonAdd = new JButton("Add");
buttonAdd.setBounds(100, 480, 100, 20);
buttonDel = new JButton("Delect");
buttonDel.setBounds(300, 480, 100, 20);
buttonChange = new JButton("Change");
buttonChange.setBounds(500, 480, 100, 20);
buttonReset = new JButton("Query");
buttonReset.setBounds(700, 480, 100, 20);
//添加按钮
container.add(buttonAdd);
container.add(buttonDel);
container.add(buttonChange);
container.add(buttonReset);
carAction = new CarAction();
//初始化表格
setTable();
//设置按钮监听
//添加按钮
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
carAction.addCarInformation(textFieldCarID, textFieldModel, textFieldColor,
textFieldManuFactory, textFieldFactoryDate, textFieldPrice);
main.setVisible(false);
new carView();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
});
//删除按钮
buttonDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
carAction.delCarInformation(Integer.parseInt(textFieldCarID.getText()));
main.setVisible(false);
new carView();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
//修改按钮
buttonChange.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
carAction.changeCarInformation(textFieldCarID, textFieldModel, textFieldColor,
textFieldManuFactory, textFieldFactoryDate, textFieldPrice);
main.setVisible(false);
new carView();
} catch (Exception e2) {
// TODO: handle exception
JOptionPane.showMessageDialog(null,"表中没有该数据","错误"
, JOptionPane.PLAIN_MESSAGE);
}
}
});
//查询按钮
buttonReset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
setTable();
main.setVisible(false);
new carView();
JOptionPane.showMessageDialog(null, "数据更新成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e1) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据更新失败","tips",JOptionPane.PLAIN_MESSAGE);
}
}
});
}
private void setTable() {
// TODO Auto-generated method stub
// 列名
String[] columnNames = { "轿车编号", "型号", "颜色", "生产厂家", "出厂日期", "价格(元)" };
try {
CarAction carAction = new CarAction();
Object[][] results = carAction.query(columnNames);
table = new JTable(results, columnNames);
scrollPane = new JScrollPane(table);
// 设置表格
new SetTable(scrollPane,table,columnNames,main);
table.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
GuestView.java
客户信息视图层
package com.view;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import com.controller.GuestAction;
import com.utils.BackgroundImage;
import com.utils.FrameOption;
import com.utils.MenuBar;
import com.utils.SetTable;
public class guestView {
JFrame main = new JFrame("Car Sale System");
Container container = main.getContentPane();
private JScrollPane scrollPane;
private JButton buttonAdd;
private JButton buttonReset;
private JButton buttonChange;
private JButton buttonDel;
JTable table;
JTextField guestNameField, contactInformationField, addressField, businessRecordField;
private GuestAction guestAction;
public guestView() {
// TODO Auto-generated constructor stub
main.setLayout(null);
new BackgroundImage(main, container, "GuestBackground.jpg");
new FrameOption(main);
new MenuBar(main);
// 实例化标签和文本框
JLabel guestNameJLabel = new JLabel("GuestName");
guestNameJLabel.setForeground(Color.green);
guestNameJLabel.setBounds(100, 350, 125, 20);
JTextField guestNameField = new JTextField();
guestNameField.setBounds(225, 350, 175, 20);
JLabel contactInformationJLabel = new JLabel("ContactInformation");
contactInformationJLabel.setForeground(Color.green);
contactInformationJLabel.setBounds(500, 350, 125, 20);
JTextField contactInformationField = new JTextField();
contactInformationField.setBounds(625, 350, 150, 20);
JLabel addressJLabel = new JLabel("Address");
addressJLabel.setForeground(Color.green);
addressJLabel.setBounds(100, 400, 125, 20);
JTextField addressField = new JTextField();
addressField.setBounds(225, 400, 175, 20);
JLabel businessRecordJLabel = new JLabel("BusinessRecord");
businessRecordJLabel.setForeground(Color.green);
businessRecordJLabel.setBounds(500, 400, 125, 20);
JTextField businessRecordField = new JTextField();
businessRecordField.setBounds(625, 400, 150, 20);
// 修改输入字体颜色
guestNameField.setForeground(new Color(19, 190, 196));
contactInformationField.setForeground(new Color(19, 190, 196));
addressField.setForeground(new Color(19, 190, 196));
businessRecordField.setForeground(new Color(19, 190, 196));
// 使文本框透明
guestNameField.setOpaque(false);
contactInformationField.setOpaque(false);
addressField.setOpaque(false);
businessRecordField.setOpaque(false);
container.add(guestNameJLabel);
container.add(guestNameField);
container.add(contactInformationJLabel);
container.add(contactInformationField);
container.add(addressJLabel);
container.add(addressField);
container.add(businessRecordJLabel);
container.add(businessRecordField);
// 实例化按钮
buttonAdd = new JButton("Add");
buttonAdd.setBounds(100, 480, 100, 20);
buttonDel = new JButton("Delect");
buttonDel.setBounds(300, 480, 100, 20);
buttonChange = new JButton("Change");
buttonChange.setBounds(500, 480, 100, 20);
buttonReset = new JButton("Query");
buttonReset.setBounds(700, 480, 100, 20);
container.add(buttonAdd);
container.add(buttonDel);
container.add(buttonChange);
container.add(buttonReset);
guestAction = new GuestAction();
//初始化表格
setTable();
//添加按钮监听
//添加按钮
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
guestAction.addGuestInformation(guestNameField, contactInformationField, addressField,
businessRecordField);
main.setVisible(false);
new guestView();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
//删除按钮
buttonDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
guestAction.delGuestInformation(guestNameField.getText());
main.setVisible(false);
new guestView();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
//修改按钮
buttonChange.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
guestAction.changeGuestInformation(guestNameField, contactInformationField, addressField,
businessRecordField);
main.setVisible(false);
new guestView();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
//查询按钮
buttonReset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
setTable();
main.setVisible(false);
new guestView();
JOptionPane.showMessageDialog(null, "数据更新成功", "tips", JOptionPane.PLAIN_MESSAGE);
} catch (Exception e1) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据更新失败", "tips", JOptionPane.PLAIN_MESSAGE);
}
}
});
}
private void setTable() {
String[] columnNames = { "姓名", "联系方式", "地址", "业务联系记录" };
try {
GuestAction guestAction = new GuestAction();
Object[][] results = guestAction.query(columnNames);
table = new JTable(results, columnNames);
scrollPane = new JScrollPane(table);
// 设置表格
new SetTable(scrollPane, table, columnNames, main);
table.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
SaleView.java
销售信息视图层
package com.view;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import com.controller.SaleAction;
import com.utils.BackgroundImage;
import com.utils.FrameOption;
import com.utils.MenuBar;
import com.utils.SetTable;
public class saleView {
JFrame main = new JFrame("Car Sale System");
Container container = main.getContentPane();
private JScrollPane scrollPane;
private JButton buttonReset;
private JButton buttonChange;
private JButton buttonDel;
private JButton buttonAdd;
private JTable table;
private SaleAction saleAction;
public saleView() {
// TODO Auto-generated constructor stub
main.setLayout(null);
new BackgroundImage(main, container, "SaleBackground.jpg");
new FrameOption(main);
new MenuBar(main);
// 实例化标签和文本框
JLabel saleDateJLabel = new JLabel("SaleDate");
saleDateJLabel.setForeground(Color.green);
saleDateJLabel.setBounds(75, 350, 100, 20);
JTextField saleDateField = new JTextField();
saleDateField.setBounds(175, 350, 100, 20);
JLabel carTypeJLabel = new JLabel("CarType");
carTypeJLabel.setForeground(Color.green);
carTypeJLabel.setBounds(350, 350, 100, 20);
JTextField carTypeField = new JTextField();
carTypeField.setBounds(450, 350, 100, 20);
JLabel colorJLabel = new JLabel("Color");
colorJLabel.setForeground(Color.green);
colorJLabel.setBounds(625, 350, 100, 20);
JTextField colorField = new JTextField();
colorField.setBounds(725, 350, 100, 20);
JLabel numberJLabel = new JLabel("Number");
numberJLabel.setForeground(Color.green);
numberJLabel.setBounds(150, 400, 125, 20);
JTextField numberField = new JTextField();
numberField.setBounds(225, 400, 175, 20);
JLabel handlerJLabel = new JLabel("Handler");
handlerJLabel.setForeground(Color.green);
handlerJLabel.setBounds(500, 400, 125, 20);
JTextField handlerField = new JTextField();
handlerField.setBounds(625, 400, 150, 20);
// 修改输入文字颜色
saleDateField.setForeground(new Color(12, 239, 34));
carTypeField.setForeground(new Color(12, 239, 34));
colorField.setForeground(new Color(12, 239, 34));
numberField.setForeground(new Color(12, 239, 34));
handlerField.setForeground(new Color(12, 239, 34));
// 使文本框透明
saleDateField.setOpaque(false);
carTypeField.setOpaque(false);
colorField.setOpaque(false);
numberField.setOpaque(false);
handlerField.setOpaque(false);
container.add(saleDateJLabel);
container.add(saleDateField);
container.add(carTypeJLabel);
container.add(carTypeField);
container.add(colorJLabel);
container.add(colorField);
container.add(numberJLabel);
container.add(numberField);
container.add(handlerJLabel);
container.add(handlerField);
// 实例化按钮
buttonAdd = new JButton("Add");
buttonAdd.setBounds(100, 480, 100, 20);
buttonDel = new JButton("Delect");
buttonDel.setBounds(300, 480, 100, 20);
buttonChange = new JButton("Change");
buttonChange.setBounds(500, 480, 100, 20);
buttonReset = new JButton("Query");
buttonReset.setBounds(700, 480, 100, 20);
container.add(buttonAdd);
container.add(buttonDel);
container.add(buttonChange);
container.add(buttonReset);
saleAction = new SaleAction();
// 初始化表格
setTable();
// 添加按钮监听
// 添加按钮
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
saleAction.addSaleInformation(saleDateField, carTypeField, colorField, numberField, handlerField);
main.setVisible(false);
new saleView();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
// 删除按钮
buttonDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
saleAction.delSaleInformation(saleDateField.getText());
main.setVisible(false);
new saleView();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
// 修改按钮
buttonChange.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
saleAction.changeSaleInformation(saleDateField, carTypeField, colorField, numberField,
handlerField);
main.setVisible(false);
new saleView();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
// 查询按钮
buttonReset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
setTable();
main.setVisible(false);
new saleView();
JOptionPane.showMessageDialog(null, "数据更新成功", "tips", JOptionPane.PLAIN_MESSAGE);
} catch (Exception e1) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据更新失败", "tips", JOptionPane.PLAIN_MESSAGE);
}
}
});
}
private void setTable() {
// TODO Auto-generated method stub
String[] columnNames = { "销售日期", "轿车类型", "颜色", "数量", "经手人" };
try {
SaleAction saleAction = new SaleAction();
Object[][] results = saleAction.query(columnNames);
table = new JTable(results, columnNames);
scrollPane = new JScrollPane(table);
// 设置表格
new SetTable(scrollPane, table, columnNames, main);
table.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
WorkerView.java
员工信息视图层
package com.view;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import com.controller.WorkerAction;
import com.utils.BackgroundImage;
import com.utils.FrameOption;
import com.utils.MenuBar;
import com.utils.SetTable;
public class workerView {
JFrame main = new JFrame("Car Sale System");
Container container = main.getContentPane();
private JScrollPane scrollPane;
JButton buttonAdd, buttonDel, buttonChange, buttonReset;
private JTable table;
JTextField
workerIDField,
nameField,
sexField,
ageField,
originField,
educationField;
private WorkerAction workerAction;
public workerView() {
main.setLayout(null);
new BackgroundImage(main, container, "WorkerBackground.jpg");
new FrameOption(main);
new MenuBar(main);
// 实例化标签和文本框
JLabel workerIDJLabel = new JLabel("CarID");
workerIDJLabel.setForeground(Color.green);
workerIDJLabel.setBounds(75, 350, 100, 20);
JTextField workerIDField= new JTextField();
workerIDField.setBounds(175, 350, 100, 20);
JLabel nameJLabel = new JLabel("Name");
nameJLabel.setForeground(Color.green);
nameJLabel.setBounds(350, 350, 100, 20);
JTextField nameField = new JTextField();
nameField.setBounds(450, 350, 100, 20);
JLabel sexJLabel = new JLabel("Sex");
sexJLabel.setForeground(Color.green);
sexJLabel.setBounds(625, 350, 100, 20);
JTextField sexField = new JTextField();
sexField.setBounds(725, 350, 100, 20);
JLabel ageJLabel = new JLabel("Age");
ageJLabel.setForeground(Color.green);
ageJLabel.setBounds(75, 400, 100, 20);
JTextField ageField = new JTextField();
ageField.setBounds(175, 400, 100, 20);
JLabel originJLabel = new JLabel("Origin");
originJLabel.setForeground(Color.green);
originJLabel.setBounds(350, 400, 100, 20);
JTextField originField = new JTextField();
originField.setBounds(450, 400, 100, 20);
JLabel educationJLabel = new JLabel("Education");
educationJLabel.setForeground(Color.green);
educationJLabel.setBounds(625, 400, 100, 20);
JTextField educationField = new JTextField();
educationField.setBounds(725, 400, 100, 20);
//修改输入字体颜色
workerIDField.setForeground(new Color(19, 190, 196));
nameField.setForeground(new Color(19, 190, 196));
sexField.setForeground(new Color(19, 190, 196));
ageField.setForeground(new Color(19, 190, 196));
originField.setForeground(new Color(19, 190, 196));
educationField.setForeground(new Color(19, 190, 196));
//使文本框透明
workerIDField.setOpaque(false);
nameField.setOpaque(false);
sexField.setOpaque(false);
ageField.setOpaque(false);
originField.setOpaque(false);
educationField.setOpaque(false);
container.add(workerIDJLabel);
container.add(workerIDField);
container.add(nameJLabel);
container.add(nameField);
container.add(sexJLabel);
container.add(sexField);
container.add(ageJLabel);
container.add(ageField);
container.add(originJLabel);
container.add(originField);
container.add(educationJLabel);
container.add(educationField);
// 实例化按钮
buttonAdd = new JButton("Add");
buttonAdd.setBounds(100, 480, 100, 20);
buttonDel = new JButton("Delect");
buttonDel.setBounds(300, 480, 100, 20);
buttonChange = new JButton("Change");
buttonChange.setBounds(500, 480, 100, 20);
buttonReset = new JButton("Query");
buttonReset.setBounds(700, 480, 100, 20);
container.add(buttonAdd);
container.add(buttonDel);
container.add(buttonChange);
container.add(buttonReset);
workerAction = new WorkerAction();
//初始化表格
setTable();
//添加按钮监听
//添加按钮
buttonAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
workerAction.addWorkerInformation(workerIDField, nameField, sexField,
ageField, originField, educationField);
main.setVisible(false);
new workerView();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
});
//刪除按鈕
buttonDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
workerAction.delWorkerInformation(Integer.parseInt(workerIDField.getText()));
main.setVisible(false);
new workerView();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
});
//修改按鈕
buttonChange.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
workerAction.changeWorkerinformation(nameField, sexField, ageField,
originField, educationField, table);
main.setVisible(false);
new workerView();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
//查询按钮
buttonReset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
setTable();
main.setVisible(false);
new workerView();
JOptionPane.showMessageDialog(null, "数据更新成功","tips",JOptionPane.PLAIN_MESSAGE);
} catch (Exception e1) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "数据更新失败","tips",JOptionPane.PLAIN_MESSAGE);
}
}
});
}
private void setTable() {
// TODO Auto-generated method stub
String[] columnNames = { "员工编号","姓名","性别","年龄","籍贯","学历" };
try {
WorkerAction workerAction = new WorkerAction();
Object[][] results = workerAction.query(columnNames);
table = new JTable(results, columnNames);
scrollPane = new JScrollPane(table);
// 设置表格
new SetTable(scrollPane,table,columnNames,main);
table.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Login.java
登录界面视图层
package com.view;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import com.controller.UserAction;
import com.view.MainFrame;
public class Login {
public void loginGUi(char[] passwordStrings) {
JFrame login = new JFrame();
login.setTitle("Welcome login system");
login.setSize(500, 400);
login.setLocationRelativeTo(null);
login.setResizable(false);
login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
login.setLayout(new GridLayout(3, 1));
JLabel jl_username = new JLabel("UserName: ", JLabel.CENTER);
JTextField JTextFieldUser = new JTextField(JTextField.LEFT);
JLabel jl_password = new JLabel("Password: ", JLabel.CENTER);
JPasswordField JPasswordField = new JPasswordField(SwingConstants.LEFT);
jl_username.setFont(new Font("comic sans MS", Font.BOLD, 20));
JTextFieldUser.setFont(new Font("Segoe Print", Font.BOLD, 20));
jl_password.setFont(new Font("comic sans MS", Font.BOLD, 20));
JPasswordField.setFont(new Font("Segoe Print", Font.BOLD, 20));
JButton jb_login = new JButton("Login");
JButton jb_exit = new JButton("Exit");
jb_login.setFont(new Font("Segoe Print", Font.BOLD, 20));
jb_exit.setFont(new Font("Segoe Print", Font.BOLD, 20));
ImageIcon bgimg = new ImageIcon("res/Welcome.jpg");
JLabel imgLabel = new JLabel(bgimg);
login.add(imgLabel);
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
jp2.setLayout(new GridLayout(2, 2, 5, 10));
JPanel jp3 = new JPanel();
jp3.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 50));
jp1.add(imgLabel);
jp2.add(jl_username);
jp2.add(JTextFieldUser);
jp2.add(jl_password);
jp2.add(JPasswordField);
jp3.add(jb_login);
jp3.add(jb_exit);
login.add(jp1);
login.add(jp2);
login.add(jp3);
login.setVisible(true);
// 设置键盘监听事件
jb_login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String username=JTextFieldUser.getText();
String passwd = new String(JPasswordField.getPassword());
com.controller.UserAction userAction = new UserAction();
String result = userAction.login(username,passwd);
if (result.equals("1")) {
login.setVisible(false);
new MainFrame().MainGUI();
}else if(result.equals("2")) {
JOptionPane.showMessageDialog(null,"用户名或密码不能为空","错误"
,JOptionPane.PLAIN_MESSAGE);
} else if(result.equals("3")) {
JOptionPane.showMessageDialog(null,"用户名或密码错误","错误"
,JOptionPane.PLAIN_MESSAGE);
}
}
});
// 退出按钮鼠标监听
jb_exit.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
}
MainFrame.java
项目主界面视图层
package com.view;
import java.awt.Container;
import javax.swing.*;
import com.utils.*;
public class MainFrame {
JFrame main = new JFrame("Car Sale System");
Container container = main.getContentPane();
public void MainGUI() {
new FrameOption(main);
new BackgroundImage(main, container, "MainBackground.jpg");
new MenuBar(main);
}
}
entity层
Car.java
汽车实体类
package com.entity;
public class Car {
private int car_id; // 轿车编号
private String model; // 型号
private String color; //颜色
private String manufactory; // 生产厂家
private String factory_date; // 出厂日期
private String price;// 价格
public int getCar_id() {
return car_id;
}
public void setCar_id(int car_id) {
this.car_id = car_id;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getManufactory() {
return manufactory;
}
public void setManufactory(String manufactory) {
this.manufactory = manufactory;
}
public String getFactory_date() {
return factory_date;
}
public void setFactory_date(String factory_date) {
this.factory_date = factory_date;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
Guest.java
客户实体类
package com.entity;
public class Guest {
private String guest_name; //客户姓名
private String contact_information; // 联系信息
private String address;
private String business_record;
public String getGuest_name() {
return guest_name;
}
public void setGuest_name(String guest_name) {
this.guest_name = guest_name;
}
public String getContact_information() {
return contact_information;
}
public void setContact_information(String contact_information) {
this.contact_information = contact_information;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBusiness_record() {
return business_record;
}
public void setBusiness_record(String business_record) {
this.business_record = business_record;
}
}
Sale.java
销售信息实体类
package com.entity;
public class Sale {
private String sale_date;
private String car_type;
private String color;
private Integer number;
private String handler;
public String getSale_date() {
return sale_date;
}
public void setSale_date(String sale_date) {
this.sale_date = sale_date;
}
public String getCar_type() {
return car_type;
}
public void setCar_type(String car_type) {
this.car_type = car_type;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getHandler() {
return handler;
}
public void setHandler(String handler) {
this.handler = handler;
}
}
User.java
管理员实体类
package com.entity;
public class User {
private int ID;
private String userName;
private String password;
public int getID() {
return ID;
}
public void setID(int iD) {
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;
}
}
Worker.java
员工实体类
package com.entity;
public class Worker {
private int worker_id;
private String name;
private String sex;
private String age;
private String origin;
private String education;
public int getWorker_id() {
return worker_id;
}
public void setWorker_id(int i) {
this.worker_id = i;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
}
utils工具类
BackgroundImage.java
背景图片工具类
package com.utils;
import java.awt.Container;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
// 背景图片设置
public class BackgroundImage {
public BackgroundImage(JFrame frame,Container container,String ImageName) {
// 限定加载图片路径
ImageIcon icon= new ImageIcon("res/" + ImageName);
final JLabel labelBackground = new JLabel();
ImageIcon iconBookManageSystemBackground = icon;
labelBackground.setIcon(iconBookManageSystemBackground);
// 设置label的大小
labelBackground.setBounds(0,0,iconBookManageSystemBackground.getIconWidth()
,iconBookManageSystemBackground.getIconHeight());
//将背景图片标签放入桌面面板的最底层
frame.getLayeredPane().add(labelBackground,new Integer(Integer.MIN_VALUE));
// 将容器转换为面板设置为透明
JPanel panel = (JPanel)container;
panel.setOpaque(false);
}
}
DBUtil.java
数据库工具类,主要用于加载配置文件连接数据库
package com.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBUtil {
private static String driverName;
private static String url;
private static String name;
private static String password;
static {
Properties properties = new Properties();
// 新建properties文件数据流
InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream("dbconfig.properties");
try {
properties.load(inputStream);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
driverName = (String) properties.get("driverName");
url = (String) properties.get("url");
name = (String) properties.get("name");
password = (String) properties.get("password");
try {
//加载JDBC驱动
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection con = null;
try {
// 建立数据库连接
con = DriverManager.getConnection(url, name, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
FrameOption.java
主界面相关设置工具类
package com.utils;
import javax.swing.JFrame;
public class FrameOption {
public FrameOption(JFrame main) {
// TODO Auto-generated constructor stub
main.setSize(900, 600);
main.setLocationRelativeTo(null);
main.setResizable(false);// 禁止修改窗口大小
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setVisible(true);
}
}
MenuBar.java
菜单栏工具类
package com.utils;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import com.view.*;
public class MenuBar {
JMenuBar menuBar;
JMenuItem carInfomationItem;
JMenuItem workerInformationItem;
JMenuItem guestInformationItem;
JMenuItem saleInformationItem;
JMenuItem exitItem;
public MenuBar(JFrame frame) {
// TODO Auto-generated constructor stub
menuBar = new JMenuBar();
carInfomationItem = new JMenuItem("汽车信息");
carInfomationItem.setBackground(Color.black);
carInfomationItem.setForeground(Color.green);
setCarInfomationItem(frame);
workerInformationItem = new JMenuItem("员工信息");
workerInformationItem.setBackground(Color.black);
workerInformationItem.setForeground(Color.green);
setWorkerInformationItem(frame);
guestInformationItem = new JMenuItem("客户信息");
guestInformationItem.setBackground(Color.black);
guestInformationItem.setForeground(Color.green);
setGuestInformationItem(frame);
saleInformationItem = new JMenuItem("销售信息");
saleInformationItem.setBackground(Color.black);
saleInformationItem.setForeground(Color.green);
setSaleInformationItem(frame);
exitItem = new JMenuItem("退出系统");
exitItem.setBackground(Color.black);
exitItem.setForeground(Color.red);
setExitItem(frame);
menuBar.add(carInfomationItem);
menuBar.add(workerInformationItem);
menuBar.add(guestInformationItem);
menuBar.add(saleInformationItem);
menuBar.add(exitItem);
frame.setJMenuBar(menuBar);
}
private void setCarInfomationItem(JFrame main) {
// TODO Auto-generated method stub
carInfomationItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
main.setVisible(false);
new carView();
}
});
}
private void setWorkerInformationItem(JFrame main) {
// TODO Auto-generated method stub
workerInformationItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
main.setVisible(false);
new workerView();
}
});
}
private void setGuestInformationItem(JFrame main) {
// TODO Auto-generated method stub
guestInformationItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
main.setVisible(false);
new guestView();
}
});
}
private void setSaleInformationItem(JFrame main) {
// TODO Auto-generated method stub
saleInformationItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
main.setVisible(false);
new saleView();
}
});
}
private void setExitItem(JFrame main) {
// TODO Auto-generated method stub
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
main.setVisible(false);
System.exit(0);
}
});
}
}
SetScrollPane.java
单元格设置工具类
package com.utils;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class SetScrollPane {
public SetScrollPane(JScrollPane scrollPane,JTable table) {
// TODO Auto-generated method stub
// 将JScrollPane设置为透明
scrollPane.setOpaque(false);
//将viewport设置为透明
scrollPane.getViewport().setOpaque(false);
//转载table
scrollPane.setViewportView(table);
//设置头部透明
scrollPane.setColumnHeaderView(table.getTableHeader());
scrollPane.getColumnHeader().setOpaque(false);
// 设置滚动条位置
scrollPane.setBounds(50, 50, 800, 250);
}
}
SetTable.java
表格设置工具类
package com.utils;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class SetTable {
public SetTable(JScrollPane scrollPane,JTable table,String[] columnNames,JFrame main) {
DefaultTableCellRenderer r = new DefaultTableCellRenderer();
r.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
table.setDefaultRenderer(Object.class, r);
// 自动调整列表状态
table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
//设置表格数据颜色
table.setForeground(Color.green);
table.setBackground(Color.black);
table.setOpaque(false);//将table设置为透明
scrollPane.setOpaque(false);//将scrollPane根面板设置为透明
scrollPane.getViewport().setOpaque(false);//将scrollPane的viewport设置为透明
// 设置滚动条位置
scrollPane.setBounds(50, 50, 800, 250);
main.add(scrollPane);
}
}
main主启动类
Main.java
项目主启动类,程序启动运行入口
package com.main;
import com.view.Login;
public class Main {
public static void main(String[] args) {
Login lg =new Login();
lg.loginGUi(null);
}
}
配置文件dbconfig.properties
driverName=com.mysql.jdbc.Driver
url=jdbc/:mysql/://127.0.0.1/:3306/carsalesystem?useSSL=false
name=root
password=toor
运行截图
- 登录界面
- 主界面
-
汽车信息
-
员工信息
-
客户信息
-
销售信息
开发者信息
- GhostGuest
- 个人博客地址:http://www.ghostai.top
项目源码地址
上一篇: pear安装phpunit异常问题
下一篇: eclipse出现乱码怎么解决