赞
踩
目录
功能介绍:
1.登录不同管理员账号
2.查看商品信息
3.查看每一单商品的交易信息
4.上架新商品
5.下架旧商品
6.售卖商品或进货商品
1.商品信息表格:
表头有商品名,进价,售价,是否打折。。。
2.商品交易详情:
表头有交易时间,交易数量,进/售货,商品名
3.操作员信息表格
表头有账号,密码,交易额
mysql创建表友情链接
mysql 建表语句 及完整案例_大蛇王的博客-CSDN博客_mysql建表语句
idea导入mysql驱动包方法友情链接Intellij IDEA 添加jar包的三种方式_inzaghihalo的博客-CSDN博客_idea导入jar包
- setTitle("超市管理系统");
- JMenu jm1=new JMenu("商品信息");
- JMenu jm2=new JMenu("商品管理");
- JMenuItem jmi1=new JMenu("进货商品信息");
- JMenuItem jmi2=new JMenu("商品交易信息");
- JMenuItem jmi3=new JMenu("上架商品");
- JMenuItem jmi4=new JMenu("下架商品");
- jm1.add(jmi1);
- jm1.add(jmi2);
- jm2.add(jmi3);
- jm2.add(jmi4);
- JMenuBar jmb=new JMenuBar();
- jmb.add(jm1);
- jmb.add(jm2);
- setJMenuBar(jmb);
- setBounds(300,200,400,300);
- setLayout(null);
- JLabel l=new JLabel("超市管理系统");
- JLabel l1=new JLabel("账号:");
- JLabel l2=new JLabel("密码:");
- jtf=new JTextField(20);
- jwf=new JPasswordField(20);
- jwf.setEchoChar('*');
- b=new JButton("登录");
- l.setBounds(130,20,150,50);
- l.setForeground(Color.red);
- l.setFont(new Font("宋体",Font.BOLD,23));
- l1.setBounds(120,75,100,40);
- l2.setBounds(120,110,100,40);
- jtf.setBounds(180,75,100,30);
- jwf.setBounds(180,110,100,30);
- b.setBounds(170,170,70,40);
- add(l);
- add(l1);
- add(l2);
- add(jtf);
- add(jwf);
- add(b);
- setLayout(null);
- JLabel l1=new JLabel("商品名称:");
- JLabel l2=new JLabel("进 价:");
- JLabel l3=new JLabel("售 价:");
- JLabel l4=new JLabel("是否打折:");
- JTextField jtf1=new JTextField(20);
- JTextField jtf2=new JTextField(20);
- JTextField jtf3=new JTextField(20);
- JRadioButton jrb1=new JRadioButton("是");
- JRadioButton jrb2=new JRadioButton("否");
- jrb2.setSelected(true);
- JButton b=new JButton("确认");
- ButtonGroup bg=new ButtonGroup();
- bg.add(jrb1);
- bg.add(jrb2);
- l1.setBounds(20,20,70,30);
- l2.setBounds(20,70,70,30);
- l3.setBounds(20,120,70,30);
- l4.setBounds(20,170,70,30);
- jtf1.setBounds(90,25,90,20);
- jtf2.setBounds(90,75,90,20);
- jtf3.setBounds(90,125,90,20);
- jrb1.setBounds(90,175,70,20);
- jrb2.setBounds(140,175,70,20);
- b.setBounds(200,75,70,30);
- add(l1);
- add(l2);
- add(l3);
- add(l4);
- add(jtf1);
- add(jtf2);
- add(jtf3);
- add(jrb1);
- add(jrb2);
- add(b);
- setLayout(null);
- JLabel l=new JLabel("选择需要下架的商品:");
- JComboBox jcb=new JComboBox(jdbc.str2);
- JButton b=new JButton("确定");
- l.setBounds(100,70,130,30);
- jcb.setBounds(250,70,70,30);
- b.setBounds(150,120,70,40);
- add(l);
- add(jcb);
- add(b);
- JLabel l1=new JLabel("选择进售货");
- String []str1={"进货","销售"};
- JComboBox jcb1=new JComboBox(str1);
- JLabel l2=new JLabel("选择商品");
- JComboBox jcb2=new JComboBox(new JDBC().str2);
- JLabel l3=new JLabel("选择件数");
- JTextField jtf=new JTextField(20);
- JButton b=new JButton("确认");
- l1.setBounds(20,20,70,30);
- jcb1.setBounds(120,20,100,30);
- l2.setBounds(20,80,70,30);
- jcb2.setBounds(120,80,100,30);
- l3.setBounds(20,150,70,30);
- jtf.setBounds(120,150,100,30);
- b.setBounds(260,80,70,30);
- add(l1);
- add(l2);
- add(l3);
- add(jcb1);
- add(jcb2);
- add(jtf);
- add(b);
- setLayout(null);
- JTable jt=new JTable(str,name);
- jt.setPreferredScrollableViewportSize(new Dimension(350,200));
- JScrollPane jsp=new JScrollPane(jt);
- add(jsp,BorderLayout.CENTER);
通过Class.forName("com.mysql.jdbc.Driver");
初始化驱动类com.mysql.jdbc.Driver
就在 mysql-connector-java-5.0.8-bin.jar中
示例代码:
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
-
- public class TestJDBC {
- public static void main(String[] args) {
-
- try {
- Class.forName("com.mysql.jdbc.Driver");
-
- // 建立与数据库的Connection连接
- // 这里需要提供:
- // 数据库所处于的ip:127.0.0.1 (本机)
- // 数据库的端口号: 3306 (mysql专用端口号)
- // 数据库名称 how2java
- // 编码方式 UTF-8
- // 账号 root
- // 密码 admin
-
- Connection c = DriverManager
- .getConnection(
- "jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
- "root", "admin");
-
- System.out.println("连接成功,获取连接对象: " + c);
-
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
- }
示例代码
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- public class TestJDBC {
- public static void main(String[] args) {
-
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
-
- try (
- Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
- "root", "admin");
- Statement s = c.createStatement();
- )
- {
- String sql = "insert into hero values(null," + "'提莫'" + "," + 313.0f + "," + 50 + ")";//增
- //String sql = "delete from hero where id = 5";删
- // String sql = "update hero set name = 'name 5' where id = 3";改
- s.execute(sql);
-
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
示例代码
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- public class TestJDBC {
- public static void main(String[] args) {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
-
- try (Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/how2java?characterEncoding=UTF-8",
- "root", "admin"); Statement s = c.createStatement();) {
-
- String sql = "select * from hero";
-
- // 执行查询语句,并把结果集返回给ResultSet
- ResultSet rs = s.executeQuery(sql);
- while (rs.next()) {
- int id = rs.getInt("id");// 可以使用字段名
- String name = rs.getString(2);// 也可以使用字段的顺序
- float hp = rs.getFloat("hp");
- int damage = rs.getInt(4);
- System.out.printf("%d\t%s\t%f\t%d%n", id, name, hp, damage);
- }
- // 不一定要在这里关闭ReultSet,因为Statement关闭的时候,会自动关闭ResultSet
- // rs.close();
-
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
关于采集数据及建立表格友情链接
java的JTable表格的制作,读取数据库表中数据放到JTable表格中_上官绝岚的博客-CSDN博客
对于在窗体中要进行的操作我们要验证是否已经登录,对于没有登录的状态不能访问打开其他面板。并且要记录下来是哪个账号在登录。
需定义全局变量
- boolean status=false;//默认是未登录
- String Iname;
我们需要展示不同的面板在同一个窗体里,于是需要切换面板。
详细步骤:
在面板中有很多按钮,我们点击按钮或菜单栏来执行一次事件
示例代码
- jmi1.addItemListener(e -> {//进货商品信息
- if (status){
- setContentPane(cp);
- revalidate();
- }
- });
确保数据库中已经建立各个表格,已导入驱动包。
1.创建如下工程以及类
2.复制以下各类代码
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- public class Admin {
- public Map<String,String> map=new HashMap<>();
- public List<operator> list=new ArrayList<>();
- public Admin(){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- try (
- Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mysql?characterEncoding=UTF-8",
- "root", "root")) {
- PreparedStatement ps = c.prepareStatement("select *from operator");
- ResultSet res= ps.executeQuery();//获取的结果
- res.last();
- int row=res.getRow();
- res.beforeFirst();
- res.next();
- for (int i=0;i<row;i++){
- int id=Integer.parseInt(res.getString("id"));
- String name=res.getString("name");
- String password=res.getString("pwd");
- String sum=res.getString("sum");
- map.put(name,password);
- list.add(new operator(id,name,password,sum));
- res.next();
- }
- }
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- }
- }
- }
- import javax.swing.*;
- import java.awt.*;
-
- public class commodityPanel extends JPanel {
- public commodityPanel(){
- JDBC jdbc=new JDBC();
- String []name={"id","商品名","进价","售价","是否打折"};
- JTable jt=new JTable(jdbc.str1,name);
- jt.setPreferredScrollableViewportSize(new Dimension(350,200));
- JScrollPane jsp=new JScrollPane(jt);
- add(jsp,BorderLayout.CENTER);
- }
-
- }
- import javax.swing.*;
- import java.util.Objects;
-
- public class Frame extends JFrame {
- boolean status=false;
- String Iname;
- Admin admin=new Admin();
- public Frame(){
- setTitle("超市管理系统");
- JMenu jm1=new JMenu("商品信息");
- JMenu jm2=new JMenu("商品管理");
- JMenuItem jmi1=new JMenu("进货商品信息");
- JMenuItem jmi2=new JMenu("商品交易信息");
- JMenuItem jmi3=new JMenu("上架商品");
- JMenuItem jmi4=new JMenu("下架商品");
- JMenuItem jmi5=new JMenu("交易商品");
- jm1.add(jmi1);
- jm1.add(jmi2);
- jm2.add(jmi3);
- jm2.add(jmi4);
- jm2.add(jmi5);
- JMenuBar jmb=new JMenuBar();
- jmb.add(jm1);
- jmb.add(jm2);
- setJMenuBar(jmb);
- setBounds(300,200,400,300);
- setLayout(null);
- Login_panel jl=new Login_panel();
- commodityPanel cp=new commodityPanel();
- setContentPane(jl);
- jmi1.addItemListener(e -> {//进货商品信息
- if (status){
- setContentPane(cp);
- revalidate();
- }
- });
- jmi2.addItemListener(e -> {//商品交易详情
- if (status){
- setContentPane(new Transaction_panel());
- revalidate();
- pack();
- }
- });
- jmi3.addItemListener(e -> {
- if (status){
- setContentPane(new Put_panel());
- revalidate();
- }
- });
- jmi4.addItemListener(e -> {
- if (status){
- setContentPane(new Off_panel());
- revalidate();
- }
- });
- jmi5.addItemListener(e -> {
- if (status){
- setContentPane(new Operation_panel(Iname));
- revalidate();
- }
- });
- jl.b.addActionListener(e -> {//售货员登录
- if (Objects.equals(admin.map.get(jl.jtf.getText()), jl.jwf.getText()) &&admin.map.containsKey(jl.jtf.getText())) {
- status=true;
- Iname=jl.jtf.getText();
- setContentPane(new Operation_panel(jl.jtf.getText()));
- revalidate();
- JOptionPane.showMessageDialog(null,"登陆成功","提示",JOptionPane.WARNING_MESSAGE);
- }else JOptionPane.showMessageDialog(null,"账号或密码错误","警告",JOptionPane.ERROR_MESSAGE);
- });
- setVisible(true);
- }
- }
- import java.sql.*;
- import java.util.HashMap;
- import java.util.Map;
-
- public class JDBC {
- public String[][]str1;
- public String[]str2;
- public Map<String,String> jin=new HashMap<>();
- public Map<String,String> shou=new HashMap<>();
- public JDBC(){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- try (
- Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mysql?characterEncoding=UTF-8",
- "root", "root")) {
- PreparedStatement ps = c.prepareStatement("select *from store");
- ResultSet res= ps.executeQuery();//获取的结果
- res.last();
- int row=res.getRow();
- res.beforeFirst();
- str1=new String[row][5];
- str2=new String[row];
- res.next();
- for (int i=0;i<row;i++){
- str1[i][0]=res.getString("id");
- str1[i][1]=res.getString("commodityname");
- str2[i]=str1[i][1];
- str1[i][2]=res.getString("Unit_Price");
- jin.put(str1[i][1],str1[i][2]);
- str1[i][3]=res.getString("price");
- shou.put(str1[i][1],str1[i][3]);
- str1[i][4]=res.getString("Discount");
- res.next();
- }
- }
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- }
- }
- }
- import javax.swing.*;
- import java.awt.*;
-
- public class Login_panel extends JPanel {
- public JButton b;
- public JTextField jtf;
- public JPasswordField jwf;
- public Login_panel(){
- JLabel l=new JLabel("超市管理系统");
- JLabel l1=new JLabel("账号:");
- JLabel l2=new JLabel("密码:");
- jtf=new JTextField(20);
- jwf=new JPasswordField(20);
- jwf.setEchoChar('*');
- b=new JButton("登录");
- l.setBounds(130,20,150,50);
- l.setForeground(Color.red);
- l.setFont(new Font("宋体",Font.BOLD,23));
- l1.setBounds(120,75,100,40);
- l2.setBounds(120,110,100,40);
- jtf.setBounds(180,75,100,30);
- jwf.setBounds(180,110,100,30);
- b.setBounds(170,170,70,40);
- add(l);
- add(l1);
- add(l2);
- add(jtf);
- add(jwf);
- add(b);
- setLayout(null);
- }
- }
- import javax.swing.*;
-
- public class Off_panel extends JPanel {
- JDBC jdbc=new JDBC();
- public Off_panel(){
- JLabel l=new JLabel("选择需要下架的商品:");
- JComboBox jcb=new JComboBox(jdbc.str2);
- JButton b=new JButton("确定");
- l.setBounds(100,70,130,30);
- jcb.setBounds(250,70,70,30);
- b.setBounds(150,120,70,40);
- add(l);
- add(jcb);
- add(b);
- b.addActionListener(e -> {
- String name= (String) jcb.getSelectedItem();
- String url="delete from store where commodityname = '"+name+"'";
- new Transaction(url);
- JOptionPane.showMessageDialog(null,"已下架","提示",JOptionPane.INFORMATION_MESSAGE);
- });
- }
- }
- import javax.swing.*;
- import java.math.BigDecimal;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Objects;
- public class Operation_panel extends JPanel {
- Admin admin=new Admin();
- JDBC jdbc=new JDBC();
- public String username;
- public Operation_panel(String username){
- this.username=username;
- JLabel l1=new JLabel("选择进售货");
- String []str1={"进货","销售"};
- JComboBox jcb1=new JComboBox(str1);
- JLabel l2=new JLabel("选择商品");
- JComboBox jcb2=new JComboBox(new JDBC().str2);
- JLabel l3=new JLabel("选择件数");
- JTextField jtf=new JTextField(20);
- JButton b=new JButton("确认");
- l1.setBounds(20,20,70,30);
- jcb1.setBounds(120,20,100,30);
- l2.setBounds(20,80,70,30);
- jcb2.setBounds(120,80,100,30);
- l3.setBounds(20,150,70,30);
- jtf.setBounds(120,150,100,30);
- b.setBounds(260,80,70,30);
- b.addActionListener(e -> {
- Date date=new Date();
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy:MM:dd HH-mm");
- String str= sdf.format(date);
- String str2=jtf.getText();//个数
- String str3= (String) jcb1.getSelectedItem();//进货/售货
- String str4= (String) jcb2.getSelectedItem();//商品名
- String sum="1";
- for (int i=0;i<admin.list.size();i++){
- if (Objects.equals(admin.list.get(i).name, username))sum=String.valueOf(admin.list.get(i).sum);
- }
- String jj=jdbc.jin.get(str4);
- String sj=jdbc.shou.get(str4);
- BigDecimal bd1=new BigDecimal(str2).multiply(new BigDecimal(jj));
- BigDecimal bd2=new BigDecimal(str2).multiply(new BigDecimal(sj));
- if (str3.equals("进货")){
- sum=(String.valueOf(new BigDecimal(sum).subtract(bd1)));
- }else {
- sum=String.valueOf(new BigDecimal(sum).add(bd2));
- }
- String url1="insert into shell values (null, '"+str+"','" +str2+"','"+str3+"','" +str4+"')";
- System.out.println(username);
- String url2="update operator set sum = '"+sum+"' where name= '"+username+"'";
- System.out.println(url2);
- new Transaction(url1);
- new Transaction(url2);
- });
- add(l1);
- add(l2);
- add(l3);
- add(jcb1);
- add(jcb2);
- add(jtf);
- add(b);
- setLayout(null);
- }
- }
- public class operator {
- int id;
- String name;
- String password;
- String sum;
- public operator(int id,String name,String password,String sum){
- this.id=id;
- this.name=name;
- this.password=password;
- this.sum=sum;
- }
- }
- import javax.swing.*;
-
- public class Put_panel extends JPanel {
- public Put_panel(){
- JLabel l1=new JLabel("商品名称:");
- JLabel l2=new JLabel("进 价:");
- JLabel l3=new JLabel("售 价:");
- JLabel l4=new JLabel("是否打折:");
- JTextField jtf1=new JTextField(20);
- JTextField jtf2=new JTextField(20);
- JTextField jtf3=new JTextField(20);
- JRadioButton jrb1=new JRadioButton("是");
- JRadioButton jrb2=new JRadioButton("否");
- jrb2.setSelected(true);
- JButton b=new JButton("确认");
- ButtonGroup bg=new ButtonGroup();
- bg.add(jrb1);
- bg.add(jrb2);
- l1.setBounds(20,20,70,30);
- l2.setBounds(20,70,70,30);
- l3.setBounds(20,120,70,30);
- l4.setBounds(20,170,70,30);
- jtf1.setBounds(90,25,90,20);
- jtf2.setBounds(90,75,90,20);
- jtf3.setBounds(90,125,90,20);
- jrb1.setBounds(90,175,70,20);
- jrb2.setBounds(140,175,70,20);
- b.setBounds(200,75,70,30);
- add(l1);
- add(l2);
- add(l3);
- add(l4);
- add(jtf1);
- add(jtf2);
- add(jtf3);
- add(jrb1);
- add(jrb2);
- add(b);
- setLayout(null);
- b.addActionListener(e -> {
- String name=jtf1.getText();
- String Jprice=jtf2.getText();
- String Sprice=jtf3.getText();
- String bool;
- if(jrb1.isSelected())bool="是";
- else bool="否";
- String url="insert into store values (null, '"+name+"', '"+Jprice+"', '"+Sprice+"' ,'"+bool+"')";
- new Transaction(url);
- });
- }
- }
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- public class Transaction {
- public Transaction(String sql){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- try (
- Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mysql?characterEncoding=UTF-8",
- "root", "root");
- Statement s = c.createStatement()
- )
- {
- s.execute(sql);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- import javax.swing.*;
- import java.awt.*;
- import java.sql.*;
-
- public class Transaction_panel extends JPanel {
- public Transaction_panel(){//销售面板
- try {
- Class.forName("com.mysql.jdbc.Driver");
- try (
- Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mysql?characterEncoding=UTF-8",
- "root", "root")) {
- PreparedStatement ps = c.prepareStatement("select *from shell");
- ResultSet res= ps.executeQuery();//获取的结果
- res.last();
- int row=res.getRow();
- res.beforeFirst();
- String[][] str=new String[row][5];
- res.next();
- for (int i=0;i<row;i++){
- str[i][0]=res.getString("id");
- str[i][1]=res.getString("date");
- str[i][2]=res.getString("count");
- str[i][3]=res.getString("Pname");
- str[i][4]=res.getString("Oname");
- res.next();
- }
- String []name={"编号","交易时间","交易数量","进货/售货","商品名称"};
- JTable jt=new JTable(str,name);
- jt.setPreferredScrollableViewportSize(new Dimension(350,200));
- JScrollPane jsp=new JScrollPane(jt);
- add(jsp,BorderLayout.CENTER);
- }
- } catch (ClassNotFoundException | SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- new Frame();
- }
- }
3.运行Main类
如有问题请留言交流,谢谢
反馈
有朋友反应可能会出现一些错误(并非代码本身)
1.如果你的数据库驱动包版本较高,将
"com.mysql.jdbc.Driver"改为
"com.mysql.cj.jdbc.Driver"2.如果访问数据库出现ResuleSet type is TYPE_FORWARD_ONLY错误
将PreparedStatement ps = c.prepareStatement("select *from store");等
改为
PreparedStatement ps = c.prepareStatement("select *from store",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
如果还有什么错误,请大家联系我。
+Q2482071182,备注csdn
数据库代码:
shell表
- CREATE TABLE shell (
- id int(11) AUTO_INCREMENT,
- date varchar(30) ,
- count varchar(30) ,
- Pname varchar(30) ,
- Oname varchar(30) ,
- PRIMARY KEY (id)
- ) DEFAULT CHARSET=utf8;
store表
- CREATE TABLE store (
- id int(11) AUTO_INCREMENT,
- commodityname varchar(30) ,
- Unit_Price varchar(30) ,
- price varchar(30) ,
- Discount varchar(30) ,
- PRIMARY KEY (id)
- ) DEFAULT CHARSET=utf8;
operator表
- CREATE TABLE operator (
- id int(11) AUTO_INCREMENT,
- name varchar(30) ,
- pwd varchar(30) ,
- sum varchar(30) ,
- PRIMARY KEY (id)
- ) DEFAULT CHARSET=utf8;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。