import java.awt.*;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import javax.swing.table.DefaultTableModel;
public class LoginWindow {
private JFrame frame;
private JTextField txtUser;
private JPasswordField txtPassword;
private JRadioButton r1, r2;
private ButtonGroup bg;
private JComboBox<String> city;
private JButton btnLogin, btnReset;
private JCheckBox c1, c2, c3;
private MainPage mainPage;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new LoginWindow().show());
}
public LoginWindow() {
frame = new JFrame("电商购物平台 - 登录");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 400);
frame.setLocationRelativeTo(null);
frame.getContentPane().setLayout(null);
// 用户名标签和文本框
JLabel lblUser = new JLabel("账号:");
lblUser.setBounds(64, 46, 69, 15);
frame.getContentPane().add(lblUser);
txtUser = new JTextField();
txtUser.setBounds(167, 43, 193, 21);
frame.getContentPane().add(txtUser);
// 密码标签和密码框
JLabel lblPassword = new JLabel("密码:");
lblPassword.setBounds(64, 86, 69, 15);
frame.getContentPane().add(lblPassword);
txtPassword = new JPasswordField();
txtPassword.setBounds(167, 83, 193, 21);
frame.getContentPane().add(txtPassword);
// 性别单选按钮
r1 = new JRadioButton("男");
r1.setBounds(167, 123, 60, 21);
r1.setSelected(true);
frame.getContentPane().add(r1);
r2 = new JRadioButton("女");
r2.setBounds(247, 123, 60, 21);
frame.getContentPane().add(r2);
bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
// 城市下拉框
city = new JComboBox<>(new String[]{"", "北京", "上海", "天津", "石家庄"});
city.setBounds(167, 163, 193, 21);
frame.getContentPane().add(city);
JLabel lblCity = new JLabel("所在城市:");
lblCity.setBounds(64, 166, 69, 15);
frame.getContentPane().add(lblCity);
// 兴趣爱好复选框
c1 = new JCheckBox("文学");
c1.setBounds(167, 203, 60, 21);
frame.getContentPane().add(c1);
c2 = new JCheckBox("体育");
c2.setBounds(247, 203, 60, 21);
frame.getContentPane().add(c2);
c3 = new JCheckBox("音乐");
c3.setBounds(327, 203, 60, 21);
frame.getContentPane().add(c3);
JLabel lblAihao = new JLabel("个人爱好:");
lblAihao.setBounds(64, 206, 69, 15);
frame.getContentPane().add(lblAihao);
// 登录按钮
btnLogin = new JButton("登 录");
btnLogin.setBounds(170, 250, 80, 30);
btnLogin.setFont(new Font("微软雅黑", Font.BOLD, 14));
btnLogin.setForeground(Color.BLUE);
btnLogin.setBorder(BorderFactory.createLineBorder(Color.BLUE));
btnLogin.addActionListener(e -> login());
frame.getContentPane().add(btnLogin);
// 重置按钮
btnReset = new JButton("重 置");
btnReset.setBounds(260, 250, 80, 30);
btnReset.setFont(new Font("微软雅黑", Font.BOLD, 14));
btnReset.setForeground(Color.BLUE);
btnReset.setBorder(BorderFactory.createLineBorder(Color.BLUE));
btnReset.addActionListener(e -> reset());
frame.getContentPane().add(btnReset);
}
private void login() {
String name = txtUser.getText();
String password = new String(txtPassword.getPassword());
if (!name.isEmpty() && !password.isEmpty()) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 加载 JDBC 驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password");
String sql = "SELECT * FROM yonghu WHERE yhMC = ? AND yhXB = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
// 创建并显示主页面
if (mainPage == null) {
mainPage = new MainPage();
}
mainPage.setVisible(true);
frame.setVisible(false);
} else {
JOptionPane.showMessageDialog(frame, "用户名或密码错误!", "错误", JOptionPane.ERROR_MESSAGE);
}
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(frame, "未找到 MySQL JDBC 驱动!", "错误", JOptionPane.ERROR_MESSAGE);
} catch (SQLException e) {
JOptionPane.showMessageDialog(frame, "数据库连接失败!", "错误", JOptionPane.ERROR_MESSAGE);
} finally {
// 关闭资源
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} else {
JOptionPane.showMessageDialog(frame, "用户名和密码不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
private void reset() {
txtUser.setText("");
txtPassword.setText("");
r1.setSelected(true);
r2.setSelected(false);
city.setSelectedIndex(0);
c1.setSelected(false);
c2.setSelected(false);
c3.setSelected(false);
}
public void show() {
frame.setVisible(true);
}
// 主页面类
public class MainPage extends JFrame {
public MainPage() {
setTitle("电商购物平台 - 主页面");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// 添加主页面的内容
JLabel welcomeLabel = new JLabel("欢迎使用电商购物平台。");
welcomeLabel.setHorizontalAlignment(JLabel.CENTER);
welcomeLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
add(welcomeLabel);
// 创建菜单栏
JMenuBar menuBar = new JMenuBar();
// 创建菜单
JMenu productMenu = new JMenu("商品管理");
JMenu userMenu = new JMenu("用户管理");
JMenu orderMenu = new JMenu("订单管理");
JMenu purchaseMenu = new JMenu("购买");
// 创建菜单项
JMenuItem addProduct = new JMenuItem("商品新增");
JMenuItem modifyProduct = new JMenuItem("商品修改");
JMenuItem deleteProduct = new JMenuItem("商品删除");
JMenuItem queryProduct = new JMenuItem("商品查询");
JMenuItem addUser = new JMenuItem("用户新增");
JMenuItem modifyUser = new JMenuItem("用户修改");
JMenuItem deleteUser = new JMenuItem("用户删除");
JMenuItem queryUser = new JMenuItem("用户查询");
JMenuItem queryOrder = new JMenuItem("订单查询");
JMenuItem deleteOrder = new JMenuItem("订单删除");
JMenuItem modifyOrder = new JMenuItem("订单修改");
JMenuItem buyProduct = new JMenuItem("商品购买");
// 将菜单项添加到菜单
productMenu.add(addProduct);
productMenu.add(modifyProduct);
productMenu.add(deleteProduct);
productMenu.add(queryProduct);
userMenu.add(addUser);
userMenu.add(modifyUser);
userMenu.add(deleteUser);
userMenu.add(queryUser);
orderMenu.add(queryOrder);
orderMenu.add(deleteOrder);
orderMenu.add(modifyOrder);
purchaseMenu.add(buyProduct);
// 将菜单添加到菜单栏
menuBar.add(productMenu);
menuBar.add(userMenu);
menuBar.add(orderMenu);
menuBar.add(purchaseMenu);
// 设置菜单栏
setJMenuBar(menuBar);
// 添加菜单项的事件监听器
addProduct.addActionListener(e -> {
new AddProductFrame().setVisible(true);
});
modifyProduct.addActionListener(e -> {
new ModifyProductFrame().setVisible(true);
});
queryProduct.addActionListener(e -> {
new QueryProductFrame().setVisible(true);
});
// 其他菜单项的监听器可以类似地添加
}
}
// 商品新增窗口
public class AddProductFrame extends JFrame {
public AddProductFrame() {
setTitle("商品新增");
setSize(400, 600);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
// 商品ID
JLabel productIdLabel = new JLabel("商品ID:");
JTextField productIdText = new JTextField();
productIdText.setPreferredSize(new Dimension(200, 25));
JPanel productIdPanel = new JPanel();
productIdPanel.add(productIdLabel);
productIdPanel.add(productIdText);
// 商品名称
JLabel productNameLabel = new JLabel("商品名称:");
JTextField productNameText = new JTextField();
productNameText.setPreferredSize(new Dimension(200, 25));
JPanel productNamePanel = new JPanel();
productNamePanel.add(productNameLabel);
productNamePanel.add(productNameText);
// 商品价格
JLabel productPriceLabel = new JLabel("商品价格:");
JTextField productPriceText = new JTextField();
productPriceText.setPreferredSize(new Dimension(200, 25));
JPanel productPricePanel = new JPanel();
productPricePanel.add(productPriceLabel);
productPricePanel.add(productPriceText);
// 商品类别
JLabel productCategoryLabel = new JLabel("商品类别:");
JComboBox<String> productCategoryComboBox = new JComboBox<>(new String[]{"", "电子产品", "服装", "食品", "书籍"});
productCategoryComboBox.setPreferredSize(new Dimension(200, 25));
JPanel productCategoryPanel = new JPanel();
productCategoryPanel.add(productCategoryLabel);
productCategoryPanel.add(productCategoryComboBox);
// 商品产地
JLabel productOriginLabel = new JLabel("商品产地:");
JTextField productOriginText = new JTextField();
productOriginText.setPreferredSize(new Dimension(200, 25));
JPanel productOriginPanel = new JPanel();
productOriginPanel.add(productOriginLabel);
productOriginPanel.add(productOriginText);
// 商品简介
JLabel productDescriptionLabel = new JLabel("商品简介:");
JTextArea productDescriptionArea = new JTextArea(5, 25);
JScrollPane descriptionScrollPane = new JScrollPane(productDescriptionArea);
descriptionScrollPane.setPreferredSize(new Dimension(200, 100));
JPanel productDescriptionPanel = new JPanel();
productDescriptionPanel.add(productDescriptionLabel);
productDescriptionPanel.add(descriptionScrollPane);
// 按钮面板
JButton saveButton = new JButton("保存");
JButton cancelButton = new JButton("取消");
JPanel buttonPanel = new JPanel();
buttonPanel.add(saveButton);
buttonPanel.add(cancelButton);
// 添加所有面板到主面板
panel.add(productIdPanel);
panel.add(productNamePanel);
panel.add(productPricePanel);
panel.add(productCategoryPanel);
panel.add(productOriginPanel);
panel.add(productDescriptionPanel);
panel.add(buttonPanel);
// 将主面板添加到窗口
add(panel);
// 添加按钮事件监听
saveButton.addActionListener(e -> {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password");
String sql = "INSERT INTO shangpin (spMC, spLB, spJG, spCD, spJJ) VALUES (?, ?, ?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, productNameText.getText());
pstmt.setString(2, productCategoryComboBox.getSelectedItem().toString());
pstmt.setFloat(3, Float.parseFloat(productPriceText.getText()));
pstmt.setString(4, productOriginText.getText());
pstmt.setString(5, productDescriptionArea.getText());
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(this, "商品新增成功!");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "商品新增失败!");
ex.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
cancelButton.addActionListener(e -> {
dispose();
});
}
}
// 商品修改窗口
public class ModifyProductFrame extends JFrame {
public ModifyProductFrame() {
setTitle("商品修改");
setSize(400, 600);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
// 商品ID查询
JLabel searchIdLabel = new JLabel("请输入商品ID:");
JTextField searchIdText = new JTextField();
searchIdText.setPreferredSize(new Dimension(200, 25));
JButton searchButton = new JButton("查询");
JPanel searchPanel = new JPanel();
searchPanel.add(searchIdLabel);
searchPanel.add(searchIdText);
searchPanel.add(searchButton);
// 商品ID显示
JLabel productIdLabel = new JLabel("商品ID:");
JTextField productIdText = new JTextField();
productIdText.setEditable(false);
productIdText.setPreferredSize(new Dimension(200, 25));
JPanel productIdPanel = new JPanel();
productIdPanel.add(productIdLabel);
productIdPanel.add(productIdText);
// 商品名称
JLabel productNameLabel = new JLabel("商品名称:");
JTextField productNameText = new JTextField();
productNameText.setPreferredSize(new Dimension(200, 25));
JPanel productNamePanel = new JPanel();
productNamePanel.add(productNameLabel);
productNamePanel.add(productNameText);
// 商品价格
JLabel productPriceLabel = new JLabel("商品价格:");
JTextField productPriceText = new JTextField();
productPriceText.setPreferredSize(new Dimension(200, 25));
JPanel productPricePanel = new JPanel();
productPricePanel.add(productPriceLabel);
productPricePanel.add(productPriceText);
// 商品类别
JLabel productCategoryLabel = new JLabel("商品类别:");
JComboBox<String> productCategoryComboBox = new JComboBox<>(new String[]{"", "电子产品", "服装", "食品", "书籍"});
productCategoryComboBox.setPreferredSize(new Dimension(200, 25));
JPanel productCategoryPanel = new JPanel();
productCategoryPanel.add(productCategoryLabel);
productCategoryPanel.add(productCategoryComboBox);
// 商品产地
JLabel productOriginLabel = new JLabel("商品产地:");
JTextField productOriginText = new JTextField();
productOriginText.setPreferredSize(new Dimension(200, 25));
JPanel productOriginPanel = new JPanel();
productOriginPanel.add(productOriginLabel);
productOriginPanel.add(productOriginText);
// 商品简介
JLabel productDescriptionLabel = new JLabel("商品简介:");
JTextArea productDescriptionArea = new JTextArea(5, 25);
JScrollPane descriptionScrollPane = new JScrollPane(productDescriptionArea);
descriptionScrollPane.setPreferredSize(new Dimension(200, 100));
JPanel productDescriptionPanel = new JPanel();
productDescriptionPanel.add(productDescriptionLabel);
productDescriptionPanel.add(descriptionScrollPane);
// 按钮面板
JButton updateButton = new JButton("修改");
JButton deleteButton = new JButton("删除");
JButton exitButton = new JButton("取消");
JPanel buttonPanel = new JPanel();
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
buttonPanel.add(exitButton);
// 添加所有面板到主面板
panel.add(searchPanel);
panel.add(productIdPanel);
panel.add(productNamePanel);
panel.add(productPricePanel);
panel.add(productCategoryPanel);
panel.add(productOriginPanel);
panel.add(productDescriptionPanel);
panel.add(buttonPanel);
// 将主面板添加到窗口
add(panel);
// 添加按钮事件监听
searchButton.addActionListener(e -> {
String productId = searchIdText.getText();
if (productId.isEmpty()) {
JOptionPane.showMessageDialog(this, "请输入商品ID!");
return;
}
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password");
String sql = "SELECT * FROM shangpin WHERE spID = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, productId);
rs = pstmt.executeQuery();
if (rs.next()) {
productIdText.setText(rs.getString("spID"));
productNameText.setText(rs.getString("spMC"));
productCategoryComboBox.setSelectedItem(rs.getString("spLB"));
productPriceText.setText(rs.getString("spJG"));
productOriginText.setText(rs.getString("spCD"));
productDescriptionArea.setText(rs.getString("spJJ"));
} else {
JOptionPane.showMessageDialog(this, "未找到商品ID为 " + productId + " 的商品信息");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "查询失败!");
ex.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
updateButton.addActionListener(e -> {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password");
String sql = "UPDATE shangpin SET spMC = ?, spLB = ?, spJG = ?, spCD = ?, spJJ = ? WHERE spID = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, productNameText.getText());
pstmt.setString(2, productCategoryComboBox.getSelectedItem().toString());
pstmt.setFloat(3, Float.parseFloat(productPriceText.getText()));
pstmt.setString(4, productOriginText.getText());
pstmt.setString(5, productDescriptionArea.getText());
pstmt.setString(6, productIdText.getText());
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(this, "商品修改成功!");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "商品修改失败!");
ex.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
deleteButton.addActionListener(e -> {
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password");
String sql = "DELETE FROM shangpin WHERE spID = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, productIdText.getText());
int rowsAffected = pstmt.executeUpdate();
if (rowsAffected > 0) {
JOptionPane.showMessageDialog(this, "商品删除成功!");
dispose();
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "商品删除失败!");
ex.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
exitButton.addActionListener(e -> {
dispose();
});
}
}
// 商品查询窗口
public class QueryProductFrame extends JFrame {
public QueryProductFrame() {
setTitle("商品查询");
setSize(800, 600);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
// 北部面板 - 查询条件
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
JLabel conditionLabel = new JLabel("查询条件:");
northPanel.add(conditionLabel);
JButton queryAllButton = new JButton("查询所有商品信息");
northPanel.add(queryAllButton);
JButton queryDepartmentButton = new JButton("查询部门员工信息");
northPanel.add(queryDepartmentButton);
JLabel departmentLabel = new JLabel("部门:");
JComboBox<String> departmentComboBox = new JComboBox<>(new String[]{"", "设计部", "开发部", "市场部", "人事部"});
northPanel.add(departmentLabel);
northPanel.add(departmentComboBox);
// 中部面板 - 查询结果表格
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout());
String[] columnNames = {"商品ID", "商品名称", "商品类别", "商品价格", "商品产地", "商品简介"};
Object[][] data = {};
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
centerPanel.add(scrollPane, BorderLayout.CENTER);
// 添加面板到窗口
add(northPanel, BorderLayout.NORTH);
add(centerPanel, BorderLayout.CENTER);
// 添加按钮事件监听
queryAllButton.addActionListener(e -> {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shangpin", "your_username", "your_password");
String sql = "SELECT * FROM shangpin";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
// 获取查询结果并更新表格数据
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
Object[][] newData = new Object[0][];
int rowCount = 0;
while (rs.next()) {
Object[] row = new Object[columnCount];
for (int i = 0; i < columnCount; i++) {
row[i] = rs.getObject(i + 1);
}
newData = Arrays.copyOf(newData, newData.length + 1);
newData[newData.length - 1] = row;
rowCount++;
}
table.setModel(new DefaultTableModel(newData, columnNames));
} catch (SQLException ex) {
JOptionPane.showMessageDialog(this, "查询所有商品信息失败!");
ex.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
});
queryDepartmentButton.addActionListener(e -> {
String department = departmentComboBox.getSelectedItem().toString();
if (department.isEmpty()) {
JOptionPane.showMessageDialog(this, "请选择部门!");
return;
}
JOptionPane.showMessageDialog(this, "查询 " + department + " 的商品信息成功!");
});
}
}
}为什么显示无法找到jdbc
最新发布