复试华工——java swing界面

复试华工——java swing界面

1 文件结构

1.1

  1. Dao包,操作数据库的全在里面
  2. model,想向MVC这个模式靠拢,但是复试时间短没必要
  3. Util,工具
  4. View,里面都是窗口,由mainFrame主窗口点击按钮,进去其他窗口

2 HospitalDao.java代码

public class HospitalDao extends BaseDao{

	PreparedStatement preparedStatement = null;
	ResultSet resultSet = null;
	
	public ResultSet totalNumofDept() {
		String SQL = "select [Department].DeptName, sum([Group].[Number]) as [Numbers] " + 
				"from [Department], [Group] " + 
				"where [Department].DeptNo = [Group].[DeptNo] " + 
				"group by [Department].[DeptName]";
		try {
			preparedStatement = con.prepareStatement(SQL);
			resultSet = preparedStatement.executeQuery();
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return resultSet;
	}
	
	public ResultSet numOfMonth() {
		String sql = "select [Month], sum([Number]) as [Numbers]\r\n" + 
				"from [Group]\r\n" + 
				"group by [Month]";
		try {
			preparedStatement = con.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return resultSet;
	}
	
	public ResultSet research(String deptName, String groupName, String patientName) {
		String sql = "select [Department].[DeptName], [Group].[GroupName], [Patient].[PName]\r\n" + 
				"from [Department], [Group], [Patient]\r\n" + 
				"where [Department].[DeptNo] = [Group].[DeptNo] and [Group].[GroupNo] = [Patient].[GroupNo]";		
		if (!StringUtil.isEmpty(deptName)) {
			sql += " and [Department].[DeptName] = '" + deptName + "' ";
		}
		if (!StringUtil.isEmpty(groupName)) {
			sql += " and [Group].[GroupName] = '" + groupName + "' ";
		}
		if (!StringUtil.isEmpty(patientName)) {
			sql += " and [Patient].[PName] like '%" + patientName + "%'";
		}
		sql += "\r\norder by [Department].[DeptName] desc, [Group].[GroupNo] ,[Patient].[PName] desc";
		try {
			preparedStatement = con.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return resultSet;
	}
	
	public ResultSet searchPatient() {
		String sql = "select PID, PName, Date, GroupNo from Patient";
		try {
			preparedStatement = con.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
		} catch (SQLException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return resultSet;
	}
}

3 MainFrame

写一个frame的构造方法,先就要写上setTitle(), setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE),
setBounds(x, y, width, height)
后面的时候不要忘记写setContentPane(),把JPanel面板放进去

public class MainFrame extends JFrame {
	
	private JPanel panel = null;
	private JLabel label = null;
	private JButton searchButton = null;
	private JButton sjwhPatientButton = null;
	private JButton tjButton = null;
	private JButton sjwhDeptButton = null;
	
	public MainFrame() {
		// 主窗格
		setTitle("2017JiYuan_Hospital");
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setBounds(600, 500, 400, 400);
		
		panel = new JPanel(null); 
		label = new JLabel("选择功能");
		label.setSize(150, 50);
		label.setLocation(150, 0);
		searchButton = new JButton("查询功能");
		searchButton.setSize(200, 50);
		searchButton.setLocation(80, 60);
		sjwhPatientButton = new JButton("数据维护-病人");
		sjwhPatientButton.setSize(200, 50);
		sjwhPatientButton.setLocation(80, 120);
		tjButton = new JButton("统计功能");
		tjButton.setSize(200, 50);
		tjButton.setLocation(80, 180);
		sjwhDeptButton = new JButton("数据维护-大科室");
		sjwhDeptButton.setSize(200, 50);
		sjwhDeptButton.setLocation(80, 240);
		
		panel.add(label);
		panel.add(searchButton);
		panel.add(sjwhPatientButton);
		panel.add(tjButton);
		panel.add(sjwhDeptButton);
		
		searchButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ResearchFrame researchFrame = new ResearchFrame();
				researchFrame.setVisible(true);//因为没在这个Frame窗体中写setVisible所以要在这里写上显示
			}
		});
		
		sjwhPatientButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				SjwhPatient sjwhPatient = new SjwhPatient();
				sjwhPatient.setVisible(true);
			}
		});
		tjButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				TotalFrame totalFrame = new TotalFrame();
				totalFrame.setVisible(true);
			}
		});
		setContentPane(panel);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					MainFrame mainFrame = new MainFrame();
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		});
	}
}

3.1

4 TotalFrame

注意在把JTable加进JScrollPane时,一定要先获得Viewport。jScrollPane.getViewport().add(jTable);

public class TotalFrame extends JFrame implements ActionListener {

    private JButton jButton_deptNum = null;
    private JButton jButton_MonthNum = null;
    private JPanel panel = null;
    private JPanel panel_button = null;
    private JScrollPane jScrollPane = null;
    private JTable jTable = null;
    HospitalDao dao = new HospitalDao();
    
    public TotalFrame() {
        // 要设置DISPOSE_ON_CLOSE参数,关闭这个窗口的时候就不会关闭父窗口
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("统计功能");
        setBounds(200, 200, 500, 500);
        
        panel = new JPanel(new BorderLayout());    // 要用边界布局,如果用绝对布局的话会把table给挤掉
        panel.setBorder(new EmptyBorder(5, 5, 5, 5));	// 为了更好看一点
        //把按钮都放在一个面板上,在让面板在主面板的北边,这样就好看了点           
        panel_button = new JPanel();
        jButton_deptNum = new JButton("大科室人数");
        jButton_MonthNum = new JButton("每月人数");
        panel_button.add(jButton_deptNum);
        panel_button.add(jButton_MonthNum);
        panel.add(panel_button, BorderLayout.NORTH);
        
        jTable = new JTable();
        jScrollPane = new JScrollPane();
        // 一定不要忘记吧JScrollPane滚动窗格给放进面板里面去,我总忘记  
        panel.add(jScrollPane);
        
        jButton_deptNum.addActionListener(this);
        jButton_MonthNum.addActionListener(this);
        
        setContentPane(panel);
    }

    @Override
    //通过这个点击事件,可以实现在一个滚动窗口中点击不同按钮显示不同的表格,yoyoyo
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if (arg0.getSource() == jButton_deptNum) {// 如果点击的是大科室人数按钮
            String[] col1 = {"大科室名称", "病人数"};//列名
            // 大科室人数的表模型,第一个参数是String数组变成列名,第二个参数是行数,这里弄成0
            DefaultTableModel mm1 = new DefaultTableModel(col1, 0);
            System.out.println("点击了dept");
            try {
                ResultSet resultSet = dao.totalNumofDept();
                while (resultSet.next()) {
                    String DeptName = resultSet.getString("DeptName");
                    String number = String.valueOf(resultSet.getInt("Numbers"));
                    String[] row = {DeptName, number};    // 一行数据放入String数组row中
                    mm1.addRow(row);    // 再将这一行的数据加入到表模板中
                }
            } catch (SQLException e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
            jTable.setModel(mm1);//设置表格的模板为mm1
            jScrollPane.getViewport().add(jTable);//先获取到Viewport然后把table加进滚动窗口中
        }
        else if (arg0.getSource() == jButton_MonthNum) {
            String[] col2 = {"月份", "病人数"};
            // 每月人数的表模型
            DefaultTableModel mm2 = new DefaultTableModel(col2, 0);
            System.out.println("点击了Month");
            try {
                ResultSet resultSet = dao.numOfMonth();
                while (resultSet.next()) {
                    String DeptName = resultSet.getString("Month");
                    String number = String.valueOf(resultSet.getInt("Numbers"));
                    String[] row = {DeptName, number};    // 一行数据放入row中
                    mm2.addRow(row);    // 再加入到表模板中
                }
            } catch (SQLException e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }
            jTable.setModel(mm2);
            jScrollPane.getViewport().add(jTable);
        }
    }
}

3.2

5 ResearshFrame

该部分的sql语句的写法

String sql = "select [Department].[DeptName], [Group].[GroupName], [Patient].[PName]\r\n" + 
				"from [Department], [Group], [Patient]\r\n" + 
				"where [Department].[DeptNo] = [Group].[DeptNo] and [Group].[GroupNo] = [Patient].[GroupNo]";		
		if (!StringUtil.isEmpty(deptName)) {
			sql += " and [Department].[DeptName] = '" + deptName + "' ";
		}
		if (!StringUtil.isEmpty(groupName)) {
			sql += " and [Group].[GroupName] = '" + groupName + "' ";
		}
		if (!StringUtil.isEmpty(patientName)) {
			sql += " and [Patient].[PName] like '%" + patientName + "%'";
		}
		sql += "\r\norder by [Department].[DeptName] desc, [Group].[GroupNo] ,[Patient].[PName] desc";
public class ResearchFrame extends JFrame implements ActionListener {
	
	private JPanel panel = null;
	private JPanel panel_btn = null;
	private JButton button = null;
	private JLabel label1 = null;
	private JLabel label2 = null;
	private JLabel label3 = null;
	private JTextField textField1 = null;
	private JTextField textField2 = null;
	private JTextField textField3 = null;
	private JScrollPane jScrollPane = null;
	private JTable table = null;
	
	HospitalDao dao = new HospitalDao();
	
	public ResearchFrame() {
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setBounds(200, 200, 700, 500);
		setTitle("查询功能");
		
		panel = new JPanel(new BorderLayout());
		panel_btn = new JPanel();
		
		button = new JButton("查询");
		label1 = new JLabel("大科室名称:");
		label2 = new JLabel("小科室名称:");
		label3 = new JLabel("病人姓名:");
		textField1 = new JTextField(8);
		textField2 = new JTextField(8);
		textField3 = new JTextField(8);
		
		panel_btn.add(label1);
		panel_btn.add(textField1);
		panel_btn.add(label2);
		panel_btn.add(textField2);
		panel_btn.add(label3);
		panel_btn.add(textField3);
		panel_btn.add(button);
		panel_btn.setBackground(new Color(111111));
		
		panel.add(panel_btn, BorderLayout.NORTH);
		
		table = new JTable();
		jScrollPane = new JScrollPane();
		panel.add(jScrollPane);
		
		button.addActionListener(this);
		
		setContentPane(panel);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String deptName = textField1.getText();
		String groupName = textField2.getText();
		String patientName = textField3.getText();
		String[] col = {"大科室名称", "小科室名称", "病人姓名"};
		DefaultTableModel mm = new DefaultTableModel(col, 0);
		System.out.println("点击了查询");
		try {
			ResultSet resultSet = dao.research(deptName, groupName, patientName);
			if (!resultSet.next()) {//这里判断了是否有第一条数据,这个游标已经走到了第一条数据上了,所以下面就得用do-while循环
				JOptionPane.showMessageDialog(this, "没有查询到结果");
			}
			else {
				do {//使用do-while才能够把第一条数据也给读了,要不然总会少一条,哎~弟弟了
					deptName = resultSet.getString("DeptName");
					groupName = resultSet.getString("GroupName");
					patientName = resultSet.getString("PName");
					String[] row = {deptName, groupName, patientName};
					mm.addRow(row);
				} while (resultSet.next());
				table.setModel(mm);
				jScrollPane.getViewport().add(table);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

5.1

6 关于数据库Date类型的相关操作

// 判定一个str是否为日期格式
	private boolean isValidDate(String str) {
		boolean flag = true;
        //定义一个date的格式,大小写是有区别的,MM表示月份,而mm表示的是秒
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			// 设置lenient为false,否则SimpleDateFormat会比较宽松地验证日期,比如2007-02-29会被接受,并转换成2007-03-01
			sdf.setLenient(false);  
			sdf.parse(str);//这个语句就把str转成了该有的模式,且是java.util.date对象
		} catch (ParseException e) {
			// TODO: handle exception
			flag = false;
		}
		return flag;
	}
// 这是个程序段
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
		Date utilDate = sdf.parse(DateString);// 通过SimpleDateFormat的parse(str)的方法把str转成了java.util.Date对象
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());//而数据库中所存储Date对于java是java.sql.Date对象,所以要把utilDate转换一下
		if (dao.addPatient(PID, PName, sqlDate, GroupNo)) {
			JOptionPane.showMessageDialog(this, "添加成功");
		}
		else {
			JOptionPane.showMessageDialog(this, "添加失败");
		}
	} catch (ParseException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

7 加强版增删改界面

可以根据选择的JTable的表项来进行相关操作,如删除的话就是选中一项点击删除就可;如果是修改操作,修改是不能修改经销商号的,所以我在DefaultTableModel中设置了第一列是不能被选中的,其他的列是能在表格上修改的,修改完后按下enter或者点击其他列的位置,再点击修改的话就可以完成修改操作,不足的地方是:如果还在修改状态就点击修改的话,修改不了。
7.1

public class maintainFrame extends JFrame implements ActionListener {

	private JPanel contentPane;
	private JPanel panel;
	private JButton button1;
	private JButton button2;
	private JButton button3;
	private JButton button4;
	private JScrollPane jsp;
	private JTable table;
	private String selectedDealer = null;
	String[] cols = {"经销商号", "经销商名", "城市"};
	JiYuanDao dao = new JiYuanDao();
	/**
	 * Create the frame.
	 */
	public maintainFrame() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(500, 500, 470, 332);
		setTitle("维护-经销商");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		panel = new JPanel();
		button1 = new JButton("增加");
		button2 = new JButton("删除");
		button3 = new JButton("修改");
		button4 = new JButton("刷新");
		table = new JTable();
		table.setFont(new Font("宋体", Font.PLAIN, 15));
		jsp = new JScrollPane();
		panel.add(button1);
		panel.add(button2);
		panel.add(button3);
		panel.add(button4);
		contentPane.add(panel, BorderLayout.NORTH);
		contentPane.add(jsp, BorderLayout.CENTER);
		button1.addActionListener(this);
		button2.addActionListener(this);
		button3.addActionListener(this);
		button4.addActionListener(this);
		setTable();
		setContentPane(contentPane);
	}

	private void setTable() {
		DefaultTableModel mm = new DefaultTableModel(cols, 0) {
        //重写了isCellEditable方法,这里设置了第一列不会被选中
			@Override
			public boolean isCellEditable(int row, int column) {
				// TODO Auto-generated method stub
				if (column == 0) {//第一列不会被选中,不可编辑
					return false;
				}
				else {
					return true;
				}
			}
		};
		ResultSet rs = dao.allDealer();
		try {
			while (rs.next()) {
				String DNo = rs.getString(1);
				String DName = rs.getString(2);
				String City = rs.getString(3);
				String[] row = {DNo, DName, City};
				mm.addRow(row);
			}
			table.setModel(mm);
			jsp.setViewportView(table);
            rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getSource() == button1) {
			AddDealerFrame addDealerFrame = new AddDealerFrame();
			addDealerFrame.setVisible(true);
		}
		if (arg0.getSource() == button2) {
			int index = table.getSelectedRow();//返回table被选中的行号
			if (index == -1) {
				JOptionPane.showMessageDialog(this, "请选择要删除的一项");
				return;
			}
			String DNo = table.getValueAt(index, 0).toString();
			int result = JOptionPane.showConfirmDialog(this, "确定要删除这一条记录吗?");//这个是确认对话框,返回有三个值
            //是:   JOptionPane.YES_OPTION
            //否:   JOptionPane.NO_OPTION
            //取消: JOptionPane.CANCEL_OPTION
            //关闭: JOptionPane.CLOSED_OPTION
			if (result == JOptionPane.YES_OPTION) {
				if (dao.deleteDealer(DNo)) {
					JOptionPane.showMessageDialog(this, "删除成功");
					setTable();
					return;
				}
				else {
					JOptionPane.showMessageDialog(this, "删除失败");
					return;
				}
			}
		}
		if (arg0.getSource() == button3) {
			int index = table.getSelectedRow();
			if (index == -1) {
				JOptionPane.showMessageDialog(this, "请选择要修改的一条记录");
				return;
			}
			String DNo = table.getValueAt(index, 0).toString();
			String DName = table.getValueAt(index, 1).toString();
			String City = table.getValueAt(index, 2).toString();
			int result = JOptionPane.showConfirmDialog(this, "要修改这一项吗?");
			if (result == JOptionPane.YES_OPTION) {
				if (dao.updateDealer(DNo, DName, City)) {
					JOptionPane.showMessageDialog(this, "修改成功");
					setTable();
					return;
				}
				else {
					JOptionPane.showMessageDialog(this, "修改失败");
					return;
				}
			}
		}
		if (arg0.getSource() == button4) {
			setTable();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值