Javaswing人口普查项目系统 java+mysql

数据库

在这里插入图片描述

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/population_db";
	//数据库登录账号
	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);
	}
	
}

查询方法查询数据库的表格

package ce;

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


public class cha {
	public List<EMP> queryHero(){
		ArrayList<EMP>arr=new ArrayList<EMP>();
		Connection conn=null;
		PreparedStatement pStat=null;
		ResultSet res=null;
		//创建List对象,使用泛型规范集合中添加元素的类型
		
		try {
			String sql="Select * from  `tb_ population`";
			conn=mysql.getConnection();
			pStat=conn.prepareStatement(sql);
		
			res=pStat.executeQuery(); //获取ResultSet结果集
            //判断ResultSet结果集中是否还有数据
			while(res.next()){		
				int id =res.getInt("id");
				String city = res.getString("city");
				String community = res.getString("community");
				String street = res.getString("street");
				String name = res.getString("name");
				String sex = res.getString("sex");
				String card_no  = res.getString("card_no");
				String register_date = res.getString("register_date");
				
				EMP emp = new EMP();
				emp.setId(id);
				emp.setCity(city);
				emp.setCommunity(community);
				emp.setStreet(street);
				emp.setName(name);
				emp.setSex(sex);
				emp.setCard_no(card_no);
				emp.setRegister_date(register_date);
				
				arr.add(emp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	finally {
			try {
				
				if(pStat!=null) {
					pStat.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return arr;
	}

}

EMP类

package ce;

public class EMP {
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCommunity() {
		return community;
	}
	public void setCommunity(String community) {
		this.community = community;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	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 getCard_no() {
		return card_no;
	}
	public void setCard_no(String card_no) {
		this.card_no = card_no;
	}
	public String getRegister_date() {
		return register_date;
	}
	public void setRegister_date(String register_date) {
		this.register_date = register_date;
	}
	private int id;//编号
	private String city;//所在城市
	private String community;//所在社区
	private String street;//所在街道 
	private String name;//姓名
	private String sex;//性别 
	private String card_no;//身份证号 
	private String register_date;//登记时间 
	
	
}

人口普查窗体代码

package ce;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JTextPane;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class luru {

	private JFrame frame;

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

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

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 321, 419);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel("\u4EBA\u53E3\u666E\u67E5\u4FE1\u606F\u5F55\u5165");
		lblNewLabel.setFont(new Font("黑体", Font.BOLD, 24));
		lblNewLabel.setForeground(Color.BLACK);
		lblNewLabel.setBounds(40, 6, 255, 71);
		frame.getContentPane().add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("\u57CE\u5E02");
		lblNewLabel_1.setBounds(26, 87, 54, 21);
		frame.getContentPane().add(lblNewLabel_1);
		
		JTextPane textPane = new JTextPane();
		textPane.setBounds(88, 87, 159, 21);
		frame.getContentPane().add(textPane);
		
		JLabel lblNewLabel_1_1 = new JLabel("\u793E\u533A");
		lblNewLabel_1_1.setBounds(26, 118, 54, 21);
		frame.getContentPane().add(lblNewLabel_1_1);
		
		JTextPane textPane_1 = new JTextPane();
		textPane_1.setBounds(88, 118, 159, 21);
		frame.getContentPane().add(textPane_1);
		
		JLabel lblNewLabel_1_1_1 = new JLabel("\u8857\u9053");
		lblNewLabel_1_1_1.setBounds(26, 149, 54, 21);
		frame.getContentPane().add(lblNewLabel_1_1_1);
		
		JTextPane textPane_1_1 = new JTextPane();
		textPane_1_1.setBounds(88, 149, 159, 21);
		frame.getContentPane().add(textPane_1_1);
		
		JLabel lblNewLabel_1_1_1_1 = new JLabel("\u59D3\u540D");
		lblNewLabel_1_1_1_1.setBounds(26, 180, 54, 21);
		frame.getContentPane().add(lblNewLabel_1_1_1_1);
		
		JTextPane textPane_1_1_1 = new JTextPane();
		textPane_1_1_1.setBounds(88, 180, 159, 21);
		frame.getContentPane().add(textPane_1_1_1);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u7537", "\u5973", "\u4EBA\u5996"}));
		comboBox.setBounds(88, 222, 159, 21);
		frame.getContentPane().add(comboBox);
		
		JLabel lblNewLabel_1_1_1_1_1 = new JLabel("\u6027\u522B");
		lblNewLabel_1_1_1_1_1.setBounds(26, 222, 43, 21);
		frame.getContentPane().add(lblNewLabel_1_1_1_1_1);
		
		JTextPane textPane_1_1_1_1 = new JTextPane();
		textPane_1_1_1_1.setBounds(88, 262, 159, 21);
		frame.getContentPane().add(textPane_1_1_1_1);
		
		JLabel lblNewLabel_1_1_1_1_1_1 = new JLabel("\u8EAB\u4EFD\u8BC1\u53F7");
		lblNewLabel_1_1_1_1_1_1.setBounds(26, 262, 54, 21);
		frame.getContentPane().add(lblNewLabel_1_1_1_1_1_1);
		
		JButton btnNewButton = new JButton("\u4FDD\u5B58");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				shan s1 = new shan();
	
				s1.let(textPane.getText(), textPane_1.getText(), textPane_1_1.getText(), textPane_1_1_1.getText(),
						comboBox.getModel().getSelectedItem()+"", 
						textPane_1_1_1_1.getText());
				JOptionPane.showMessageDialog(null, "添加成功");
			}
		});
		btnNewButton.setBounds(35, 316, 93, 23);
		frame.getContentPane().add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("\u5173\u95ED");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.dispose();//关闭当前窗口
			}
		});
		btnNewButton_1.setBounds(169, 316, 93, 23);
		frame.getContentPane().add(btnNewButton_1);
	}
}

删除方法和输入方法类

package ce;

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

public class shan {//保存到数据库
	public boolean let(String s1,String s2,String s3,String s4,String s5,String s6) {
		luru l1 = new luru();
		Connection conn =mysql.getConnection();
		
		String sql="insert into `tb_ population` VALUES(null,?,?,?,?,?,?, '2021-06-22')";
		PreparedStatement pStat=null;
		try {
		
			//获取Prapared对象
				pStat = conn.prepareStatement(sql);
			//将?用预处理的方式替换成你要的数据 1表示第一个占位符
			pStat.setString(1,s1);
			pStat.setString(2,s2);
			pStat.setString(3,s3);
			pStat.setString(4,s4);
			pStat.setString(5,s5);
			pStat.setString(6,s6);
			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;
			
			
	
	}
	public boolean shan(String s1) {//删
		luru l1 = new luru();
		Connection conn =mysql.getConnection();
		
		String sql="DELETE FROM `tb_ population` WHERE id=?";
		PreparedStatement pStat=null;
		try {
		
			//获取Prapared对象
				pStat = conn.prepareStatement(sql);
			//将?用预处理的方式替换成你要的数据 1表示第一个占位符
			pStat.setString(1,s1);

			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.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;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JMenu;
import java.awt.TextField;
import java.awt.Label;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;

public class text {

	private JFrame frame;
	private JScrollPane scrollPane;
	private JTable table;
	private JTextField textField;
	private JPanel panel;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;

	/**
	 * 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("\u4EBA\u53E3\u666E\u67E5\u7BA1\u7406\u7CFB\u7EDF");
		frame.setBounds(100, 100, 844, 512);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		gengxin();
		
		textField = new JTextField();
		textField.setBounds(146, 29, 298, 21);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("\u57CE\u5E02");
		lblNewLabel.setFont(new Font("仿宋", Font.BOLD, 13));
		lblNewLabel.setForeground(Color.BLACK);
		lblNewLabel.setBounds(99, 17, 37, 46);
		frame.getContentPane().add(lblNewLabel);
		
		panel = new JPanel();
		panel.setBounds(470, 17, 220, 38);
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		btnNewButton_1 = new JButton("\u67E5\u8BE2");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				cha ca = new cha();
				List<EMP>arr = ca.queryHero();
				
				 String[] name = new String[]  {
							"编号", "所在城市", "所在社区","所在街道","姓名","性别","身份证号","登记时间"
						};
				 String[][]s1  =  new String[arr.size()][8];
				 
					
					
						boolean flag = false;
						for(int j=0;j<s1.length;j++) {
							EMP e =arr.get(j);
						
							if(e.getCity().equals(textField.getText())) {
								s1[0][0]=e.getId()+"";
								s1[0][1]=e.getCity()+"";
								s1[0][2]=e.getCommunity()+"";
								s1[0][3]=e.getStreet()+"";
								s1[0][4]=e.getName()+"";
								s1[0][5]=e.getSex()+"";
								s1[0][6]=e.getCard_no()+"";
								s1[0][7]=e.getRegister_date()+"";
								flag=true;

							}
							
							
						}
						if(flag==false) {
							JOptionPane.showMessageDialog(null, "数据不存在查询失败");
						}
						
				scrollPane = new JScrollPane();
				scrollPane.setBounds(57, 76, 711, 344);
				frame.getContentPane().add(scrollPane);
				
				table = new JTable();
				table.setModel(new DefaultTableModel(
					s1,
					name
				));
				scrollPane.setViewportView(table);
			}
		});
		btnNewButton_1.setBounds(26, 10, 79, 23);
		panel.add(btnNewButton_1);
		
		btnNewButton = new JButton("\u5F55\u5165");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				luru lu = new luru();
				lu.main(null);
				
			}
		});
		btnNewButton.setBounds(126, 10, 84, 23);
		panel.add(btnNewButton);
		
		btnNewButton_2 = new JButton("\u5220\u9664");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				shan delite = new shan();

				 int row =table.getSelectedRowCount();

				if(row == -1) {
					JOptionPane.showMessageDialog(null, "改数据已删除,或者未选中");

					return;
				}
				String id = table.getValueAt(table.getSelectedRowCount(), 0).toString();
				delite.shan(id);
				JOptionPane.showMessageDialog(null, "删除成功");
				gengxin();
			}
		});

		btnNewButton_2.setBounds(700, 28, 93, 23);
		frame.getContentPane().add(btnNewButton_2);
	}
	
	
	public  void gengxin() {
		
		
		cha ca = new cha();
		List<EMP>arr = ca.queryHero();
		
		 String[] name = new String[]  {
					"编号", "所在城市", "所在社区","所在街道","姓名","性别","身份证号","登记时间"
				};
		 String[][]s1  =  new String[arr.size()][8];
		 
			
			
				
				for(int j=0;j<s1.length;j++) {
					EMP e =arr.get(j);
					s1[j][0]=e.getId()+"";
					s1[j][1]=e.getCity()+"";
					s1[j][2]=e.getCommunity()+"";
					s1[j][3]=e.getStreet()+"";
					s1[j][4]=e.getName()+"";
					s1[j][5]=e.getSex()+"";
					s1[j][6]=e.getCard_no()+"";
					s1[j][7]=e.getRegister_date()+"";

					
				}
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(57, 76, 711, 344);
		frame.getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			s1,
			name
		));
		scrollPane.setViewportView(table);
	
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值