一、系统介绍
本系统实现了用户登录,实现了对货物类别的增删改查,实现了对货物的增删改查,数据库使用Mysql8.0.13,界面较良好。
二、系统展示
1.登录页面
2.主页面
3.货物类型添加页面
4.货物类型添加成功提示页面
5.货物类别修改页面
6.货物类型修改成功提示
7.货物类型修改成功显示页面
备注:修改之后的页面结果显示
8.货物添加页面
9.货物添加成功提示页面
10.货物修改页面
11.货物修改成功提示页面
12.货物修改成功显示页面
13.货物删除提示页面
14.退出系统提示页面
15.联系我们页面
三、系统实现
Goods.java
package com.sjsq.model; /** * Goods实体类 * * @author shuijianshiqing * */ public class Goods { private int id; private String goodsName; private String goodsSupplier; private String sex; private double price; private String goodsDesc; private Integer goodsTypeId; private String goodsTypeName; /** * 继承父类的构造方法 */ public Goods() { super(); } /** * 一个参数的构造方法 * * @param id */ public Goods(int id) { super(); this.id = id; } /** * 3个参数构造方法 * * @param goodsName * @param goodsSupplier * @param goodsTypeId */ public Goods(String goodsName, String goodsSupplier, Integer goodsTypeId) { super(); this.goodsName = goodsName; this.goodsSupplier = goodsSupplier; this.goodsTypeId = goodsTypeId; } /** * 6个参数的构造方法 * * @param goodsName * @param goodsSupplier * @param sex * @param price * @param goodsDesc * @param goodsTypeId */ public Goods(String goodsName, String goodsSupplier, String sex, double price, String goodsDesc, int goodsTypeId) { super(); this.goodsName = goodsName; this.goodsSupplier = goodsSupplier; this.sex = sex; this.price = price; this.goodsDesc = goodsDesc; this.goodsTypeId = goodsTypeId; } /** * 7个参数的构造方法 * @param goodsName * @param goodsSupplier * @param sex * @param price * @param goodsDesc * @param goodsTypeId * @param goodsTypeName */ public Goods(String goodsName, String goodsSupplier, String sex, double price, String goodsDesc, Integer goodsTypeId, String goodsTypeName) { super(); this.goodsName = goodsName; this.goodsSupplier = goodsSupplier; this.sex = sex; this.price = price; this.goodsDesc = goodsDesc; this.goodsTypeId = goodsTypeId; this.goodsTypeName = goodsTypeName; } /** * 7个参数的构造方法 * * @param id * @param goodsName * @param goodsSupplier * @param sex * @param price * @param goodsDesc * @param goodsTypeId */ public Goods(int id, String goodsName, String goodsSupplier, String sex, double price, String goodsDesc, Integer goodsTypeId) { super(); this.id = id; this.goodsName = goodsName; this.goodsSupplier = goodsSupplier; this.sex = sex; this.price = price; this.goodsDesc = goodsDesc; this.goodsTypeId = goodsTypeId; } /** * 8个参数的构造方法 * @param id * @param goodsName * @param goodsSupplier * @param sex * @param price * @param goodsDesc * @param goodsTypeId * @param goodsTypeName */ public Goods(int id, String goodsName, String goodsSupplier, String sex, double price, String goodsDesc, Integer goodsTypeId, String goodsTypeName) { super(); this.id = id; this.goodsName = goodsName; this.goodsSupplier = goodsSupplier; this.sex = sex; this.price = price; this.goodsDesc = goodsDesc; this.goodsTypeId = goodsTypeId; this.goodsTypeName = goodsTypeName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public String getGoodsSupplier() { return goodsSupplier; } public void setGoodsSupplier(String goodsSupplier) { this.goodsSupplier = goodsSupplier; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getGoodsDesc() { return goodsDesc; } public void setGoodsDesc(String goodsDesc) { this.goodsDesc = goodsDesc; } public Integer getGoodsTypeId() { return goodsTypeId; } public void setGoodsTypeId(Integer goodsTypeId) { this.goodsTypeId = goodsTypeId; } public String getGoodsTypeName() { return goodsTypeName; } @Override public String toString() { return "Goods [id=" + id + ", goodsName=" + goodsName + ", goodsSupplier=" + goodsSupplier + ", sex=" + sex + ", price=" + price + ", goodsDesc=" + goodsDesc + ", goodsTypeId=" + goodsTypeId + ", goodsTypeName=" + goodsTypeName + "]"; } }
GoodsType.java
package com.sjsq.model; /** * GoodsType实体类 * * @author shuijianshiqing * */ public class GoodsType { // 货物类型Id private int id; // 获取类型名称 private String goodsTypeName; // 货物类型描述 private String goodsTypeDesc; /** * 继承自父类的构造方法 */ public GoodsType() { super(); } /** * 一个参数的构造方法 * * @param goodsTypeName */ public GoodsType(String goodsTypeName) { super(); this.goodsTypeName = goodsTypeName; } /** * 一个参数的构造方法 * * @param id */ public GoodsType(int id) { super(); this.id = id; } /** * 2个参数的构造方法 * * @param goodsTypeName * @param goodsTypeDesc */ public GoodsType(String goodsTypeName, String goodsTypeDesc) { super(); this.goodsTypeName = goodsTypeName; this.goodsTypeDesc = goodsTypeDesc; } /** * 3个参数构造方法 * * @param id * @param goodsTypeName * @param goodsTypeDesc */ public GoodsType(int id, String goodsTypeName, String goodsTypeDesc) { super(); this.id = id; this.goodsTypeName = goodsTypeName; this.goodsTypeDesc = goodsTypeDesc; } @Override public String toString() { return goodsTypeName; } // Set和Get方法 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGoodsTypeName() { return goodsTypeName; } public void setGoodsTypeName(String goodsTypeName) { this.goodsTypeName = goodsTypeName; } public String getGoodsTypeDesc() { return goodsTypeDesc; } public void setGoodsTypeDesc(String goodsTypeDesc) { this.goodsTypeDesc = goodsTypeDesc; } }
DbUtil.java
package com.sjsq.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; /** * 数据库的连接 * * @author shuijianshiqing * */ public class DbUtil { // 数据库驱动名 private static String DriverName = "com.mysql.cj.jdbc.Driver"; // 数据库协议 private static String dbUril = "jdbc:mysql://localhost:3306/db_warehouse?serverTimezone=UTC"; // 数据库用户名 private static String dbName = "root"; // 数据库密码 private static String dbPassword = "admin"; /** * 数据库驱动的加载与数据库连接 * * @return * @throws Exception */ public static Connection getCon() throws Exception { Class.forName(DriverName); Connection conn = DriverManager.getConnection(dbUril, dbName, dbPassword); return conn; } /** * 关闭数据库连接 * * @param conn * @throws SQLException */ public static void close(Connection conn) throws SQLException { if (conn != null) { conn.close(); } } /** * 关闭数据库连接 * * @param conn * @param rs * @throws SQLException */ public static void close(Connection conn, ResultSet rs) throws SQLException { if (rs != null) { rs.close(); if (conn != null) { conn.close(); } } } /** * 测试main()方法 * * @param args */ public static void main(String[] args) { try { getCon(); System.out.println("数据库连接成功!"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("数据库连接失败!"); } } }
StringUtil.java
package com.sjsq.util; /** * 判断用户的输入是否为空方法类 * * @author shuijianshiqing * */ public class StringUtil { /** * 判断字符串是否为空 * * @param str * @return */ public static boolean isEmpty(String str) { if (str == null || "".equals(str.trim())) { return true; } else { return false; } } /** * 判断字符串是否不为空 * * @param str * @return */ public static boolean isNotEmpty(String str) { if (str != null && !"".equals(str.trim())) { return true; } else { return false; } } }
GoodsDao.java
package com.sjsq.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import com.sjsq.model.Goods; import com.sjsq.util.DbUtil; import com.sjsq.util.StringUtil; /** * 货物控制操作包 * * @author Peter * @author shuijianshiqing * */ public class GoodsDao { /** * 货物删除事件 * * @param conn * @param goods * @return * @throws Exception */ public static int deleteGoods(Connection conn, Goods goods) throws Exception { String sql = "delete from t_goods where id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1, goods.getId()); return pstmt.executeUpdate(); } /** * 货物更新事件 * * @param conn * @param goods * @return * @throws Exception */ public static int updateGoods(Connection conn, Goods goods) throws Exception { String sql = "update t_goods set goodsName=?, goodsSupplier=?, sex=?," + " price=?, goodsDesc=? , goodsTypeId = ? where id = ?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, goods.getGoodsName()); pstmt.setString(2, goods.getGoodsSupplier()); pstmt.setString(3, goods.getSex()); pstmt.setDouble(4, goods.getPrice()); pstmt.setString(5, goods.getGoodsDesc()); pstmt.setInt(6, goods.getGoodsTypeId()); pstmt.setInt(7, goods.getId()); return pstmt.executeUpdate(); } /** * 货物查询事件 * * @param conn * @param goods * @return * @throws Exception */ public static ResultSet listGoods(Connection conn, Goods goods) throws Exception { StringBuffer sb = new StringBuffer("select * from t_goods t_g, t_goodsType t_gt where t_g.goodsTypeId=t_gt.id"); if (StringUtil.isNotEmpty(goods.getGoodsName())) { sb.append(" and t_g.goodsName like '%" + goods.getGoodsName() + "%'"); } if (StringUtil.isNotEmpty(goods.getGoodsSupplier())) { sb.append(" and t_g.goodsSupplier like '%" + goods.getGoodsSupplier() + "%'"); } if (goods.getGoodsTypeId() != null && goods.getGoodsTypeId() != -1) { sb.append(" and t_g.goodsTypeId like '%" + goods.getGoodsTypeId() + "%'"); } PreparedStatement pstmt = conn.prepareStatement(sb.toString()); return pstmt.executeQuery(); } /** * 添加货物 * * @param conn * @param goods * @return * @throws Exception */ public static int addGoods(Connection conn, Goods goods) throws Exception { String sql = "insert into t_goods values(null, ?, ?, ?, ?, ?, ?,?)"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, goods.getGoodsName()); pstmt.setString(2, goods.getGoodsSupplier()); pstmt.setString(3, goods.getSex()); pstmt.setDouble(4, goods.getPrice()); pstmt.setString(5, goods.getGoodsDesc()); pstmt.setInt(6, goods.getGoodsTypeId()); pstmt.setString(7, goods.getGoodsTypeName()); return pstmt.executeUpdate(); } public static void main(String[] args) { Connection conn; try { conn = DbUtil.getCon(); GoodsDao goodsDao = new GoodsDao(); Goods goods = new Goods(1,"q", "q", "nv", 2, "q", 1,"AA"); System.out.println(goods); int result = goodsDao.addGoods(conn, goods); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
UserDao.java
package com.sjsq.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import com.sjsq.model.User; /** * UserDao实体类是对user管理员的操做控制 * * @author shuijianshiqing * */ public class UserDao { /** * 管理员登录操作 * * @param conn * @param user * @return * @throws Exception */ public User login(Connection conn, User user) throws Exception { User resultUser = null; String sql = "select * from t_user where username=? and password=?"; PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, user.getUsername()); pstmt.setString(2, user.getPassword()); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { resultUser = new User(); resultUser.setId(rs.getInt("id")); resultUser.setUsername(rs.getString("username")); resultUser.setPassword(rs.getString("password")); } return resultUser; } }
LoginFrm.java
package com.sjsq.view; import java.awt.EventQueue; import java.awt.Font; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.SQLException; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.border.EmptyBorder; import com.sjsq.dao.UserDao; import com.sjsq.model.User; import com.sjsq.util.DbUtil; import com.sjsq.util.StringUtil; /** * 登录页面视图层 * * @author shuijianshiqing * */ public class LoginFrm extends JFrame { private static DbUtil dbUtil = new DbUtil(); private static UserDao userDao = new UserDao(); // 主板 private JPanel contentPane; // 账号 private JTextField userNameTxt; // 密码 private JPasswordField passwordTxt; /** * 创建窗体 */ public LoginFrm() { // 该表系统默认字体 Font font = new Font("Dialog", Font.PLAIN, 12); java.util.Enumeration keys = UIManager.getDefaults().keys(); while (keys.hasMoreElements()) { Object key = keys.nextElement(); Object value = UIManager.get(key); if (value instanceof javax.swing.plaf.FontUIResource) { UIManager.put(key, font); } } setIconImage(Toolkit.getDefaultToolkit().getImage(LoginFrm.class.getResource("/images/goods_logo.png"))); setTitle("仓库管理系统"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 546, 369); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); JLabel label = new JLabel("仓库管理系统管理员登录界面"); label.setFont(new Font("宋体", Font.BOLD, 20)); label.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/goods_logo.png"))); JLabel label_1 = new JLabel("账号"); label_1.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/user.png"))); JLabel label_2 = new JLabel("密码"); label_2.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/password.png"))); userNameTxt = new JTextField(); userNameTxt.setColumns(10); passwordTxt = new JPasswordField(); JButton btnNewButton = new JButton("登录"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { loginActionPerformed(arg0); } }); btnNewButton.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/login.png"))); JButton button = new JButton("重置"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { resetValueActionPerformed(arg0); } }); button.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/reset.png"))); GroupLayout gl_contentPane = new GroupLayout(contentPane); gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup().addContainerGap(99, Short.MAX_VALUE) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addComponent(label) .addGroup(gl_contentPane.createSequentialGroup().addGap(38) .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING) .addComponent(label_2).addComponent(label_1)) .addGap(18) .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false) .addComponent(passwordTxt) .addComponent(userNameTxt, GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE) .addComponent(button, Alignment.TRAILING)))) .addGap(96)) .addGroup(gl_contentPane.createSequentialGroup().addGap(176).addComponent(btnNewButton) .addContainerGap(261, Short.MAX_VALUE))); gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING) .addGroup(gl_contentPane.createSequentialGroup().addGap(29).addComponent(label).addGap(42) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addComponent(label_1)) .addGap(18) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(label_2) .addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(48) .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE) .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE) .addComponent(button, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)) .addContainerGap(37, Short.MAX_VALUE))); contentPane.setLayout(gl_contentPane); // 居中显示 this.setLocationRelativeTo(null); } /** * 管理员登录 * * @param arg0 */ private void loginActionPerformed(ActionEvent arg0) { String userName = this.userNameTxt.getText(); String password = new String(this.passwordTxt.getPassword()); if (StringUtil.isEmpty(userName)) { JOptionPane.showMessageDialog(null, "用户名不能为空!"); return; } if (StringUtil.isEmpty(password)) { JOptionPane.showMessageDialog(null, "密码不能为空!"); return; } User user = new User(userName, password); Connection conn = null; try { conn = dbUtil.getCon(); User currentUser = userDao.login(conn, user); if (currentUser != null) { // JOptionPane.showMessageDialog(null, "登录成功!"); dispose(); new MainFrm().setVisible(true); } else { JOptionPane.showMessageDialog(null, "登录失败!"); } } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(null, "登录失败!"); } finally { try { dbUtil.close(conn); } catch (SQLException e) { e.printStackTrace(); } } } /** * 重置事件 * * @param arg0 */ private void resetValueActionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub this.resetValue(); } /** * 重置表单事件 */ private void resetValue() { this.userNameTxt.setText(""); this.passwordTxt.setText(""); } /** * 运行程序 */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { LoginFrm frame = new LoginFrm(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } }