package superMarketSystem;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main extends JFrame implements ActionListener {
JPanel jp1, jp2;
JLabel jl1, jl2;
JButton jb1, jb2, jb3, jb4,jb5;
JTable jt;
JScrollPane jsp;
JTextField jtf;
Model sm;
Statement stat = null;
PreparedStatement ps;
Connection ct = null;
ResultSet rs = null;
public Main() {
jp1 = new JPanel();
jtf = new JTextField(10);
jb1 = new JButton("查询");
jb1.addActionListener(this);
jl1 = new JLabel("请输入商品号:");
jp1.add(jl1);
jp1.add(jtf);
jp1.add(jb1);
jb2 = new JButton("添加");
jb2.addActionListener(this);
jb3 = new JButton("修改");
jb3.addActionListener(this);
jb4 = new JButton("删除");
jb4.addActionListener(this);
jb5 = new JButton("浏览");
jb5.addActionListener(this);
jp2 = new JPanel();
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
jp2.add(jb5);
sm = new Model();
jt = new JTable(sm);
jsp = new JScrollPane(jt);
this.add(jsp);
this.add(jp1, "North");
this.add(jp2, "South");
setTitle("超市信息管理系统");
setSize(800, 600);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Main test3 = new Main();
}
public void actionPerformed(ActionEvent arg0) {
if (arg0.getSource() == jb1) {
System.out.println("开始进行查询操作");
String name = this.jtf.getText().trim();
String sql = "select * from goods where goodsId = '" + name + "' ";
System.out.println("查询完成");
sm = new Model(sql);
jt.setModel(sm);
}
else if (arg0.getSource() == jb2) {
System.out.println("开始进行添加操作");
Add sa = new Add(this, "添加商品信息", true);
System.out.println("添加成功");
sm = new Model();
jt.setModel(sm);
} else if (arg0.getSource() == jb4) {
System.out.println("开始进行删除操作");
int rowNum = this.jt.getSelectedRow();
if (rowNum == -1) {
JOptionPane.showMessageDialog(this, "请选中一行");
return;
}
String goodsId = (String) sm.getValueAt(rowNum, 0);
System.out.println("已成功删除商品号:" + goodsId + "的信息");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/chaoshi?useSSL=false&serverTimezone=UTC";
String user = "root";
String passwd = "123356";
ct = DriverManager.getConnection(url, user, passwd);
System.out.println("连接成功");
ps = ct.prepareStatement("delete from goods where goodsId = ?");
ps.setString(1,goodsId);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (ps != null) {
ps.close();
ps = null;
}
if (ct != null) {
ct.close();
ct = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
sm = new Model();
jt.setModel(sm);
} else if (arg0.getSource() == jb3) {
System.out.println("开始进行修改操作");
int rowNum = this.jt.getSelectedRow();
if (rowNum == -1) {
JOptionPane.showMessageDialog(this, "请选择一行");
return;
}
String goodsId = (String) sm.getValueAt(rowNum, 0);
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/chaoshi?useSSL=false&serverTimezone=UTC";
String user = "root";
String passwd = "123356";
ct = DriverManager.getConnection(url, user, passwd);
System.out.println("连接成功");
ps = ct.prepareStatement("delete from goods where goodsId = ?");
ps.setString(1, goodsId);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (ps != null) {
ps.close();
ps = null;
}
if (ct != null) {
ct.close();
ct = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
Update su = new Update(this, "修改商品信息", true, sm, rowNum);
System.out.println("修改完成");
sm = new Model();
jt.setModel(sm);
}
else if(arg0.getSource() == jb5){
sm = new Model();
jt.setModel(sm);
}
}
}