|
用Java swing和MySQL寫了一個簡單的超市信息管理系統(tǒng)
0.png (43.59 KB, 下載次數(shù): 76)
下載附件
2018-9-13 16:22 上傳
源程序如下:
- 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("超市信息管理系統(tǒng)");
- 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("開始進(jìn)行查詢操作");
- 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("開始進(jìn)行添加操作");
- Add sa = new Add(this, "添加商品信息", true);
-
- System.out.println("添加成功");
- sm = new Model();
- jt.setModel(sm);
-
- } else if (arg0.getSource() == jb4) {
-
- System.out.println("開始進(jìn)行刪除操作");
-
- 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("開始進(jìn)行修改操作");
-
- 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);
- }
- }
-
- }
復(fù)制代碼
所有資料51hei提供下載:
superMarketSystem.zip
(1.84 MB, 下載次數(shù): 66)
2018-9-13 08:41 上傳
點擊文件名下載附件
5 下載積分: 黑幣 -5
|
評分
-
查看全部評分
|