找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 7932|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

超市信息管理系統(tǒng)(Java swing+MySQL)源碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:397290 發(fā)表于 2018-9-13 08:42 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
用Java swing和MySQL寫了一個簡單的超市信息管理系統(tǒng)


源程序如下:
  1. package superMarketSystem;

  2. import javax.swing.*;
  3. import java.util.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.sql.Connection;
  7. import java.sql.Driver;
  8. import java.sql.DriverManager;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.Statement;

  12. public  class Main extends JFrame implements ActionListener {
  13.         
  14.         JPanel jp1, jp2;
  15.         JLabel jl1, jl2;
  16.         JButton jb1, jb2, jb3, jb4,jb5;
  17.         JTable jt;
  18.         JScrollPane jsp;
  19.         JTextField jtf;
  20.         
  21.         Model sm;
  22.         
  23.         
  24.         Statement stat = null;
  25.         PreparedStatement ps;
  26.         Connection ct = null;
  27.         ResultSet rs = null;
  28.                                                                                                 
  29.         
  30.         public Main() {
  31.                
  32.                
  33.                
  34.                 jp1 = new JPanel();
  35.                 jtf = new JTextField(10);
  36.                 jb1 = new JButton("查詢");
  37.                 jb1.addActionListener(this);
  38.                 jl1 = new JLabel("請輸入商品號:");

  39.                 jp1.add(jl1);
  40.                 jp1.add(jtf);
  41.                 jp1.add(jb1);

  42.                 jb2 = new JButton("添加");
  43.                 jb2.addActionListener(this);
  44.                 jb3 = new JButton("修改");
  45.                 jb3.addActionListener(this);
  46.                 jb4 = new JButton("刪除");
  47.                 jb4.addActionListener(this);
  48.                 jb5 = new JButton("瀏覽");
  49.                 jb5.addActionListener(this);

  50.                 jp2 = new JPanel();
  51.                 jp2.add(jb2);
  52.                 jp2.add(jb3);
  53.                 jp2.add(jb4);
  54.                 jp2.add(jb5);



  55.                 sm = new Model();



  56.                 jt = new JTable(sm);

  57.                 jsp = new JScrollPane(jt);



  58.                 this.add(jsp);
  59.                 this.add(jp1, "North");
  60.                 this.add(jp2, "South");
  61.                
  62.                 setTitle("超市信息管理系統(tǒng)");
  63.                 setSize(800, 600);

  64.                 setVisible(true);

  65.                 setDefaultCloseOperation(EXIT_ON_CLOSE);

  66.                

  67.         }

  68.         public static void main(String[] args) {
  69.                 Main test3 = new Main();
  70.         }
  71.         
  72.         
  73.         public void actionPerformed(ActionEvent arg0) {
  74.                
  75.                
  76.                 if (arg0.getSource() == jb1) {
  77.                         
  78.                         System.out.println("開始進(jìn)行查詢操作");
  79.                         String name = this.jtf.getText().trim();
  80.                         
  81.                         
  82.                         String sql = "select * from goods where goodsId = '" + name + "' ";
  83.                         System.out.println("查詢完成");
  84.                         
  85.                         
  86.                         sm = new Model(sql);
  87.                         
  88.                         
  89.                         jt.setModel(sm);

  90.                 }

  91.                
  92.                 else if (arg0.getSource() == jb2) {
  93.                         System.out.println("開始進(jìn)行添加操作");
  94.                         Add sa = new Add(this, "添加商品信息", true);

  95.                         
  96.                         System.out.println("添加成功");
  97.                         sm = new Model();
  98.                         jt.setModel(sm);

  99.                 } else if (arg0.getSource() == jb4) {
  100.                         
  101.                         System.out.println("開始進(jìn)行刪除操作");
  102.                         
  103.                         int rowNum = this.jt.getSelectedRow();
  104.                         
  105.                         
  106.                         if (rowNum == -1) {
  107.                                 
  108.                                 JOptionPane.showMessageDialog(this, "請選中一行");
  109.                                 return;
  110.                         }
  111.                         
  112.                         String goodsId = (String) sm.getValueAt(rowNum, 0);
  113.                         System.out.println("已成功刪除商品號:" + goodsId + "的信息");

  114.                         
  115.                         try {
  116.                                 
  117.                                 Class.forName("com.mysql.cj.jdbc.Driver");
  118.                                 
  119.                                 String url = "jdbc:mysql://localhost:3306/chaoshi?useSSL=false&serverTimezone=UTC";
  120.                                 String user = "root";
  121.                                 String passwd = "123356";

  122.                                 ct = DriverManager.getConnection(url, user, passwd);
  123.                                 System.out.println("連接成功");
  124.                                 ps = ct.prepareStatement("delete from goods where goodsId = ?");
  125.                                 ps.setString(1,goodsId);
  126.                                 ps.executeUpdate();

  127.                         } catch (Exception e) {
  128.                                 e.printStackTrace();
  129.                         } finally {
  130.                                 try {
  131.                                         if (rs != null) {
  132.                                                 rs.close();
  133.                                                 rs = null;

  134.                                         }
  135.                                         if (ps != null) {
  136.                                                 ps.close();
  137.                                                 ps = null;
  138.                                         }
  139.                                         if (ct != null) {
  140.                                                 ct.close();
  141.                                                 ct = null;
  142.                                         }
  143.                                 } catch (Exception e) {
  144.                                         e.printStackTrace();
  145.                                 }
  146.                         }
  147.                         sm = new Model();
  148.                         
  149.                         jt.setModel(sm);
  150.                 } else if (arg0.getSource() == jb3) {
  151.                         
  152.                         System.out.println("開始進(jìn)行修改操作");
  153.                         
  154.                         int rowNum = this.jt.getSelectedRow();
  155.                         if (rowNum == -1) {
  156.                                 
  157.                                 JOptionPane.showMessageDialog(this, "請選擇一行");
  158.                                 return;
  159.                         }
  160.                         
  161.                         String goodsId = (String) sm.getValueAt(rowNum, 0);

  162.                         
  163.                         try {
  164.                                 
  165.                                 Class.forName("com.mysql.cj.jdbc.Driver");
  166.                                 
  167.                                 String url = "jdbc:mysql://localhost:3306/chaoshi?useSSL=false&serverTimezone=UTC";
  168.                                 String user = "root";
  169.                                 String passwd = "123356";

  170.                                 ct = DriverManager.getConnection(url, user, passwd);
  171.                                 System.out.println("連接成功");
  172.                                 ps = ct.prepareStatement("delete from goods where goodsId = ?");
  173.                                 ps.setString(1, goodsId);
  174.                                 ps.executeUpdate();

  175.                         } catch (Exception e) {
  176.                                 e.printStackTrace();
  177.                         } finally {
  178.                                 try {
  179.                                         if (rs != null) {
  180.                                                 rs.close();
  181.                                                 rs = null;

  182.                                         }
  183.                                         if (ps != null) {
  184.                                                 ps.close();
  185.                                                 ps = null;
  186.                                         }
  187.                                         if (ct != null) {
  188.                                                 ct.close();
  189.                                                 ct = null;
  190.                                         }
  191.                                 } catch (Exception e) {
  192.                                         e.printStackTrace();
  193.                                 }
  194.                         }
  195.                         Update su = new Update(this, "修改商品信息", true, sm, rowNum);
  196.                         
  197.                         System.out.println("修改完成");
  198.                         
  199.                         sm = new Model();
  200.                         
  201.                         
  202.                         jt.setModel(sm);
  203.                 }
  204.                 else if(arg0.getSource() == jb5){
  205.                         
  206.                                     
  207.                         sm = new Model();
  208.                                                 
  209.                                                 
  210.                         jt.setModel(sm);

  211.                 }
  212.         }
  213.         
  214. }
復(fù)制代碼

所有資料51hei提供下載:
superMarketSystem.zip (1.84 MB, 下載次數(shù): 66)




評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:323130 發(fā)表于 2019-8-27 11:04 | 只看該作者
我在eclipse中運(yùn)行,結(jié)果出現(xiàn)這個

項目“superMarketSystem”缺少必需的庫:”E:\mysql-connector-java-8.0.12\mysql-connector-java-8.0.12.jar”        superMarketSystem               
請問咋解決?
回復(fù)

使用道具 舉報

板凳
ID:766192 發(fā)表于 2020-6-22 16:27 | 只看該作者
疇昔至稚 發(fā)表于 2019-8-27 11:04
我在eclipse中運(yùn)行,結(jié)果出現(xiàn)這個

項目“superMarketSystem”缺少必需的庫:”E:\mysql-connector-java- ...

在瀏覽器中搜索eclipse添加數(shù)據(jù)庫可以找到詳細(xì)步驟,數(shù)據(jù)庫作者壓縮包都有
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表