Java swing 写会员卡管理系统

6 篇文章 0 订阅

在这里插入图片描述

数据库

在这里插入图片描述

这个是主类

package ce;

import java.awt.EventQueue;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
import javax.swing.border.BevelBorder;
import javax.swing.UIManager;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class text {

	private JFrame frame;
	private JScrollPane scrollPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					text window = new text();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public text() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
	
		frame = new JFrame();
		frame.setTitle("\u4F1A\u5458\u5361\u7BA1\u7406\u7CFB\u7EDF");
		frame.setBounds(100, 100, 844, 512);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		cha ca = new cha();
		List<EMP>arr = ca.query();
		
		 String[] name = new String[]  {
					"序号", "手机", "姓名","性别","办卡日期","卡余额"
				};
		 String[][]s1  =  new String[arr.size()][6];
		 for(int i=0;i<arr.size();i++) {
				EMP e =arr.get(i);
			
				int num=e.getMemberID();
				for(int j=i;j<s1.length;j++) {
					s1[j][0]=num+"";
					s1[j][1]=e.getPhone();
					s1[j][2]=e.getName();
					s1[j][3]=e.getSex();
					s1[j][4]=e.getOpenDate();
					s1[j][5]=e.getBalance()+"";
					
					
					
				}
				
			}
		
		
		
		JButton btnNewButton = new JButton("\u67E5\u8BE2");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cha ca = new cha();
				List<EMP>arr = ca.query();
				
				 String[] name = new String[]  {
							"序号", "手机", "姓名","性别","办卡日期","卡余额"
						};
				 String[][]s1  =  new String[arr.size()][6];
				 for(int i=0;i<arr.size();i++) {
					 EMP ea =arr.get(i);
					
						int num=ea.getMemberID();
						for(int j=i;j<s1.length;j++) {//循环放进表格
							s1[j][0]=num+"";
							s1[j][1]=ea.getPhone();
							s1[j][2]=ea.getName();
							s1[j][3]=ea.getSex();
							s1[j][4]=ea.getOpenDate();
							s1[j][5]=ea.getBalance()+"";
							
							
							
						}
						
					}
					table.setModel(new DefaultTableModel(
							s1,
							name
						));
				
			}
		});
		
		btnNewButton.setBounds(456, 28, 93, 23);
		frame.getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u5220\u9664");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				shan san = new shan();
				int s1 =JOptionPane.showConfirmDialog(null, "确定要删除吗?");
				if(s1==0) {
					
				}
				
			}
		});
		btnNewButton_1.setBounds(635, 28, 93, 23);
		frame.getContentPane().add(btnNewButton_1);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(64, 75, 711, 344);
		frame.getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			s1,
			name
		));
		scrollPane.setViewportView(table);
	}
}

删除方法 暂时还没实现删除功能,因为不知道怎么删除表格行

package ce;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class shan {
	public boolean delet(int Id) {//删
		Connection conn =mysql.getConnection();
		
		String sql="delete from text where memberid = ?";
		PreparedStatement pStat=null;
		try {
		
			//获取Prapared对象
				pStat = conn.prepareStatement(sql);
			//将?用预处理的方式替换成你要的数据 1表示第一个占位符
			pStat.setInt(1, Id);
			return pStat.executeUpdate()>0?true:false;
		} catch (SQLException e) {
			
			e.printStackTrace();	
		}
		finally {
			try {
				
				if(pStat!=null) {
					pStat.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}

		return false;
			
			
	
	}

}

数据库链接驱动

package ce;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Scanner;

import dbconnectiondemo.DbConnection;
import dbconnectiondemo.Dbsixsixsix;


public class mysql {
	private static final String DRIVERNAME = "com.mysql.jdbc.Driver";
	
	private static final String URL="jdbc:mysql://localhost:3306/memberdb";
	//数据库登录账号
	private static final String USER="root";
	//数据库登录密码
	private static final String PASSWORD="12345678";
	static {
		try {
			Class.forName(DRIVERNAME);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static Connection getConnection() {
		Connection conn=null;
		try {
			return DriverManager.getConnection(URL,USER,PASSWORD);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
		
	}
	public static void main(String[] args) {
		Connection conn= getConnection();
		System.out.println(conn);
	}
	
}

EMP 方法类

package ce;

public class EMP {
	public int getMemberID() {
		return memberID;
	}
	public void setMemberID(int memberID) {
		this.memberID = memberID;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getOpenDate() {
		return openDate;
	}
	public void setOpenDate(String openDate) {
		this.openDate = openDate;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	private int memberID;//会员编号
	private String phone;//会员手机
	private String name;//会员姓名
	private String sex;//性别
	private String openDate;//办卡日期
	private double balance;//卡内余额
	
}

查询数据库全部内容并且放进数组 返回放进表格

package ce;

import java.lang.annotation.Retention;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;




import text.Main;


public class cha {
	public List<EMP> query(){
		ArrayList<EMP>arr=new ArrayList<EMP>();
		Connection conn = mysql.getConnection();
		String sql ="select * from members";
		PreparedStatement  pStat=null;
		ResultSet res=null;
		try {
			pStat = conn.prepareStatement(sql);
			res= pStat.executeQuery();
			while(res.next()) {
				int memberID =res.getInt("memberID");
				String phone = res.getString("phone");
				String name = res.getString("name");
				String sex = res.getString("sex");
				String openDate = res.getString("openDate");
				double balance = res.getDouble("balance");
				EMP emp = new EMP();
				emp.setMemberID(memberID);
				emp.setPhone(phone);
				emp.setBalance(balance);
				emp.setName(name);
				emp.setSex(sex);
				emp.setOpenDate(openDate);
				arr.add(emp);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			try {
				if(res!=null) {
					res.close();
				}
				if(pStat!=null) {
					pStat.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}

		
		return arr;
	}
	public static void main(String[] args) {
		cha ca = new cha();
		List<EMP>arr = ca.query();
		for(int i=0;i<arr.size();i++) {
			EMP e =arr.get(0);
			System.out.println(e.getBalance()+" "+e.getMemberID()+" "+e.getName());
		}
	}
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值