Java+swing+Mysql實現(xiàn)商品銷售管理系統(tǒng)
前言
臨近期末做了一個商品銷售管理系統(tǒng),分享下,全部源碼在碼云,需要自取,博客只分享部分代碼(太多了)。
數(shù)據(jù)庫的建立
我們使用Navicat Premium 15連接mysql,建立如下九張表。
得到如下視圖(看上去似乎有點亂了)
因為register是一個注冊表,所以不參與視圖里的連接。
各種表的情況如上所示,當然你也可以用sql語句寫。
這個是我導出來的sql語句,由于我也沒試過,所以不保證對錯,如果錯了只能老老實實自己差數(shù)據(jù)了。
/* 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', '紅發(fā)四皇', '海賊王', '其他', 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', '香克斯', '否', '紅發(fā)香克斯四皇之一', 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 '進貨數(shù)量', `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 '銷售數(shù)量', `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 '數(shù)量', 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 '聯(lián)系電話', 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 ('紅發(fā)海賊團', '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 ('廣西藥業(yè)', 's010', '廣西柳州', '11100001011'); SET FOREIGN_KEY_CHECKS = 1;
Java系統(tǒng)
entity包
實體集包,你有多少張表就需要在底下建多少個類
里面寫各種屬性、方法,只要不是int型,其余全定義為string型。
data包
連接數(shù)據(jù)庫更新的包,里面寫入各種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);// 執(zhí)行查詢語句,并返回結果集 // 處理查詢結果 try { if (rs.next()) { // 創(chuàng)建員工對象,保存員工信息 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<Employee> selectEmployeeList(String sql) { List<Employee> list = new ArrayList<Employee>();// 創(chuàng)建列表 if (sql != null) { Employee employee = null; ResultSet rs = BaseDao1.executeQuery(sql);// 執(zhí)行查詢語句,并返回結果集 try { while (rs.next()) { // 創(chuàng)建員工對象,保存員工信息 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<Employee> selectEmployeeList() { // 構造查詢所有員工的sql查詢語句 String sql = "select * from employee"; return selectEmployeeList(sql);// 返回員工列表 } /** 成員方法4:按照字符屬性查詢條件,獲取員工對象列表,重載方法 */ public static List<Employee> 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<Employee> selectEmployeeList(String field, int value) { // 構造默認的sql查詢語句 String sql = "select * from employee"; if (field != null && value != 0) { // 構造按照整型屬性查詢員工的sql查詢語句,采用區(qū)間查詢 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);// 執(zhí)行更新語句 } 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);// 執(zhí)行更新語句 } else { return -1; } } /** 成員方法8:刪除指定員工信息 */ public static int deleteEmployee(String EmployeeNo) { // 構造刪除員工的sql更新語句 String sql = "delete from employee where employeeNo='" + EmployeeNo + "'"; return BaseDao1.executeUpdate(sql);// 執(zhí)行更新語句 } /** 成員方法9:清空所有員工信息 */ public static void emptyEmployee() { // 構造清空員工的sql更新語句 String sql = "delete from employee"; BaseDao1.executeUpdate(sql);// 執(zhí)行更新語句 } }
Login包
里面寫入java swing登錄窗口的方法和連接數(shù)據(jù)庫的方法
一個handleLogin類
package Login; import windows.MainFrame; import java.sql.*; import javax.swing.JOptionPane; public class HandleLogin { Connection con;//與特定數(shù)據(jù)庫的連接(會話)。 PreparedStatement preSql;//PreparedStatement是一個接口,preSql是一個接口引用,接口不能new對象,所以借用下方prepareStatement方法 ResultSet rs;//表示數(shù)據(jù)庫結果集的數(shù)據(jù)表,通常通過執(zhí)行查詢數(shù)據(jù)庫的語句生成。 public HandleLogin(){ con = GetDBConnection.connectDB("user","root","123456");//鏈接數(shù)據(jù)庫 } 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);//創(chuàng)建一個 PreparedStatement對象,用于將參數(shù)化的SQL語句發(fā)送到數(shù)據(jù)庫 preSql.setString(1,id);//將指定的參數(shù)設置為給定的Java String值。1代表sql語句中第一個?號,后面的參數(shù)代表給?賦的值 preSql.setString(2,pw);//同上 rs = preSql.executeQuery(); if(rs.next()==true) {//最初,光標位于第一行之前,next方法將光標移動到下一行,并且由于在ResultSet對象中沒有更多行時返回false loginModel.setLoginSuccess(true);//第一行有數(shù)據(jù)代表該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; } }
這個包建完后已經可以實現(xiàn)登錄和注冊了
windows包
這個包最復雜了,包含了查詢和更新調用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<String> cb_query;// 聲明查詢欄目組合框 JButton bt_query;// 聲明查詢按鈕 private JTable table;// 聲明表格 // 調用方法,獲取所有員工列表,并保存 private List<Employee> list = EmployeeDao.selectEmployeeList(); // 創(chuàng)建員工表欄目名稱字符串 private String[] tb_heads = { "員工編號 ", "員工名稱 ", "性別", "職務", "基本工資" }; // 員工表欄目名稱對應的表字段 private String[] fields = { "EmployeeNo", "EmployeeName", "sex", "post","basePay" }; // 創(chuàng)建員工表格模型 private DefaultTableModel model = new DefaultTableModel( new Object[][] {}, tb_heads); private String field = "EmployeeNo";// 設置默認的當前的查詢字段 String valueStr = " ";// 設置字符串類型查詢字段的初始值 int valueInt = 0;// 設置整型類型查詢字段的初始值 double valueDouble = 0.0;// 設置實型數(shù)據(jù)查詢字段的初始值 /** 構造方法:初始化界面 */ 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:創(chuàng)建查詢面板的方法 */ private JPanel createQueryPanel() { JPanel queryPanel = new JPanel(); queryPanel.setOpaque(false);//允許底層像素出現(xiàn) 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(); // 為查詢條件文本框,添加回車鍵事件監(jiān)聽器 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<String>(tb_heads); // 為查詢欄目組合框,添加條目事件監(jiān)聽器 cb_query.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent arg0) { // 調用“查詢欄目”組合框欄目狀態(tài)被改變事件響應方法 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("查詢 "); // 為“查詢”按鈕,添加單擊事件監(jiān)聽器 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("關閉"); // 為“關閉”按鈕,添加單擊事件監(jiān)聽器 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:創(chuàng)建表格面板的方法 */ private JPanel createTablePanel() { // 創(chuàng)建表格面板,采用邊界布局 JPanel tablePanel = new JPanel(new BorderLayout(5, 5)); JScrollPane scrollPane = new JScrollPane();// 創(chuàng)建滾動面板 tablePanel.add(scrollPane);// 將表格面板添加到滾動面板上 table = new JTable(model);// 創(chuàng)建表格,并采用指定的表格模型 addRowToModel(list); scrollPane.setViewportView(table); return tablePanel; } /** 成員方法3:向表格模型,添加員工列表 */ private void addRowToModel(List<Employee> list) { model.setRowCount(0);// 清除表格模型中的數(shù)據(jù) 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:查詢欄目組合框狀態(tài)被改變的事件響應方法 */ private void cb_query_itemStateChanged(ItemEvent arg0) { if (arg0.getStateChange() == ItemEvent.SELECTED) { // 根據(jù)選擇改變標簽顯示內容 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<String> 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;// 聲明表格 // 創(chuàng)建員工表格模型 private DefaultTableModel model = new DefaultTableModel( new Object[][] {}, new String[] { "員工編號", "員工姓名", "性別", "職務","基本工資","密碼"}); private List<Employee> list = EmployeeDao.selectEmployeeList();// 創(chuàng)建所有員工列表,并保存員工對象 /** 構造方法:用于初始化界面 */ EmployeeUpKeep() { // 界面設置 this.setTitle("--員工信息維護--"); setBounds(220, 100, 1300, 650); this.setResizable(false); this.setLocationRelativeTo(null); // 創(chuàng)建一個對話面板,其上放置一個外部分割面板,并將它設置為框架的內容面板 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); // 創(chuàng)建一個外部分割面板,水平左右放置組件。其中,左測放置一個內部分割面板,右測放置一個表格面板 JSplitPane outerPane = new JSplitPane();// 創(chuàng)建外部分割面板 outerPane.setOpaque(false);// 設置外部分割面板透明 outerPane.setResizeWeight(0.1);// 設置外部分割面板寬度分配權限 outerPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);//設置外部分割面板水平分配 outerPane.setOneTouchExpandable(true);//設置外部分割面板可以有一個觸發(fā)擴展 dialogPane.add(outerPane, BorderLayout.CENTER); // 創(chuàng)建一個內部分割面板,垂直上下放置組件。其中,上方放置一個數(shù)據(jù)面板,下方放置一個按鈕面板 JSplitPane innerPane = new JSplitPane();// 創(chuàng)建內部分割面板 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();// 調用創(chuàng)建數(shù)據(jù)面板方法,創(chuàng)建數(shù)據(jù)面板 innerPane.setLeftComponent(dataPanel);// 將數(shù)據(jù)面板,設置在內部分割面板的左側 JPanel buttonPanel = createButtonPanel();// 調用創(chuàng)建按鈕面板方法,創(chuàng)建按鈕面板 innerPane.setRightComponent(buttonPanel);// 將按鈕面板設置在內部分割面板的下方 JPanel tablePanel = createTablePanel();// 調用創(chuàng)建表格面板方法,創(chuàng)建表格面板 outerPane.setRightComponent(tablePanel);// 將表格面板,設置在外部分割面板的右側 } /** 成員方法1:用于創(chuàng)建“數(shù)據(jù)面板"的方法 */ 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() {//為員工名文本框添加失去焦點監(jiān)聽器 @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<String>(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:用于創(chuàng)建"按鈕面板"的方法 */ private JPanel createButtonPanel() { JPanel buttonPanel = new JPanel(new GridBagLayout());//創(chuàng)建按鈕面板,用網格包布局 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() {//為添加按鈕添加單擊事件監(jiān)聽器 @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() {//為修改按鈕添加單擊事件監(jiān)聽器 @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() {// 為刪除按鈕添加單擊事件監(jiān)聽器 @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() {//為取消按鈕添加單擊事件監(jiān)聽器 @Override public void actionPerformed(ActionEvent e) { del_content();// 調用刪除內容方法,清除數(shù)據(jù)面板中的數(shù)據(jù) } }); 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() {//為關閉按鈕添加單擊事件監(jiān)聽器 @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() {//為清空按鈕添加單擊事件監(jiān)聽器 @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:用于創(chuàng)建表格面板的方法 */ private JPanel createTablePanel() { // 創(chuàng)建表格面板,采用邊界布局 JPanel tablePanel = new JPanel(new BorderLayout(5, 5)); JScrollPane scrollPane = new JScrollPane();// 創(chuàng)建滾動面板 tablePanel.add(scrollPane);// 將表格面板添加到滾動面板上 table = new JTable(model);// 創(chuàng)建表格,并采用指定的表格模型 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() {// 為表格添加鼠標事件監(jiān)聽器 @Override public void mouseClicked(MouseEvent e) { // 調用表格鼠標單擊事件響應方法,向左側數(shù)據(jù)面板顯示選中員工 table_mouseClicked(); } }); scrollPane.setViewportView(table); return tablePanel; } /** 成員方法4:用于清除數(shù)據(jù)面板中的數(shù)據(jù) */ 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) {// 判斷員工對象是否存在,若存在,清除數(shù)據(jù),重新輸入員工編號 JOptionPane.showMessageDialog(null, "員工編號已經存在,請重新輸入員工編號!"); del_content(); // 清除數(shù)據(jù)面板中的數(shù)據(jù) } } /** 成員方法6:員工名文本框失去焦點事件響應具體方法 */ public void tf_name_focusLost() { Employee Employee = EmployeeDao.getEmployeeByEmployeeNo(tf_name.getText().trim()); if (Employee != null) { JOptionPane.showMessageDialog(null, "員工名已經存在,請重新輸入員工名!"); tf_name.setText(""); } } /** 成員方法7:用于更新表格數(shù)據(jù) */ public void refresh() { model.setRowCount(0);// 清除表格模型中的數(shù)據(jù) list = EmployeeDao.selectEmployeeList();// 獲取員工對象列表 for (int i = 0; i < list.size(); i++) {// 重新項表格模型中添加數(shù)據(jù) Employee Employee = list.get(i); model.addRow(new Object[] { Employee.getEmployeeNo(), Employee.getEmployeeName(), Employee.getSex(), Employee.getPost(), Employee.getBasePay(), Employee.getPassword() }); } del_content();// 清除數(shù)據(jù)面板中的數(shù)據(jù) } /** 成員方法8:表格鼠標單擊事件響應方法 */ public void table_mouseClicked() { // 獲取選中員工的信息 Employee Employee_old = list.get(table.getSelectedRow()); // 在數(shù)據(jù)面板中設置對應的員工數(shù)據(jù) 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();// 創(chuàng)建一個員工對象 // 設置員工對象的屬性值 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 {// 若不空,則執(zhí)行添加員工對象操作 int i = EmployeeDao.insertEmployee(Employee);// 執(zhí)行添加操作 if (i == 1) {// 判斷添加操作是否成功?若成功,則向表格模型添加這條數(shù)據(jù) 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();// 創(chuàng)建一個員工對象 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 {// 若不空,則執(zhí)行添加員工對象操作 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) {// 如果確定,則執(zhí)行刪除操作 if (EmployeeDao.deleteEmployee(id) == 1) {// 執(zhí)行用戶刪除操作 refresh();// 更新表格內容 JOptionPane.showMessageDialog(null, "員工信息刪除成功!"); } del_content();// 清除數(shù)據(jù)面板內容 } } /** 成員方法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, "真的確認清空所有員工數(shù)據(jù)嗎?", "清空員工信息再次確認", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) { EmployeeDao.emptyEmployee(); refresh();// 更新表格內容 del_content();// 清除數(shù)據(jù)面板內容 } } } // 測試方法:用于員工維護的測試 public static void main(String[] args) { new EmployeeUpKeep(); } }
測試
最后一個刪除所有員工就不試了哈,好難重新插回去的。。
主系統(tǒng)類
一個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;// 聲明退出系統(tǒng)菜單項 private JButton bt_employee_upkeep;// 聲明讀者維護按鈕 private JButton bt_book_upkeep;// 聲明商品維護按鈕 private JButton bt_reader_query;// 聲明讀者查詢按鈕 private JButton bt_book_query;// 聲明商品查詢按鈕 private JButton bt_exit;// 聲明退出系統(tǒng)按鈕 /** 構造方法:系統(tǒng)主界面初始化方法 */ public MainFrame() { //第1步:系統(tǒng)主界面設置 this.setTitle("商品銷售管理系統(tǒng)");// 設置主界面標題 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步:創(chuàng)建、設置菜單欄。先調用創(chuàng)建系統(tǒng)菜單欄方法,創(chuàng)建系統(tǒng)菜單欄,再在主界面設置菜單欄 this.setJMenuBar(createMenuBar()); //第3步: 創(chuàng)建、設置工具欄。先調用創(chuàng)建系統(tǒng)工具欄方法,創(chuàng)建系統(tǒng)工具欄,再將工具欄添加到界面的上方 this.add(createToolBar(),BorderLayout.NORTH); this.setVisible(true); this.setPurView(LoginView.a); } /** 成員方法1:創(chuàng)建系統(tǒng)菜單欄方法 */ private JMenuBar createMenuBar() { // 第1步:創(chuàng)建菜單欄對象 JMenuBar menuBar = new JMenuBar(); //第2步:創(chuàng)建菜單。如基礎維護菜單,又包含用戶維護、讀者維護、商品維護菜單項 JMenu menu_upkeep = new JMenu("基礎維護"); // 創(chuàng)建"基礎維護"菜單 //第3步:創(chuàng)建菜單項,并為菜單項設置單擊事件監(jiān)聽器 mi_supplier_upkeep = new JMenuItem("供應商信息維護"); // 創(chuàng)建“用戶維護”菜單項 mi_supplier_upkeep.addActionListener(new ActionListener() {//添加單擊事件監(jiān)聽器 @Override public void actionPerformed(ActionEvent e) { supplier_upkeep_actionPerformed();// 調用“供應商信息維護”響應方法 } }); //第4步:將菜單項添加到菜單上 menu_upkeep.add(mi_supplier_upkeep);// 將“供應商信息維護”菜單項添加到“基礎維護”菜單 mi_item_upkeep = new JMenuItem("商品信息維護"); // 創(chuàng)建"商品維護"菜單項 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("進貨信息維護"); // 創(chuàng)建"商品維護"菜單項 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("庫存信息維護"); // 創(chuàng)建"商品維護"菜單項 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("員工信息維護"); // 創(chuàng)建"商品維護"菜單項 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);// 將"基礎維護"菜單添加到系統(tǒng)菜單欄 JMenu menu_query = new JMenu("查詢統(tǒng)計"); // 創(chuàng)建"查詢統(tǒng)計"菜單 mi_item_query = new JMenuItem("商品查詢"); // 創(chuàng)建"商品查詢"菜單項 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("員工查詢"); // 創(chuàng)建"讀者查詢"菜單項 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("庫存查詢"); // 創(chuàng)建"讀者查詢"菜單項 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("銷售查詢"); // 創(chuàng)建"銷售查詢"菜單項 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("進貨查詢"); // 創(chuàng)建"進貨查詢"菜單項 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("供貨商查詢"); // 創(chuàng)建"供貨商查詢"菜單項 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("系統(tǒng)管理");// 創(chuàng)建"系統(tǒng)管理"菜單 mi_exit = new JMenuItem("退出系統(tǒng)"); // 創(chuàng)建"退出系統(tǒng)"菜單項 mi_exit.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { exit_actionPerformed();// 調用"退出系統(tǒng)"響應方法 } }); menu_management.add(mi_exit); menuBar.add(menu_management); //第6步:返回菜單欄。 return menuBar; } /** 成員方法2: 創(chuàng)建工具欄方法 */ private JToolBar createToolBar() { //第1步:創(chuàng)建、設置工具欄 JToolBar toolBar = new JToolBar("商品管理系統(tǒng)工具欄");// 創(chuàng)建工具欄對象 toolBar.setFloatable(false); // 設置工具欄不可浮動 // 設置工具欄邊框導角方式 toolBar.setBorder(new BevelBorder(BevelBorder.LOWERED)); //第2步: 創(chuàng)建、設置工具欄上的快捷按鈕 bt_employee_upkeep = new JButton("員工維護");// 創(chuàng)建"讀者維護"按鈕 // 創(chuàng)建"讀者維護"圖標 ImageIcon icon_reader_upkeep = new ImageIcon("images/reader_upkeep.png"); bt_employee_upkeep.setIcon(icon_reader_upkeep);// 為"讀者維護"按鈕設置圖標 bt_employee_upkeep.setToolTipText("員工維護");// 為"讀者維護"按鈕設置提示 // 將"讀者維護"按鈕注冊單擊事件監(jiān)聽器 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("退出系統(tǒng)"); 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();// 調用“退出系統(tǒng)"響應方法 } }); //第4步:返回工具欄 return toolBar; } /** 成員方法3:設置系統(tǒng)功能使用權限的方法 */ 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();// 創(chuàng)建“供應商信息維護”界面 } /** 成員方法5:進貨信息響應方法 */ private void stock_upkeep_actionPerformed() { new StockUpKeep();// 創(chuàng)建“進貨信息維護”界面 } /** 成員方法6:庫存信息響應方法 */ private void inventory_upkeep_actionPerformed() { new InventoryUpKeep();// 創(chuàng)建“進貨信息維護”界面 } /** 成員方法7:員工信息響應方法 */ private void employee_upkeep_actionPerformed() { new EmployeeUpKeep();// 創(chuàng)建“進貨信息維護”界面 } /** 成員方法8:商品維護響應方法 */ private void item_upkeep_actionPerformed() { new ItemUpKeep();// 創(chuàng)建“商品維護”界面 } /** 成員方法9:商品查詢響應方法 */ private void item_query_actionPerformed() { new ItemQuery();// 創(chuàng)建“商品查詢”界面 } /** 成員方法10:員工查詢響應方法 */ private void employee_query_actionPerformed() { new EmployeeQuery();// 創(chuàng)建“員工查詢”界面 } /** 成員方法11:庫存查詢響應方法 */ private void inventory_query_actionPerformed() { new InventoryQuery();// 創(chuàng)建“員工查詢”界面 } /** 成員方法12:銷售查詢響應方法 */ private void sale_query_actionPerformed() { new SaleQuery();// 創(chuàng)建“員工查詢”界面 } /** 成員方法13:進貨查詢響應方法 */ private void stock_query_actionPerformed() { new StockQuery();// 創(chuàng)建“員工查詢”界面 } /** 成員方法14:供貨商查詢響應方法 */ private void supplier_query_actionPerformed() { new SupplierQuery();// 創(chuàng)建“員工查詢”界面 } /** 成員方法15:退出系統(tǒng)響應方法 */ private void exit_actionPerformed() { BaseDao1.close();// 關閉與數(shù)據(jù)庫的連接 System.exit(0);// 退出系統(tǒng) } // 測試方法:用于主界面的測試 public static void main(String args[]) { new MainFrame();// 創(chuàng)建系統(tǒng)主界面匿名對象 } }
測試
因為趕時間做出來的,許多窗口不是很好看,圖片很單一,以后有時間再美化下,或者感興趣的你們也可以試著美化下。
有任何問題可以問我哦。
到此這篇關于Java+swing+Mysql實現(xiàn)商品銷售管理系統(tǒng)的文章就介紹到這了,更多相關Java 銷售管理系統(tǒng)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- java(swing)+ mysql實現(xiàn)學生信息管理系統(tǒng)源碼
- Java+MySQL實現(xiàn)圖書管理系統(tǒng)(完整代碼)
- 基于Mysql+JavaSwing的超市商品管理系統(tǒng)設計與實現(xiàn)
- Java實現(xiàn)茶葉售賣商城系統(tǒng)(java+SSM+JSP+EasyUi+mysql)
- Eclipse+Java+Swing+Mysql實現(xiàn)工資管理系統(tǒng)
- Eclipse+Java+Swing+Mysql實現(xiàn)電影購票系統(tǒng)(詳細代碼)
- Java+MySQL實現(xiàn)設計優(yōu)惠券系統(tǒng)
相關文章
JavaWeb Servlet生命周期細枝末節(jié)處深究
Servlet指在服務器端執(zhí)行的一段Java代碼,可以接收用戶的請求和返回給用戶響應結果,下面這篇文章主要給大家介紹了關于JavaWeb.servlet生命周期的相關資料,需要的朋友可以參考下2022-10-10使用java實現(xiàn)telnet-client工具分享
這篇文章主要介紹了使用java實現(xiàn)telnet-client工具,需要的朋友可以參考下2014-03-03Springboot內置Tomcat配置參數(shù)調優(yōu)方式
這篇文章主要介紹了Springboot內置Tomcat配置參數(shù)調優(yōu)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06