目录
前言
数据库的建立
Java系统
entity包
data包
Login包
windows包
主系统类
临近期末做了一个商品销售管理系统,分享下,全部源码在码云,需要自取,博客只分享部分代码(太多了)。
我们使用Navicat Premium 15连接mysql,建立如下九张表。
得到如下视图(看上去似乎有点乱了)
因为register是一个注册表,所以不参与视图里的连接。
各种表的情况如上所示,当然你也可以用sql语句写。
这个是我导出来的sql语句,由于我也没试过,所以不保证对错,如果错了只能老老实实自己差数据了。
/*
Navicat Premium Data Transfer
Source Server : 123
Source Server Type : MySQL
Source Server Version : 80027
Source Host : localhost:3306
Source Schema : user
Target Server Type : MySQL
Target Server Version : 80027
File Encoding : 65001
Date: 27/12/2021 21:08:30
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for comminfo
-- ----------------------------
DROP TABLE IF EXISTS `comminfo`;
CREATE TABLE `comminfo` (
`itemNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '商品编号',
`itemName` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '商品名称',
`brand` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '品牌',
`model` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '型号',
`salePrice` int NOT NULL COMMENT '售价',
PRIMARY KEY (`itemNo`) USING BTREE,
INDEX `pk10`(`salePrice`) USING BTREE,
CONSTRAINT `comminfo_pk` FOREIGN KEY (`itemNo`) REFERENCES `inventory` (`itemNo`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of comminfo
-- ----------------------------
INSERT INTO `comminfo` VALUES ('i001', '大航海时代', '海贼王', '其他', 10000);
INSERT INTO `comminfo` VALUES ('i002', '红发四皇', '海贼王', '其他', 5000);
INSERT INTO `comminfo` VALUES ('i003', '白胡子之死', '海贼王', '其他', 1000);
INSERT INTO `comminfo` VALUES ('i004', '路飞出海', '海贼王', '其他', 500);
INSERT INTO `comminfo` VALUES ('i005', '艾斯之死', '海贼王', '其他', 1000);
INSERT INTO `comminfo` VALUES ('i006', '克洛克达尔', '海贼王', '其他', 3000);
INSERT INTO `comminfo` VALUES ('i007', '汉库克逃脱', '海贼王', '其他', 2000);
INSERT INTO `comminfo` VALUES ('i008', '海绵宝宝的小窝', '其他', '其他', 10000);
INSERT INTO `comminfo` VALUES ('i009', '海星', '食品', 'min', 1000);
-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`employeeNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '员工号',
`employeeName` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '员工姓名',
`sex` char(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '性别',
`basePay` int NOT NULL COMMENT '基本工资',
`post` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '职务',
`password` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '密码',
PRIMARY KEY (`employeeNo`) USING BTREE,
INDEX `员工编号`(`employeeNo`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of employee
-- ----------------------------
INSERT INTO `employee` VALUES ('e001', '香克斯', '男', 4000, '四皇', '123456');
INSERT INTO `employee` VALUES ('e002', '路飞', '男', 500, '新星', '000111');
INSERT INTO `employee` VALUES ('e003', '索隆', '男', 500, '新星', '000222');
INSERT INTO `employee` VALUES ('e004', '娜美', '女', 500, '新星', '000333');
INSERT INTO `employee` VALUES ('e005', '山治', '男', 500, '新星', '000444');
INSERT INTO `employee` VALUES ('e006', '乌索普', '男', 500, '新星', '000555');
INSERT INTO `employee` VALUES ('e007', '罗杰', '男', 10000, '海贼王', '666666');
INSERT INTO `employee` VALUES ('e008', '爱德华', '男', 4000, '四皇', '888888');
INSERT INTO `employee` VALUES ('e009', '艾斯', '男', 600, '新星', '111111');
INSERT INTO `employee` VALUES ('e010', '克洛克达尔', '男', 800, '七武海', '222222');
INSERT INTO `employee` VALUES ('e011', '汉库克', '女', 800, '七武海', '333333');
-- ----------------------------
-- Table structure for employeeifom
-- ----------------------------
DROP TABLE IF EXISTS `employeeifom`;
CREATE TABLE `employeeifom` (
`employeeNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '员工号',
`employeeName` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '员工姓名',
`ispartyMember` varchar(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '是否党员',
`Resume` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '简介',
`photo` blob NULL COMMENT '照片',
PRIMARY KEY (`employeeNo`) USING BTREE,
CONSTRAINT `pk6` FOREIGN KEY (`employeeNo`) REFERENCES `employee` (`employeeNo`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of employeeifom
-- ----------------------------
INSERT INTO `employeeifom` VALUES ('e001', '香克斯', '否', '红发香克斯四皇之一', NULL);
INSERT INTO `employeeifom` VALUES ('e002', '路飞', '否', '草帽小子', NULL);
INSERT INTO `employeeifom` VALUES ('e003', '索隆', '否', '绿毛,努力,自律', NULL);
INSERT INTO `employeeifom` VALUES ('e004', '娜美', '是', '航海士', NULL);
INSERT INTO `employeeifom` VALUES ('e005', '山治', '否', '厨师', NULL);
INSERT INTO `employeeifom` VALUES ('e006', '乌索普', '否', '绝境乌索普', NULL);
-- ----------------------------
-- Table structure for inventory
-- ----------------------------
DROP TABLE IF EXISTS `inventory`;
CREATE TABLE `inventory` (
`itemNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '商品编号',
`purchaseDate` datetime NOT NULL COMMENT '进货日期',
`buyingPrice` int NOT NULL COMMENT '进货价',
`itemQuantity` int NOT NULL COMMENT '进货数量',
`supplierNo` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '供应商编号',
PRIMARY KEY (`itemNo`) USING BTREE,
INDEX `supplierNo`(`supplierNo`) USING BTREE,
INDEX `itemNo`(`itemNo`) USING BTREE,
CONSTRAINT `inventory_pk` FOREIGN KEY (`supplierNo`) REFERENCES `supplier` (`supplierNo`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of inventory
-- ----------------------------
INSERT INTO `inventory` VALUES ('i001', '1497-02-24 00:00:00', 10000, 1, 's004');
INSERT INTO `inventory` VALUES ('i002', '1517-01-01 00:00:00', 4000, 1, 's001');
INSERT INTO `inventory` VALUES ('i003', '1520-03-24 00:00:00', 4000, 1, 's002');
INSERT INTO `inventory` VALUES ('i004', '1520-01-01 00:00:00', 1000, 5, 's003');
INSERT INTO `inventory` VALUES ('i005', '1520-08-10 00:00:00', 2000, 1, 's007');
INSERT INTO `inventory` VALUES ('i006', '1515-01-13 00:00:00', 1000, 1, 's005');
INSERT INTO `inventory` VALUES ('i007', '1506-01-18 00:00:00', 3000, 1, 's006');
INSERT INTO `inventory` VALUES ('i008', '2021-09-09 00:00:00', 1000, 10, 's008');
INSERT INTO `inventory` VALUES ('i009', '2021-09-09 00:00:00', 500, 100, 's009');
INSERT INTO `inventory` VALUES ('i010', '1520-03-24 00:00:00', 500, 1010, 's010');
-- ----------------------------
-- Table structure for register
-- ----------------------------
DROP TABLE IF EXISTS `register`;
CREATE TABLE `register` (
`id` char(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`password` char(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of register
-- ----------------------------
INSERT INTO `register` VALUES ('012', '123');
INSERT INTO `register` VALUES ('0123', '0123');
INSERT INTO `register` VALUES ('123', '123');
INSERT INTO `register` VALUES ('123456', '123');
-- ----------------------------
-- Table structure for returnit
-- ----------------------------
DROP TABLE IF EXISTS `returnit`;
CREATE TABLE `returnit` (
`price` int NOT NULL COMMENT '价格',
`returnNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '退货号',
`transactionNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '销售号',
`itemNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '商品编号',
`supplierNo` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '供应商编号',
`reason` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '退货原因',
PRIMARY KEY (`returnNo`) USING BTREE,
INDEX `supplierNo`(`supplierNo`) USING BTREE,
INDEX `transactionNo`(`transactionNo`) USING BTREE,
INDEX `itemNo`(`itemNo`) USING BTREE,
INDEX `price`(`price`) USING BTREE,
CONSTRAINT `returnit_pk1` FOREIGN KEY (`itemNo`) REFERENCES `inventory` (`itemNo`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `returnit_pk2` FOREIGN KEY (`transactionNo`) REFERENCES `salefact` (`transactionNo`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of returnit
-- ----------------------------
-- ----------------------------
-- Table structure for salefact
-- ----------------------------
DROP TABLE IF EXISTS `salefact`;
CREATE TABLE `salefact` (
`transactionNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '销售号',
`itemNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '商品编号',
`saleQuantity` int NOT NULL COMMENT '销售数量',
`saleSumPrice` int NOT NULL COMMENT '销售总金额',
`saleDate` datetime NOT NULL COMMENT '销售日期',
`employeeNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '员工号',
PRIMARY KEY (`transactionNo`) USING BTREE,
INDEX `商品编号`(`itemNo`) USING BTREE,
INDEX `员工编号`(`employeeNo`) USING BTREE,
CONSTRAINT `salefact_pk1` FOREIGN KEY (`itemNo`) REFERENCES `inventory` (`itemNo`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `salefact_pk2` FOREIGN KEY (`employeeNo`) REFERENCES `employee` (`employeeNo`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of salefact
-- ----------------------------
INSERT INTO `salefact` VALUES ('t001', 'i001', 1, 20000, '1497-12-31 00:00:00', 'e007');
INSERT INTO `salefact` VALUES ('t002', 'i002', 1, 10000, '1520-02-21 00:00:00', 'e001');
INSERT INTO `salefact` VALUES ('t003', 'i003', 1, 12000, '1521-03-24 00:00:00', 'e008');
INSERT INTO `salefact` VALUES ('t004', 'i004', 5, 1000, '1521-08-21 00:00:00', 'e002');
INSERT INTO `salefact` VALUES ('t005', 'i005', 1, 1000, '1521-05-21 00:00:00', 'e009');
INSERT INTO `salefact` VALUES ('t006', 'i006', 1, 2000, '1520-02-01 00:00:00', 'e010');
INSERT INTO `salefact` VALUES ('t007', 'i007', 1, 6000, '1521-03-05 00:00:00', 'e011');
-- ----------------------------
-- Table structure for stock
-- ----------------------------
DROP TABLE IF EXISTS `stock`;
CREATE TABLE `stock` (
`buyingPrice` int NOT NULL COMMENT '进货价',
`entryNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '进货号',
`itemNo` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '商品编号',
`quantity` int NOT NULL COMMENT '数量',
PRIMARY KEY (`entryNo`) USING BTREE,
INDEX `stock_pk`(`itemNo`) USING BTREE,
CONSTRAINT `stock_pk` FOREIGN KEY (`itemNo`) REFERENCES `inventory` (`itemNo`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of stock
-- ----------------------------
INSERT INTO `stock` VALUES (10000, 'e001', 'i001', 1);
INSERT INTO `stock` VALUES (4000, 'e002', 'i002', 1);
INSERT INTO `stock` VALUES (4000, 'e003', 'i003', 1);
INSERT INTO `stock` VALUES (1000, 'e004', 'i004', 5);
INSERT INTO `stock` VALUES (2000, 'e005', 'i005', 1);
INSERT INTO `stock` VALUES (1000, 'e006', 'i006', 1);
INSERT INTO `stock` VALUES (3000, 'e007', 'i007', 1);
INSERT INTO `stock` VALUES (1000, 'e008', 'i008', 10);
INSERT INTO `stock` VALUES (500, 'e009', 'i009', 100);
INSERT INTO `stock` VALUES (500, 'e010', 'i010', 10);
-- ----------------------------
-- Table structure for supplier
-- ----------------------------
DROP TABLE IF EXISTS `supplier`;
CREATE TABLE `supplier` (
`supplierName` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '供应商名称',
`supplierNo` char(6) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '供应商编号',
`address` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '供应商地址',
`teleNumber` char(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '联系电话',
PRIMARY KEY (`supplierNo`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of supplier
-- ----------------------------
INSERT INTO `supplier` VALUES ('红发海贼团', 's001', '雷德>佛斯号', '13245672386');
INSERT INTO `supplier` VALUES ('白胡子海贼团', 's002', '莫比迪克号', '19046575123');
INSERT INTO `supplier` VALUES ('草帽海贼团', 's003', '黄金梅利号', '12300230989');
INSERT INTO `supplier` VALUES ('罗杰海贼团', 's004', '奥尔>杰克森号', '11109998888');
INSERT INTO `supplier` VALUES ('巴洛克工作社', 's005', '下大雨', '10010020003');
INSERT INTO `supplier` VALUES ('九蛇海贼团', 's006', '九蛇岛', '10020030004');
INSERT INTO `supplier` VALUES ('黑桃海贼团', 's007', '没提到', '12340056007');
INSERT INTO `supplier` VALUES ('海绵宝宝', 's008', '菠萝屋', '12345678910');
INSERT INTO `supplier` VALUES ('派大星的石头屋', 's009', '深海石头屋', '10010010201');
INSERT INTO `supplier` VALUES ('广西药业', 's010', '广西柳州', '11100001011');
SET FOREIGN_KEY_CHECKS = 1;
实体集包,你有多少张表就需要在底下建多少个类
里面写各种属性、方法,只要不是int型,其余全定义为string型。
连接数据库更新的包,里面写入各种sql语句,同样有多少个表需要更新就要写多少个类
里面写入各种更新的语句及成员 。
一个employeeDao类
package data;
import entity.Employee;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class EmployeeDao {
/** 成员方法1:通过员工编号,获取一个员工对象 */
public static Employee getEmployeeByEmployeeNo(String employeeNo) {
Employee employee = null;
if (employeeNo != null) {
// 构造按照员工编号查询员工的sql查询语句
String sql = "select * from employee where employeeNo='" + employeeNo + "'";
ResultSet rs = BaseDao1.executeQuery(sql);// 执行查询语句,并返回结果集
// 处理查询结果
try {
if (rs.next()) {
// 创建员工对象,保存员工信息
employee = new Employee();
employee.setEmployeeNo(rs.getString("employeeNo"));
employee.setEmployeeName(rs.getString("employeeName"));
employee.setSex(rs.getString("sex"));
employee.setBasePay(rs.getInt("basePay"));
employee.setPost(rs.getString("post"));
employee.setPassword(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return employee;// 返回员工对象
}
/** 成员方法2:通过sql查询语句,获取员工对象列表 */
public static List selectEmployeeList(String sql) {
List list = new ArrayList();// 创建列表
if (sql != null) {
Employee employee = null;
ResultSet rs = BaseDao1.executeQuery(sql);// 执行查询语句,并返回结果集
try {
while (rs.next()) {
// 创建员工对象,保存员工信息
employee = new Employee();
employee.setEmployeeNo(rs.getString("employeeNo"));
employee.setEmployeeName(rs.getString("employeeName"));
employee.setSex(rs.getString("sex"));
employee.setBasePay(rs.getInt("basePay"));
employee.setPost(rs.getString("post"));
employee.setPassword(rs.getString("password"));
list.add(employee);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return list;// 返回员工列表
}
/** 成员方法3:获取所有员工对象列表 */
public static List selectEmployeeList() {
// 构造查询所有员工的sql查询语句
String sql = "select * from employee";
return selectEmployeeList(sql);// 返回员工列表
}
/** 成员方法4:按照字符属性查询条件,获取员工对象列表,重载方法 */
public static List selectEmployeeList(String field, String value) {
// 构造默认的sql查询语句
String sql = "select * from employee";
if ((value != null & value.length() > 0) && field != null) {
// 构造按照字符属性查询员工的sql查询语句,采用模糊查询
sql = "select * from employee where " + field + " like '%" + value + "%'";
}
return selectEmployeeList(sql);// 返回员工列表
}
/** 成员方法5:按照整型查询条件,获取员工对象列表,重载方法 */
public static List selectEmployeeList(String field, int value) {
// 构造默认的sql查询语句
String sql = "select * from employee";
if (field != null && value != 0) {
// 构造按照整型属性查询员工的sql查询语句,采用区间查询
sql = "select * from employee where " + field + "=" +value;
}
return selectEmployeeList(sql);// 返回员工列表
}
/** 成员方法6:添加新员工 */
public static int insertEmployee(Employee Employee) {
if (Employee != null) {
// 获取员工信息
String EmployeeNo = Employee.getEmployeeNo();
String EmployeeName = Employee.getEmployeeName();
String sex = Employee.getSex();
String post = Employee.getPost();
String password = Employee.getPassword();
int basePay = Employee.getBasePay();
// 构造添加员工的sql更新语句
String sql = "insert into employee values('" + EmployeeNo + "','" + EmployeeName + "','" + sex + "'," +basePay+
", '"+ post + "','"+ password + "' )";
return BaseDao1.executeUpdate(sql);// 执行更新语句
} else {
return -1;
}
}
/** 成员方法7:修改指定员工信息 */
public static int upDateEmployee(Employee Employee) {
if (Employee != null) {
// 获取员工信息
String EmployeeNo = Employee.getEmployeeNo();
String EmployeeName = Employee.getEmployeeName();
String sex = Employee.getSex();
String post = Employee.getPost();
String password = Employee.getPassword();
int basePay = Employee.getBasePay();
// 构造修改员工的sql更新语句
String sql = "update employee set employeeName='" + EmployeeName + "',sex='" + sex + "', post='" + post
+"',password ='"+password + "',basePay=" + basePay + " where employeeNo='" + EmployeeNo + "'";
return BaseDao1.executeUpdate(sql);// 执行更新语句
} else {
return -1;
}
}
/** 成员方法8:删除指定员工信息 */
public static int deleteEmployee(String EmployeeNo) {
// 构造删除员工的sql更新语句
String sql = "delete from employee where employeeNo='" + EmployeeNo + "'";
return BaseDao1.executeUpdate(sql);// 执行更新语句
}
/** 成员方法9:清空所有员工信息 */
public static void emptyEmployee() {
// 构造清空员工的sql更新语句
String sql = "delete from employee";
BaseDao1.executeUpdate(sql);// 执行更新语句
}
}
里面写入java swing登录窗口的方法和连接数据库的方法
一个handleLogin类
package Login;
import windows.MainFrame;
import java.sql.*;
import javax.swing.JOptionPane;
public class HandleLogin {
Connection con;//与特定数据库的连接(会话)。
PreparedStatement preSql;//PreparedStatement是一个接口,preSql是一个接口引用,接口不能new对象,所以借用下方prepareStatement方法
ResultSet rs;//表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。
public HandleLogin(){
con = GetDBConnection.connectDB("user","root","123456");//链接数据库
}
public Login queryVerify(Login loginModel) {
String id = loginModel.getID();
String pw = loginModel.getPassword();
String sqlStr ="select id,password from register where "+
"id = ? and password = ?";
try {
preSql = con.prepareStatement(sqlStr);//创建一个 PreparedStatement对象,用于将参数化的SQL语句发送到数据库
preSql.setString(1,id);//将指定的参数设置为给定的Java String值。1代表sql语句中第一个?号,后面的参数代表给?赋的值
preSql.setString(2,pw);//同上
rs = preSql.executeQuery();
if(rs.next()==true) {//最初,光标位于第一行之前,next方法将光标移动到下一行,并且由于在ResultSet对象中没有更多行时返回false
loginModel.setLoginSuccess(true);//第一行有数据代表该ID和密码都存在,因此登录成功,并将loginSuccess设为true
JOptionPane.showMessageDialog(null,"登录成功",//弹出一个标准对话框,提示用户获取值或通知他们某些东西。
"恭喜",JOptionPane.WARNING_MESSAGE);
MainFrame mainFrame = new MainFrame();
}
else {
loginModel.setLoginSuccess(false);//第一行不存在代表ID或密码错误,并将loginSuccess设为false
JOptionPane.showMessageDialog(null,"登录失败",//弹出一个标准对话框,提示用户获取值或通知他们某些东西。
"登录失败,重新登录",JOptionPane.WARNING_MESSAGE);
}
con.close();
}
catch(SQLException e) {}
return loginModel;
}
}
这个包建完后已经可以实现登录和注册了
这个包最复杂了,包含了查询和更新调用sql语句的方法,query是查询,upkeep是更新
一个employeeQuery类
package windows;
import data.EmployeeDao;
import entity.Employee;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.util.List;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class EmployeeQuery extends PublicJFrame{
/** 成员变量: */
JLabel lb_query;// 声明查询标签
JTextField tf_query;// 声明查询条件文本框
JComboBox cb_query;// 声明查询栏目组合框
JButton bt_query;// 声明查询按钮
private JTable table;// 声明表格
// 调用方法,获取所有员工列表,并保存
private List list = EmployeeDao.selectEmployeeList();
// 创建员工表栏目名称字符串
private String[] tb_heads = { "员工编号 ", "员工名称 ", "性别", "职务", "基本工资" };
// 员工表栏目名称对应的表字段
private String[] fields = { "EmployeeNo", "EmployeeName", "sex", "post","basePay" };
// 创建员工表格模型
private DefaultTableModel model = new DefaultTableModel(
new Object[][] {}, tb_heads);
private String field = "EmployeeNo";// 设置默认的当前的查询字段
String valueStr = " ";// 设置字符串类型查询字段的初始值
int valueInt = 0;// 设置整型类型查询字段的初始值
double valueDouble = 0.0;// 设置实型数据查询字段的初始值
/** 构造方法:初始化界面 */
EmployeeQuery() {
this.setTitle("--员工查询-- ");
this.setSize(1200, 500);
JPanel queryPanel = createQueryPanel();
this.add(queryPanel, BorderLayout.NORTH);
JPanel tablePanel = createTablePanel();
this.add(tablePanel, BorderLayout.CENTER);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
/** 成员方法1:创建查询面板的方法 */
private JPanel createQueryPanel() {
JPanel queryPanel = new JPanel();
queryPanel.setOpaque(false);//允许底层像素出现
queryPanel.setLayout(new GridBagLayout());
((GridBagLayout) queryPanel
.getLayout()).columnWidths = new int[] { 0, 100, 200,
120, 80, 80, 0 };
((GridBagLayout) queryPanel
.getLayout()).columnWeights = new double[] { 0.5, 0.0,
0.0, 0.0, 0.0, 0.0, 0.5 };
lb_query = new JLabel("请输入员工编号");
queryPanel.add(lb_query,
new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.EAST,
GridBagConstraints.BOTH,
new Insets(10, 10, 10, 10), 0, 0));
tf_query = new JTextField();
tf_query.requestFocus();
// 为查询条件文本框,添加回车键事件监听器
tf_query.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent arg0) {
if (arg0.getKeyChar() == '\n') {
// 调用“查询”按钮事件响应方法
bn_query_actionPerformed();
}
}
});
queryPanel.add(tf_query,
new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(10, 0, 10, 10), 0, 0));
cb_query = new JComboBox(tb_heads);
// 为查询栏目组合框,添加条目事件监听器
cb_query.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
// 调用“查询栏目”组合框栏目状态被改变事件响应方法
cb_query_itemStateChanged(arg0);
}
});
queryPanel.add(cb_query,
new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(10, 0, 10, 10), 0, 0));
bt_query = new JButton("查询 ");
// 为“查询”按钮,添加单击事件监听器
bt_query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// 调用“查询”按钮事件响应方法
bn_query_actionPerformed();
}
});
queryPanel.add(bt_query,
new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(10, 0, 10, 10), 0, 0));
JButton bn_close = new JButton("关闭");
// 为“关闭”按钮,添加单击事件监听器
bn_close.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();// 关闭界面
}
});
queryPanel.add(bn_close,
new GridBagConstraints(5, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(10, 0, 10, 10), 0, 0));
return queryPanel;// 返回“查询面板”
}
/** 成员方法2:创建表格面板的方法 */
private JPanel createTablePanel() {
// 创建表格面板,采用边界布局
JPanel tablePanel = new JPanel(new BorderLayout(5, 5));
JScrollPane scrollPane = new JScrollPane();// 创建滚动面板
tablePanel.add(scrollPane);// 将表格面板添加到滚动面板上
table = new JTable(model);// 创建表格,并采用指定的表格模型
addRowToModel(list);
scrollPane.setViewportView(table);
return tablePanel;
}
/** 成员方法3:向表格模型,添加员工列表 */
private void addRowToModel(List list) {
model.setRowCount(0);// 清除表格模型中的数据
for (int i = 0; i < list.size(); i++) {
Employee Employee = list.get(i);
model.addRow(new Object[] { Employee.getEmployeeNo(),
Employee.getEmployeeName(),
Employee.getSex(),
Employee.getPost(),
Employee.getBasePay()});
}
}
/** 成员方法4:查询栏目组合框状态被改变的事件响应方法 */
private void cb_query_itemStateChanged(ItemEvent arg0) {
if (arg0.getStateChange() == ItemEvent.SELECTED) {
// 根据选择改变标签显示内容
lb_query.setText("请输入" + (String) arg0.getItem() + ":");
tf_query.setText("");
// 获取组合框中选定的栏目名称
for (int i = 0; i < tb_heads.length; i++) {
if (arg0.getItem().equals(tb_heads[i])) {
field = fields[i];// 保存对应的字段,作为当前被选定的字段
}
}
list = EmployeeDao.selectEmployeeList();// 调用方法,获取所有商品列表
addRowToModel(list);// 调用方法:将商品列表,添加到表格模型中
tf_query.requestFocus();// 设置查询条件文本框,获得焦点
}
}
/** 成员方法5:“修改”按钮单击事件响应方法 */
private void bt_modify_actionPerformed() {
switch (field) {
case "EmployeeNo":
case "EmployeeName":
case "sex":
case "post":
// 获取字符串类型的查询条件
valueStr = tf_query.getText();
// 调用按照(字段名、字符串字段值)查询员工列表的方法,查询员工信息
list = EmployeeDao.selectEmployeeList(field, valueStr);
break;
case "basePay":
// 获取整型类型的查询条件
if (tf_query.getText().equals("")) {
tf_query.setText("0");
}
valueInt = new Integer(tf_query.getText()).intValue();
// 调用按照(字段名、整型字段值)查询员工列表的方法,查询员工信息
list = EmployeeDao.selectEmployeeList(field, valueInt);
break;
}
addRowToModel(list);// 将查询获取的员工列表,添加到表格模型中
}
/**
* 成员方法6“查询”按钮单击事件响应方法
*/
private void bn_query_actionPerformed() {
switch (field) {
case "EmployeeNo":
case "EmployeeName":
case "sex":
case "post":
case "password":
// 获取字符串类型的查询条件
valueStr = tf_query.getText();
// 调用按照(字段名、字符串字段值)查询员工列表的方法,查询员工信息
list = EmployeeDao.selectEmployeeList(field, valueStr);
break;
case "basePay":
// 获取整型类型的查询条件
if (tf_query.getText().equals("")) {
tf_query.setText("0");
}
valueInt = new Integer(tf_query.getText()).intValue();
// 调用按照(字段名、整型字段值)查询员工列表的方法,查询员工信息
list = EmployeeDao.selectEmployeeList(field, valueInt);
break;
}
addRowToModel(list);// 将查询获取的员工列表,添加到表格模型中
}
/** 测试方法: */
public static void main(String[] args) {
new EmployeeQuery();
}
}
测试下
一个employeeUpkeep类
package windows;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import CommoditySales.Constant;
import data.EmployeeDao;
import entity.Employee;
public class EmployeeUpKeep extends PublicJFrame {
/** 成员变量 */
private JTextField tf_id;// 声明员工编号文本框
private JTextField tf_name;// 声明员工名文本框
private JComboBox cb_type;// 声明员工类型组合框
private JTextField tf_basePay;// 声明基本工资文本框
private JTextField tf_post;// 声明职务文本框
private JTextField tf_password;// 声明密码文本框
// 声明添加、修改、取消、关闭、删除、清空按钮
private JButton jb_insert, jb_update, jb_cancel, jb_close,
jb_delete, jb_empty;
private JTable table;// 声明表格
// 创建员工表格模型
private DefaultTableModel model = new DefaultTableModel(
new Object[][] {}, new String[] { "员工编号", "员工姓名", "性别", "职务","基本工资","密码"});
private List list = EmployeeDao.selectEmployeeList();// 创建所有员工列表,并保存员工对象
/** 构造方法:用于初始化界面 */
EmployeeUpKeep() {
// 界面设置
this.setTitle("--员工信息维护--");
setBounds(220, 100, 1300, 650);
this.setResizable(false);
this.setLocationRelativeTo(null);
// 创建一个对话面板,其上放置一个外部分割面板,并将它设置为框架的内容面板
JPanel dialogPane = new JPanel();
dialogPane.setBorder(new EmptyBorder(5, 5, 5, 5));
dialogPane.setBackground(new Color(198, 236, 253));
dialogPane.setLayout(new BorderLayout());
setContentPane(dialogPane);
// 创建一个外部分割面板,水平左右放置组件。其中,左测放置一个内部分割面板,右测放置一个表格面板
JSplitPane outerPane = new JSplitPane();// 创建外部分割面板
outerPane.setOpaque(false);// 设置外部分割面板透明
outerPane.setResizeWeight(0.1);// 设置外部分割面板宽度分配权限
outerPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);//设置外部分割面板水平分配
outerPane.setOneTouchExpandable(true);//设置外部分割面板可以有一个触发扩展
dialogPane.add(outerPane, BorderLayout.CENTER);
// 创建一个内部分割面板,垂直上下放置组件。其中,上方放置一个数据面板,下方放置一个按钮面板
JSplitPane innerPane = new JSplitPane();// 创建内部分割面板
innerPane.setOpaque(false);
// 为一个内部分割面板设置边框说明
innerPane.setBorder(new TitledBorder(
new EtchedBorder(EtchedBorder.LOWERED, null, null),
"员工信息维护", TitledBorder.LEADING, TitledBorder.TOP,
new Font("微软雅黑", Font.PLAIN, 14),
new Color(59, 59, 59)));
innerPane.setResizeWeight(0.8);
innerPane.setOrientation(JSplitPane.VERTICAL_SPLIT);//设置内部分割面板为垂直分配
outerPane.setLeftComponent(innerPane);// 将内部分割面板设置在外部分割面板的左侧
JPanel dataPanel = createDataPanel();// 调用创建数据面板方法,创建数据面板
innerPane.setLeftComponent(dataPanel);// 将数据面板,设置在内部分割面板的左侧
JPanel buttonPanel = createButtonPanel();// 调用创建按钮面板方法,创建按钮面板
innerPane.setRightComponent(buttonPanel);// 将按钮面板设置在内部分割面板的下方
JPanel tablePanel = createTablePanel();// 调用创建表格面板方法,创建表格面板
outerPane.setRightComponent(tablePanel);// 将表格面板,设置在外部分割面板的右侧
}
/** 成员方法1:用于创建“数据面板"的方法 */
private JPanel createDataPanel() {
JPanel dataPanel = new JPanel(null);
dataPanel.setBorder(new EmptyBorder(5, 5, 5, 10));
dataPanel.setOpaque(false);
JLabel jl_id = new JLabel("员工编号:");
jl_id.setBounds(50, 20, 100, 25);
dataPanel.add(jl_id);
tf_id = new JTextField();
tf_id.setBounds(140, 20, 170, 25);
tf_id.setToolTipText("必须输入员工编号");
tf_id.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(FocusEvent e) {
tf_id_focusLost();// 调用员工编号文本框加失去焦点事件响应方法
}
});
dataPanel.add(tf_id);
JLabel jl_name = new JLabel("员工姓名:");
jl_name.setBounds(50, 60, 100, 25);
dataPanel.add(jl_name);
tf_name = new JTextField(10);
tf_name.setBounds(140, 60, 170, 25);
tf_name.setToolTipText("必须输入员工姓名");
dataPanel.add(tf_name);
tf_name.addFocusListener(new FocusAdapter() {//为员工名文本框添加失去焦点监听器
@Override
public void focusLost(FocusEvent e) {
tf_name_focusLost();// 调用员工名文本框加失去焦点事件响应方法
}
});
JLabel jl_type = new JLabel("性别:");
jl_type.setBounds(50, 100, 150, 25);
dataPanel.add(jl_type);
cb_type = new JComboBox(Constant.SEX);
cb_type.setBounds(140, 100, 170, 25);
dataPanel.add(cb_type);
JLabel jl_author = new JLabel("基本工资:");
jl_author.setBounds(50, 140, 150, 25);
dataPanel.add(jl_author);
tf_basePay = new JTextField(10);
tf_basePay.setBounds(140, 140, 170, 25);
dataPanel.add(tf_basePay);
JLabel jl_translator = new JLabel("职务:");
jl_translator.setBounds(50, 180, 150, 25);
dataPanel.add(jl_translator);
tf_post = new JTextField(10);
tf_post.setBounds(140, 180, 170, 25);
dataPanel.add(tf_post);
JLabel jl_publisher = new JLabel("密码:");
jl_publisher.setBounds(50, 220, 150, 25);
dataPanel.add(jl_publisher);
tf_password = new JTextField(10);
tf_password.setBounds(140, 220, 170, 25);
dataPanel.add(tf_password);
return dataPanel;
}
/** 成员方法2:用于创建"按钮面板"的方法 */
private JPanel createButtonPanel() {
JPanel buttonPanel = new JPanel(new GridBagLayout());//创建按钮面板,用网格包布局
buttonPanel.setOpaque(false);
((GridBagLayout) buttonPanel
.getLayout()).columnWidths = new int[] { 0, 60, 60,
60, 60, 60, 0 };// 设置网格包布局各列宽度
((GridBagLayout) buttonPanel
.getLayout()).columnWeights = new double[] { 0.5, 0.0,
0.0, 0.0, 0.0, 0.0, 0.5 };// 设置网格包布局各列的宽度权限
jb_insert = new JButton("添加");
jb_insert.addActionListener(new ActionListener() {//为添加按钮添加单击事件监听器
@Override
public void actionPerformed(ActionEvent e) {
sb_insert_actionPerformed();// 调用添加按钮事件响应方法,添加员工
}
});
buttonPanel.add(jb_insert,
new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
jb_update = new JButton("修改");
jb_update.addActionListener(new ActionListener() {//为修改按钮添加单击事件监听器
@Override
public void actionPerformed(ActionEvent e) {
jb_update_actionPerformed();// 调用修改按钮事件响应方法,修改员工信息
}
});
buttonPanel.add(jb_update,
new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
jb_delete = new JButton("删除");
jb_delete.addActionListener(new ActionListener() {// 为删除按钮添加单击事件监听器
@Override
public void actionPerformed(ActionEvent e) {
jb_delete_actionPerformed();// 调用删除按钮事件响应方法,删除员工对象
}
});
buttonPanel.add(jb_delete,
new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
jb_cancel = new JButton("取消");
jb_cancel.addActionListener(new ActionListener() {//为取消按钮添加单击事件监听器
@Override
public void actionPerformed(ActionEvent e) {
del_content();// 调用删除内容方法,清除数据面板中的数据
}
});
buttonPanel.add(jb_cancel,
new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
jb_close = new JButton("关闭");
jb_close.addActionListener(new ActionListener() {//为关闭按钮添加单击事件监听器
@Override
public void actionPerformed(ActionEvent e) {
dispose();// 关闭员工维护界面
}
});
buttonPanel.add(jb_close,
new GridBagConstraints(5, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 5), 0, 0));
jb_empty = new JButton("清空所有员工");
jb_empty.addActionListener(new ActionListener() {//为清空按钮添加单击事件监听器
@Override
public void actionPerformed(ActionEvent e) {
jb_empty_actionPerformed();// 调用清空按钮事件响应方法,清空所有员工
}
});
buttonPanel.add(jb_empty,
new GridBagConstraints(2, 1, 3, 1, 0.0, 0.0,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(25, 0, 0, 0), 0, 0));
return buttonPanel;
}
/** 成员方法3:用于创建表格面板的方法 */
private JPanel createTablePanel() {
// 创建表格面板,采用边界布局
JPanel tablePanel = new JPanel(new BorderLayout(5, 5));
JScrollPane scrollPane = new JScrollPane();// 创建滚动面板
tablePanel.add(scrollPane);// 将表格面板添加到滚动面板上
table = new JTable(model);// 创建表格,并采用指定的表格模型
for (int i = 0; i < list.size(); i++) {
Employee Employee = list.get(i);
model.addRow(new Object[] { Employee.getEmployeeNo(),
Employee.getEmployeeName(),
Employee.getSex(),
Employee.getPost(),
Employee.getBasePay(),
Employee.getPassword()
});
}
table.addMouseListener(new MouseAdapter() {// 为表格添加鼠标事件监听器
@Override
public void mouseClicked(MouseEvent e) {
// 调用表格鼠标单击事件响应方法,向左侧数据面板显示选中员工
table_mouseClicked();
}
});
scrollPane.setViewportView(table);
return tablePanel;
}
/** 成员方法4:用于清除数据面板中的数据 */
public void del_content() {
tf_id.setText("");
tf_name.setText("");
cb_type.setSelectedIndex(0);
tf_basePay.setText("");
tf_post.setText("");
tf_password.setText("");
tf_id.setEditable(true);
}
/** 成员方法5:员工编号文本框失去焦点事件响应具体方法 */
public void tf_id_focusLost() {
// 通过员工编号,获取员工对象
Employee Employee = EmployeeDao.getEmployeeByEmployeeNo(tf_id.getText().trim());
if (Employee != null) {// 判断员工对象是否存在,若存在,清除数据,重新输入员工编号
JOptionPane.showMessageDialog(null,
"员工编号已经存在,请重新输入员工编号!");
del_content(); // 清除数据面板中的数据
}
}
/** 成员方法6:员工名文本框失去焦点事件响应具体方法 */
public void tf_name_focusLost() {
Employee Employee = EmployeeDao.getEmployeeByEmployeeNo(tf_name.getText().trim());
if (Employee != null) {
JOptionPane.showMessageDialog(null, "员工名已经存在,请重新输入员工名!");
tf_name.setText("");
}
}
/** 成员方法7:用于更新表格数据 */
public void refresh() {
model.setRowCount(0);// 清除表格模型中的数据
list = EmployeeDao.selectEmployeeList();// 获取员工对象列表
for (int i = 0; i < list.size(); i++) {// 重新项表格模型中添加数据
Employee Employee = list.get(i);
model.addRow(new Object[] { Employee.getEmployeeNo(),
Employee.getEmployeeName(),
Employee.getSex(),
Employee.getPost(),
Employee.getBasePay(),
Employee.getPassword()
});
}
del_content();// 清除数据面板中的数据
}
/** 成员方法8:表格鼠标单击事件响应方法 */
public void table_mouseClicked() {
// 获取选中员工的信息
Employee Employee_old = list.get(table.getSelectedRow());
// 在数据面板中设置对应的员工数据
tf_id.setText(Employee_old.getEmployeeNo());
tf_name.setText(Employee_old.getEmployeeName());
cb_type.setSelectedItem(Employee_old.getSex());
tf_basePay.setText(String.valueOf(Employee_old.getBasePay()));
tf_password.setText(Employee_old.getPassword());
tf_post.setText(Employee_old.getPost());
tf_id.setEditable(false);// 设置读者员工文本框不可编辑
}
/** 成员方法9:添加新员工方法 */
public void sb_insert_actionPerformed() {
Employee Employee = new Employee();// 创建一个员工对象
// 设置员工对象的属性值
Employee.setEmployeeNo(tf_id.getText().trim());
Employee.setEmployeeName(tf_name.getText().trim());
Employee.setSex(cb_type.getSelectedItem().toString());
Employee.setBasePay(new Integer(tf_basePay.getText().trim()));
Employee.setPassword(tf_password.getText().trim());
Employee.setPost(tf_post.getText().trim());
// 判断员工名或员工编号不允许为空?若空,则给出提示,并返回
if (tf_name.getText().trim().equals("")
|| tf_id.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "员工信息不能为空!");
return;
} else {// 若不空,则执行添加员工对象操作
int i = EmployeeDao.insertEmployee(Employee);// 执行添加操作
if (i == 1) {// 判断添加操作是否成功?若成功,则向表格模型添加这条数据
model.addRow(new Object[] { Employee.getEmployeeNo(),
Employee.getEmployeeName(),
Employee.getSex(),
Employee.getPost(),
Employee.getBasePay(),
Employee.getPassword()
});
refresh();
}
del_content();
}
}
/** 成员方法10:修改员工信息按钮事件响应方法 */
public void jb_update_actionPerformed() {
Employee Employee = new Employee();// 创建一个员工对象
Employee.setEmployeeNo(tf_id.getText().trim());
Employee.setEmployeeName(tf_name.getText().trim());
Employee.setSex(cb_type.getSelectedItem().toString());
Employee.setBasePay(new Integer(tf_basePay.getText().trim()));
Employee.setPassword(tf_password.getText().trim());
Employee.setPost(tf_post.getText().trim());
if (tf_name.getText().trim().equals("")
|| tf_id.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "员工信息不能为空!");
return;
} else {// 若不空,则执行添加员工对象操作
int i = EmployeeDao.upDateEmployee(Employee);
if (i == 1) {
JOptionPane.showMessageDialog(null, "员工信息修改成功!");
}
refresh();// 更新表格内容
del_content();
}
}
/** 成员方法11:删除员工 信息 */
public void jb_delete_actionPerformed() {
String id = tf_id.getText().trim(); // 获取用户编号
// 调用确认对话框,询问是否删除员工?
int m = JOptionPane.showConfirmDialog(null, "你确认要删除这条员工信息吗?",
"删除员工信息", JOptionPane.YES_NO_OPTION);
if (m == JOptionPane.YES_OPTION) {// 如果确定,则执行删除操作
if (EmployeeDao.deleteEmployee(id) == 1) {// 执行用户删除操作
refresh();// 更新表格内容
JOptionPane.showMessageDialog(null, "员工信息删除成功!");
}
del_content();// 清除数据面板内容
}
}
/** 成员方法12:删除所有员工信息 */
public void jb_empty_actionPerformed() {
int m = JOptionPane.showConfirmDialog(null, "你确认要删除所有员工信息吗?",
"清空员工信息", JOptionPane.YES_NO_OPTION);
if (m == JOptionPane.YES_OPTION) {
int n = JOptionPane.showConfirmDialog(null,
"真的确认清空所有员工数据吗?", "清空员工信息再次确认",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
EmployeeDao.emptyEmployee();
refresh();// 更新表格内容
del_content();// 清除数据面板内容
}
}
}
// 测试方法:用于员工维护的测试
public static void main(String[] args) {
new EmployeeUpKeep();
}
}
测试
最后一个删除所有员工就不试了哈,好难重新插回去的。。
一个mainfram类
package windows;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import Login.LoginView;
import data.BaseDao1;
public class MainFrame extends PublicJFrame {
/** 成员变量 */
private JMenuItem mi_inventory_upkeep;// 声明库存维护菜单项
private JMenuItem mi_stock_upkeep;// 声明进货维护菜单项
private JMenuItem mi_supplier_upkeep;// 声明员工维护菜单项
private JMenuItem mi_item_upkeep;// 声明商品维护菜单项
private JMenuItem mi_employee_upkeep;//声明员工维护菜单
private JMenuItem mi_item_query;// 声明商品查询菜单项
private JMenuItem mi_employee_query;// 声明员工查询菜单项
private JMenuItem mi_inventory_query;// 声明库存查询菜单项
private JMenuItem mi_sale_query;// 声明销售查询菜单项
private JMenuItem mi_stock_query;// 声明进货查询菜单项
private JMenuItem mi_supplier_query;// 声明供货商查询菜单项
private JMenuItem mi_exit;// 声明退出系统菜单项
private JButton bt_employee_upkeep;// 声明读者维护按钮
private JButton bt_book_upkeep;// 声明商品维护按钮
private JButton bt_reader_query;// 声明读者查询按钮
private JButton bt_book_query;// 声明商品查询按钮
private JButton bt_exit;// 声明退出系统按钮
/** 构造方法:系统主界面初始化方法 */
public MainFrame() {
//第1步:系统主界面设置
this.setTitle("商品销售管理系统");// 设置主界面标题
this.setBounds(520,0,1000,1050);
//this.setExtendedState(Frame.MAXIMIZED_BOTH);// 设置主界面窗口最大化
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel(){//设置背景图片
@Override
public void paint(Graphics g){
ImageIcon icon = new ImageIcon("images/5.png");
Image image = icon.getImage();
g.drawImage(image,0,0,null);
}
};
this.add(contentPane);
this.setVisible(true);
//第2步:创建、设置菜单栏。先调用创建系统菜单栏方法,创建系统菜单栏,再在主界面设置菜单栏
this.setJMenuBar(createMenuBar());
//第3步: 创建、设置工具栏。先调用创建系统工具栏方法,创建系统工具栏,再将工具栏添加到界面的上方
this.add(createToolBar(),BorderLayout.NORTH);
this.setVisible(true);
this.setPurView(LoginView.a);
}
/** 成员方法1:创建系统菜单栏方法 */
private JMenuBar createMenuBar() {
// 第1步:创建菜单栏对象
JMenuBar menuBar = new JMenuBar();
//第2步:创建菜单。如基础维护菜单,又包含用户维护、读者维护、商品维护菜单项
JMenu menu_upkeep = new JMenu("基础维护"); // 创建"基础维护"菜单
//第3步:创建菜单项,并为菜单项设置单击事件监听器
mi_supplier_upkeep = new JMenuItem("供应商信息维护"); // 创建“用户维护”菜单项
mi_supplier_upkeep.addActionListener(new ActionListener() {//添加单击事件监听器
@Override
public void actionPerformed(ActionEvent e) {
supplier_upkeep_actionPerformed();// 调用“供应商信息维护”响应方法
}
});
//第4步:将菜单项添加到菜单上
menu_upkeep.add(mi_supplier_upkeep);// 将“供应商信息维护”菜单项添加到“基础维护”菜单
mi_item_upkeep = new JMenuItem("商品信息维护"); // 创建"商品维护"菜单项
mi_item_upkeep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
item_upkeep_actionPerformed();// 调用"商品维护"响应方法
}
});
menu_upkeep.add(mi_item_upkeep);
mi_stock_upkeep = new JMenuItem("进货信息维护"); // 创建"商品维护"菜单项
mi_stock_upkeep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stock_upkeep_actionPerformed();// 调用"商品维护"响应方法
}
});
menu_upkeep.add(mi_stock_upkeep);
mi_inventory_upkeep = new JMenuItem("库存信息维护"); // 创建"商品维护"菜单项
mi_inventory_upkeep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
inventory_upkeep_actionPerformed();// 调用"商品维护"响应方法
}
});
menu_upkeep.add(mi_inventory_upkeep);
mi_employee_upkeep = new JMenuItem("员工信息维护"); // 创建"商品维护"菜单项
mi_employee_upkeep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
employee_upkeep_actionPerformed();// 调用"商品维护"响应方法
}
});
menu_upkeep.add(mi_employee_upkeep);
//第5步:将菜单添加到菜单栏上
menuBar.add(menu_upkeep);// 将"基础维护"菜单添加到系统菜单栏
JMenu menu_query = new JMenu("查询统计"); // 创建"查询统计"菜单
mi_item_query = new JMenuItem("商品查询"); // 创建"商品查询"菜单项
mi_item_query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
item_query_actionPerformed();// 调用"商品查询"响应方法
}
});
menu_query.add(mi_item_query);
mi_employee_query = new JMenuItem("员工查询"); // 创建"读者查询"菜单项
mi_employee_query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
employee_query_actionPerformed();// 调用"读者查询"响应方法
}
});
menu_query.add(mi_employee_query);
mi_inventory_query = new JMenuItem("库存查询"); // 创建"读者查询"菜单项
mi_inventory_query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
inventory_query_actionPerformed();// 调用"读者查询"响应方法
}
});
menu_query.add(mi_inventory_query);
mi_sale_query = new JMenuItem("销售查询"); // 创建"销售查询"菜单项
mi_sale_query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sale_query_actionPerformed();// 调用"读者查询"响应方法
}
});
menu_query.add(mi_sale_query);
mi_stock_query = new JMenuItem("进货查询"); // 创建"进货查询"菜单项
mi_stock_query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stock_query_actionPerformed();// 调用"读者查询"响应方法
}
});
menu_query.add(mi_stock_query);
mi_supplier_query = new JMenuItem("供货商查询"); // 创建"供货商查询"菜单项
mi_supplier_query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
supplier_query_actionPerformed();// 调用"读者查询"响应方法
}
});
menu_query.add(mi_supplier_query);
menuBar.add(menu_query);//查询菜单栏
JMenu menu_management = new JMenu("系统管理");// 创建"系统管理"菜单
mi_exit = new JMenuItem("退出系统"); // 创建"退出系统"菜单项
mi_exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exit_actionPerformed();// 调用"退出系统"响应方法
}
});
menu_management.add(mi_exit);
menuBar.add(menu_management);
//第6步:返回菜单栏。
return menuBar;
}
/** 成员方法2: 创建工具栏方法 */
private JToolBar createToolBar() {
//第1步:创建、设置工具栏
JToolBar toolBar = new JToolBar("商品管理系统工具栏");// 创建工具栏对象
toolBar.setFloatable(false); // 设置工具栏不可浮动
// 设置工具栏边框导角方式
toolBar.setBorder(new BevelBorder(BevelBorder.LOWERED));
//第2步: 创建、设置工具栏上的快捷按钮
bt_employee_upkeep = new JButton("员工维护");// 创建"读者维护"按钮
// 创建"读者维护"图标
ImageIcon icon_reader_upkeep = new ImageIcon("images/reader_upkeep.png");
bt_employee_upkeep.setIcon(icon_reader_upkeep);// 为"读者维护"按钮设置图标
bt_employee_upkeep.setToolTipText("员工维护");// 为"读者维护"按钮设置提示
// 将"读者维护"按钮注册单击事件监听器
bt_employee_upkeep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
employee_upkeep_actionPerformed();// 调用"读者维护’响应方法
}
});
//第3步:将按钮添加到工具栏上
toolBar.add(bt_employee_upkeep);// 将“读者维护”按钮添加到工具栏
bt_book_upkeep = new JButton("商品信息维护");
ImageIcon icon_book_upkeep = new ImageIcon("images/book_upkeep.png");
bt_book_upkeep.setIcon(icon_book_upkeep);
toolBar.add(bt_book_upkeep);
bt_book_upkeep.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
item_upkeep_actionPerformed();// 调用“商品维护”响应方法
}
});
bt_reader_query = new JButton("员工查询");
ImageIcon icon_reader_query = new ImageIcon("images/reader_query.png");
bt_reader_query.setIcon(icon_reader_query);
toolBar.add(bt_reader_query);
bt_reader_query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
employee_query_actionPerformed();// 调用“读者查询”响应方法
}
});
bt_book_query = new JButton("商品查询");
ImageIcon icon_book_query = new ImageIcon("images/book_query.png");
bt_book_query.setIcon(icon_book_query);
toolBar.add(bt_book_query);
bt_book_query.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
item_query_actionPerformed();// 调用“商品查询”响应方法
}
});
bt_exit = new JButton("退出系统");
ImageIcon icon_exit = new ImageIcon("images/exit.png");
bt_exit.setIcon(icon_exit);
toolBar.add(bt_exit);
bt_exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exit_actionPerformed();// 调用“退出系统"响应方法
}
});
//第4步:返回工具栏
return toolBar;
}
/** 成员方法3:设置系统功能使用权限的方法 */
void setPurView(String purView) {
switch (purView) {
case "0"://用户权限为0,为操作员
mi_supplier_upkeep.setEnabled(false);//设置“用户维护”菜单项不可用
mi_item_upkeep.setEnabled(false);//设置“用户维护”菜单项不可用
mi_stock_upkeep.setEnabled(false);//设置“用户维护”菜单项不可用
mi_inventory_upkeep.setEnabled(false);//设置“用户维护”菜单项不可用
mi_item_query.setEnabled(false);// 声明商品查询菜单项
mi_employee_query.setEnabled(false);// 声明员工查询菜单项
mi_inventory_query.setEnabled(false);// 声明库存查询菜单项
mi_sale_query.setEnabled(false);// 声明销售查询菜单项
mi_stock_query.setEnabled(false);// 声明进货查询菜单项
mi_supplier_query.setEnabled(false);// 声明供货商查询菜单项
bt_book_upkeep.setEnabled(false);
bt_employee_upkeep.setEnabled(false);
mi_employee_upkeep.setEnabled(false);
break;
case "1"://用户权限为1,为管理员
break;
default://用户权限为其他,为一般访客
bt_reader_query.setEnabled(false);
mi_supplier_upkeep.setEnabled(false);//设置“用户维护”菜单项不可用
mi_item_upkeep.setEnabled(false);//设置“用户维护”菜单项不可用
mi_stock_upkeep.setEnabled(false);//设置“用户维护”菜单项不可用
mi_inventory_upkeep.setEnabled(false);//设置“用户维护”菜单项不可用
mi_item_query.setEnabled(false);// 声明商品查询菜单项
mi_employee_query.setEnabled(false);// 声明员工查询菜单项
mi_inventory_query.setEnabled(false);// 声明库存查询菜单项
mi_sale_query.setEnabled(false);// 声明销售查询菜单项
mi_stock_query.setEnabled(false);// 声明进货查询菜单项
mi_supplier_query.setEnabled(false);// 声明供货商查询菜单项
bt_book_upkeep.setEnabled(false);
bt_employee_upkeep.setEnabled(false);
mi_employee_upkeep.setEnabled(false);
}
}
/** 成员方法4:供应商信息维护响应方法 */
private void supplier_upkeep_actionPerformed() {
new SupplierUpKeep();// 创建“供应商信息维护”界面
}
/** 成员方法5:进货信息响应方法 */
private void stock_upkeep_actionPerformed() {
new StockUpKeep();// 创建“进货信息维护”界面
}
/** 成员方法6:库存信息响应方法 */
private void inventory_upkeep_actionPerformed() {
new InventoryUpKeep();// 创建“进货信息维护”界面
}
/** 成员方法7:员工信息响应方法 */
private void employee_upkeep_actionPerformed() {
new EmployeeUpKeep();// 创建“进货信息维护”界面
}
/** 成员方法8:商品维护响应方法 */
private void item_upkeep_actionPerformed() {
new ItemUpKeep();// 创建“商品维护”界面
}
/** 成员方法9:商品查询响应方法 */
private void item_query_actionPerformed() {
new ItemQuery();// 创建“商品查询”界面
}
/** 成员方法10:员工查询响应方法 */
private void employee_query_actionPerformed() {
new EmployeeQuery();// 创建“员工查询”界面
}
/** 成员方法11:库存查询响应方法 */
private void inventory_query_actionPerformed() {
new InventoryQuery();// 创建“员工查询”界面
}
/** 成员方法12:销售查询响应方法 */
private void sale_query_actionPerformed() {
new SaleQuery();// 创建“员工查询”界面
}
/** 成员方法13:进货查询响应方法 */
private void stock_query_actionPerformed() {
new StockQuery();// 创建“员工查询”界面
}
/** 成员方法14:供货商查询响应方法 */
private void supplier_query_actionPerformed() {
new SupplierQuery();// 创建“员工查询”界面
}
/** 成员方法15:退出系统响应方法 */
private void exit_actionPerformed() {
BaseDao1.close();// 关闭与数据库的连接
System.exit(0);// 退出系统
}
// 测试方法:用于主界面的测试
public static void main(String args[]) {
new MainFrame();// 创建系统主界面匿名对象
}
}
测试
因为赶时间做出来的,许多窗口不是很好看,图片很单一,以后有时间再美化下,或者感兴趣的你们也可以试着美化下。
有任何问题可以问我哦。