1、项目功能演示
DC00019基于java swing+sqlserver超市商品信息管理系统java项目GUI商品信息管理系统
2、项目功能描述
基于java swing+sqlserver超市管理系统功能
1、系统登录
2、员工管理:添加员工、查询员工、所有员工
3、部门管理:添加部门、查询部门
4、商品管理:商品信息查询
5、销售管理:卖出商品、销售查询
3、项目功能截图
4、项目核心代码
4.1 数据库操作类
package com.db;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
public class Jdbc {
// 创建连接池
public static Vector<Connection> connectionPool = new Vector<Connection>();
// 在主函数运行之前创建好连接池
static {
String driver = "";
String url = "";
String username = "";
String password = "";
try {
Reader read = new FileReader("src\\db.properties");
BufferedReader bufferedReader = new BufferedReader(read);
String line = bufferedReader.readLine();
while (line != null) {
String[] strings = line.split("=", 2);
String key = strings[0];
String value = strings[1];
if ("username".equals(key)) {
username = value;
}
if ("password".equals(key)) {
password = value;
}
if ("driver".equals(key)) {
driver = value;
}
if ("url".equals(key)) {
url = value;
}
line = bufferedReader.readLine();
}
bufferedReader.close();
read.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
Class.forName(driver);
for (int i = 0; i < 10; i++) {
Connection connection = DriverManager.getConnection(url,
username, password);
connectionPool.add(connection);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 取出连接
public static Connection getConnection() {
Connection connection = connectionPool.get(0);
connectionPool.remove(0);
return connection;
}
// 放回连接
public static void releaseConnection(Connection connection) {
connectionPool.add(connection);
}
// 增删改
public static int zsg(String sql, Object... p) {
Connection connection = getConnection();
int n = 0;
try {
PreparedStatement preparedStatement = connection
.prepareStatement(sql);
for (int i = 0; i < p.length; i++) {
preparedStatement.setObject(i + 1, p[i]);
}
n = preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
releaseConnection(connection);
}
return n;
}
// 查詢
public static ResultSet query(String sql, Object... p) {
Connection connection = getConnection();
ResultSet rs = null;
try {
PreparedStatement preparedStatement = connection
.prepareStatement(sql);
for (int i = 0; i < p.length; i++) {
preparedStatement.setObject(i + 1, p[i]);
}
rs = preparedStatement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
4.2 登录窗口
package com.view;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UnsupportedLookAndFeelException;
import com.control.CancelLoginListener;
import com.control.LoginActionListener;
import com.control.RandomNumberListener;
public class LoginView extends JFrame {
private JLabel usernameJLabel, passwordJLabel;
private JTextField usernameJTextField, passwordJTextField;
private JButton loginButton, cancelButton;
private JLabel randomNumber, randomNumberJLabel;
private JTextField randomField;
private Icon icon=new ImageIcon("src//Image//longin.jpg");
private JLabel ImageLabel=new JLabel(icon);
public LoginView() {
// 设置容器属性
setTitle("用户登录");
setSize(650, 432);
setLayout(null);
setResizable(false);// 设置窗口的大小不能改变
setLocationRelativeTo(null);
// 初始化组件
usernameJLabel = new JLabel("用戶名:");
passwordJLabel = new JLabel("密 码:");
usernameJTextField = new JTextField();
passwordJTextField = new JPasswordField();
randomNumber=new JLabel("验证码:");
randomNumberJLabel=new JLabel("9527");
randomField=new JTextField();
cancelButton = new JButton("取消");
loginButton = new JButton("登陆");
// 地位组件
usernameJLabel.setBounds(320, 170, 70, 20);
usernameJTextField.setBounds(400, 170, 130, 20);
passwordJLabel.setBounds(320, 220, 70, 20);
passwordJTextField.setBounds(400, 220, 130, 20);
randomNumber.setBounds(320, 270, 70, 20);
randomNumberJLabel.setBounds(390, 270, 50, 20);
randomField.setBounds(450, 270, 70, 20);
cancelButton.setBounds(320, 320, 70, 20);
loginButton.setBounds(450, 320, 70, 20);
ImageLabel.setBounds(0, 0, 650, 432);
add(usernameJLabel);
add(usernameJTextField);
add(passwordJLabel);
add(passwordJTextField);
add(randomNumber);
add(randomNumberJLabel);
add(randomField);
add(cancelButton);
add(loginButton);
add(ImageLabel);
loginButton.addActionListener(new LoginActionListener(this));
randomNumberJLabel.addMouseListener(new RandomNumberListener(this));
cancelButton.addActionListener(new CancelLoginListener(this));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public JTextField getRandomField() {
return randomField;
}
public void setRandomField(JTextField randomField) {
this.randomField = randomField;
}
public JLabel getRandomNumber() {
return randomNumber;
}
public void setRandomNumber(JLabel randomNumber) {
this.randomNumber = randomNumber;
}
public JLabel getRandomNumberJLabel() {
return randomNumberJLabel;
}
public void setRandomNumberJLabel(JLabel randomNumberJLabel) {
this.randomNumberJLabel = randomNumberJLabel;
}
public JLabel getUsernameJLabel() {
return usernameJLabel;
}
public void setUsernameJLabel(JLabel usernameJLabel) {
this.usernameJLabel = usernameJLabel;
}
public JLabel getPasswordJLabel() {
return passwordJLabel;
}
public void setPasswordJLabel(JLabel passwordJLabel) {
this.passwordJLabel = passwordJLabel;
}
public JTextField getUsernameJTextField() {
return usernameJTextField;
}
public void setUsernameJTextField(JTextField usernameJTextField) {
this.usernameJTextField = usernameJTextField;
}
public JTextField getPasswordJTextField() {
return passwordJTextField;
}
public void setPasswordJTextField(JTextField passwordJTextField) {
this.passwordJTextField = passwordJTextField;
}
public JButton getLoginButton() {
return loginButton;
}
public void setLoginButton(JButton loginButton) {
this.loginButton = loginButton;
}
public JButton getCancelButton() {
return cancelButton;
}
public void setCancelButton(JButton cancelButton) {
this.cancelButton = cancelButton;
}
public static void main(String[] args) {
try {
javax.swing.UIManager.setLookAndFeel("com.jtattoo.plaf.bernstein.BernsteinLookAndFeel");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new LoginView();
}
}
4.3 主界面窗口
package com.view;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import com.control.CloseMainView;
public class MainView extends JFrame {
private JLabel barLabel = new JLabel();
private JLabel timejJLabel = new JLabel();
// 创建内部窗体
public static JDesktopPane rightDesktopPane = new JDesktopPane();
JDesktopPaneTree jDesktopPaneTree = new JDesktopPaneTree();
public MainView() {
// 设计窗体属性
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setTitle("超市商品管理系统");
setSize(950, 650);
setLayout(null);
setLocationRelativeTo(null);
setResizable(false);// 设置窗口的大小不能改变
rightDesktopPane.setOpaque(false);
rightDesktopPane.setBounds(250, 30, 750, 590);
barLabel.setBounds(700, 0, 200, 30);
timejJLabel.setBounds(500, 555, 180, 20);
jDesktopPaneTree.setBounds(0, 0, 250, 615);
// 添加菜单
add(barLabel);
add(rightDesktopPane);
add(jDesktopPaneTree);
rightDesktopPane.add(timejJLabel);
rightDesktopPane.setVisible(true);
jDesktopPaneTree.setVisible(true);
setVisible(true);
addWindowListener(new CloseMainView());
}
public JLabel getBarLabel() {
return barLabel;
}
public void setBarLabel(JLabel barLabel) {
this.barLabel = barLabel;
}
public JDesktopPaneTree getjDesktopPaneTree() {
return jDesktopPaneTree;
}
public void setjDesktopPaneTree(JDesktopPaneTree jDesktopPaneTree) {
this.jDesktopPaneTree = jDesktopPaneTree;
}
public JLabel getTimejJLabel() {
return timejJLabel;
}
public void setTimejJLabel(JLabel timejJLabel) {
this.timejJLabel = timejJLabel;
}
public static JDesktopPane getRightDesktopPane() {
return rightDesktopPane;
}
public static void setRightDesktopPane(JDesktopPane rightDesktopPane) {
MainView.rightDesktopPane = rightDesktopPane;
}
public static void main(String[] args) {
new MainView();
}
}
4.4 登录窗口监听
package com.control;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import com.model.UserDao;
import com.view.LoginView;
import com.view.MainView;
public class LoginActionListener implements ActionListener {
LoginView loginView;
UserDao userDao1 = new UserDao();
MainView mainView;
public LoginActionListener(MainView mainView) {
this.mainView = mainView;
}
//JDesktopPaneTree jDesktopPaneTree = new JDesktopPaneTree();
public LoginActionListener(LoginView loginView) {
this.loginView = loginView;
}
public void actionPerformed(ActionEvent e) {
String username = loginView.getUsernameJTextField().getText();
String password = loginView.getPasswordJTextField().getText();
String s = loginView.getRandomNumberJLabel().getText();
String s1 = loginView.getRandomField().getText();
if (username.length() == 0) {
JOptionPane.showMessageDialog(null, "用户名不能为空!");
return;
}
if (password.length() == 0) {
JOptionPane.showMessageDialog(null, "密码不能为空!");
return;
}
if (s1.length() == 0) {
JOptionPane.showMessageDialog(null, "请输入验证码!");
return;
}
if (s1.equals(s)) {
boolean b = userDao1.userDao(username, password);
ResultSet rs = userDao1.getResultSet(username, password);
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
int ss = Integer.parseInt(simpleDateFormat.format(date));
if (b) {
final MainView mainView = new MainView();// 登陆成功,跳转到主窗
new Thread() {
public void run() {
while (true) {
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy/MM/dd hh:mm:ss");
String date1 = simpleDateFormat.format(date);
mainView.getTimejJLabel().setText(date1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
loginView.dispose();
try {
while (rs.next()) {
if (ss > 0 && ss < 10) {
mainView.getBarLabel().setText(
"早上好!!!" + rs.getString(1));
}
if (ss >=10 && ss < 14) {
mainView.getBarLabel().setText(
"中午好!!!" + rs.getString(1));
}
if (ss >=14 && ss < 19) {
mainView.getBarLabel().setText(
"下午好!!!" + rs.getString(1));
}
if (ss >=19 && ss < 24) {
mainView.getBarLabel().setText(
"晚上好!!!" + rs.getString(1));
}
}
} catch (SQLException e1) {
e1.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
} else {
JOptionPane.showMessageDialog(null, "验证码错误!");
}
}
}
4.5 验证码监听
package com.control;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import com.view.LoginView;
public class RandomNumberListener implements MouseListener {
LoginView loginView;
public RandomNumberListener(LoginView loginView) {
this.loginView = loginView;
}
public void mouseClicked(MouseEvent e) {
String s = "abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQPSTWVWXYZ01234567890";
Random random = new Random();
char[] c = new char[4];
for (int i = 0; i < 4; i++) {
int intdex = random.nextInt(62);
c[i] = s.charAt(intdex);
}
loginView.getRandomNumberJLabel().setText(new String(c));
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
5、项目文件内容包含
6、项目获取
6.1 方式一
私聊或扫描下方名片联系获取项目文件。
6.2 方式二
点击此处直接获取项目文件。