数据库课程设计-宿舍管理系统

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/deramer1/article/details/73380407
最近写完了数据库的课程设计,想把整个源码的编辑过程发出来。程序很简单,需要有很多完善的地方,在这里,我想和大家分享写这个程序的心路历程。

首先,在开始写程序之前。我们需要先写一些工具类,来辅助完成整个程序的构建,在这里我把连接jdbc的代码放在了一个包下面。

如下图:

在这里我们先来写最基本的类,jdbcDrive,这是负责和数据库进行连接,并且执行语句的类

public class jdbcDrive {
	public static Connection connection;
	public static Statement statement;
	public static ResultSet  resultset;
	public static void jdbcConnection(){
		try {
		    connection = DriverManager.getConnection(jdbc.dbURL, jdbc.userName,jdbc.userPwd);
			System.out.println("连接数据库成功!");
			} catch (Exception e) {
			System.out.print("SQL Server连接失败!");
			}
	}
	
	public static void jdbcConnectionClose(){
		try {
			connection.close();
			System.out.println("数据库连接成功关闭");
		} catch (SQLException e) {
			System.out.println("数据库连接关闭失败");
		}
	}
	
	public static void jdbcExecuteUpdate(String s) throws SQLException{
		jdbcConnection();
		statement=connection.createStatement();
		statement.executeUpdate(s);
	}
	
	public static void jdbcExecuteQuery(String s) throws SQLException{
		jdbcConnection();
		statement=connection.createStatement();
		resultset=statement.executeQuery(s);
	}
	public static void main(String [] args) {
	}
}
在这里我选择的是SQL SERVER 2014,它的jdbc版本已经不需要加载数据库了,可以直接进行连接,在这里分为四个方法。其中两个是负责,数据库的连接和释放。jdbcExecuteUpdate负责传递SQL的增加,修改,删除,不需要有结果集的语句。jdbcExecuteQuery负责进行jdbc的查询,将查询的结果放在resultset里面。Connection类是负责进行数据库的连接,Statement 负责传递查询语句。resultset是结果集,负责保存储存的信息。

在进行数据的连接的时候,需要向数据库传递参数。我把参数放在了一个接口里面。

public interface jdbc {
    String dbURL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Dormitory";
	String userName = "sa";
	String userPwd = "123456";
}
写完基本的jdbc类之后,我们在进行整个系统的界面搭建。

设计主界面的时候,用到以下几个类,第一个是用于放置菜单选项的JMenuBar,它是放置JMenu的容器,而JMenu是房子JMenuItem的容器。具体的构建过程就是,在JFrame里添加JMenu,用来表示要放置的菜单选项,在JMenu里面放置JMeunItem用来表示菜单的子选项。其中,用了JLabel来存放图片。各个JMenuItem的监听事件放在了一个单独的包里面。具体的搭建界面如下:



public class Login extends JFrame {
	private JButton loginButton,exitButton;
	private JLabel userNameLabel,passwordLabel;
	private JTextField userNameText;
	private JPasswordField passwordText;
	private HandleAction handleAction;
	private static JPanel loginPanel;
	private ManagementUi MainUi;
	private Toolkit toolKit=null;
	private Dimension screenSize=null;
	public static personManage nowPeople;
	public  Login(){
		Init();
	}
	
	public void Init(){
		//获取当前窗口,并且获取屏幕的尺寸
		toolKit=Toolkit.getDefaultToolkit();
		screenSize=toolKit.getScreenSize();
		//加载封装了图片的Panel子类,并添加到当前JFrame容器里面
	    loginPanel=new LoginPanel();
		loginPanel.setLayout(null);
		add(loginPanel);
		//将整个图框居中
     	setBounds(screenSize.width/2-loginPanel.getWidth()/2
     			,screenSize.height/2-loginPanel.getHeight()/2
     			,loginPanel.getWidth(),loginPanel.getHeight());
		handleAction=new  HandleAction();
		this.setIconImage(new ImageIcon("resource/picture.png").getImage());
		//向loginPanel容器加入两个Jlabel,两个按钮,一个文本框,一个密码框
		userNameLabel=new JLabel("用户名");
		userNameLabel.setBounds(100, 120, 200, 18);
		loginPanel.add(userNameLabel);
		
		userNameText=new JTextField();
		userNameText.setBounds(150, 120, 150, 18);
		loginPanel.add(userNameText);
		
		passwordLabel=new JLabel("密码");
		passwordLabel.setBounds(100, 180, 200, 18);
		loginPanel.add(passwordLabel);
		
		passwordText=new JPasswordField();
		passwordText.setBounds(150, 180, 150, 18);
		loginPanel.add(passwordText);
		
		loginButton=new JButton("登录");
		loginButton.setBounds(150, 230, 60, 18);
		loginPanel.add(loginButton);
		
		exitButton=new JButton("退出");
		exitButton.setBounds(230, 230, 60, 18);
		loginPanel.add(exitButton);
		
		exitButton.addActionListener(handleAction);
		loginButton.addActionListener(handleAction);
		setTitle("宁悦宿舍管理系统");
		this.setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(this.EXIT_ON_CLOSE);
	}
	private class HandleAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==loginButton){
				String pass=new String(passwordText.getPassword());
				if(jdbcLogin.Judge(userNameText.getText(),pass)){
					nowPeople=new personManage(userNameText.getText(),null,null,pass);
					MainUi=new ManagementUi();
					Login.this.setVisible(false);
				}else{
					JOptionPane.showMessageDialog(Login.this,"不是系统的合法用户或密码错误" );
				}
				
			}else if(e.getSource()==exitButton) {
				Login.this.dispose();
			}
		}
	}
	
	public static void main(String [] args){
		Login in=new Login();
	}
}

class LoginPanel extends JPanel {
	protected ImageIcon icon = new ImageIcon("resource/login.jpg");
	public int width = icon.getIconWidth(), height = icon.getIconHeight();
	public LoginPanel() {
		super();
		setSize(width, height);
	}
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Image img = icon.getImage();
		 g.drawImage(img, 0, 0, icon.getIconWidth(),
			      icon.getIconHeight(), icon.getImageObserver());
	}
}
搭建完界面之后,我们按这个顺序来进行代码编码,首先,编写管理员的登录界面,然后写学生的管理界面,在写宿舍的管理界面,最后写调宿的管理界面。

下面先来看登录界面的的截图:




下面是整个登录界面的源代码:

public class Login extends JFrame {
	private JButton loginButton,exitButton;
	private JLabel userNameLabel,passwordLabel;
	private JTextField userNameText;
	private JPasswordField passwordText;
	private HandleAction handleAction;
	private static JPanel loginPanel;
	private ManagementUi MainUi;
	private Toolkit toolKit=null;
	private Dimension screenSize=null;
	public static personManage nowPeople;
	public  Login(){
		Init();
	}
	
	public void Init(){
		//获取当前窗口,并且获取屏幕的尺寸
		toolKit=Toolkit.getDefaultToolkit();
		screenSize=toolKit.getScreenSize();
		//加载封装了图片的Panel子类,并添加到当前JFrame容器里面
	    loginPanel=new LoginPanel();
		loginPanel.setLayout(null);
		add(loginPanel);
		//将整个图框居中
     	setBounds(screenSize.width/2-loginPanel.getWidth()/2
     			,screenSize.height/2-loginPanel.getHeight()/2
     			,loginPanel.getWidth(),loginPanel.getHeight());
		handleAction=new  HandleAction();
		this.setIconImage(new ImageIcon("resource/picture.png").getImage());
		//向loginPanel容器加入两个Jlabel,两个按钮,一个文本框,一个密码框
		userNameLabel=new JLabel("用户名");
		userNameLabel.setBounds(100, 120, 200, 18);
		loginPanel.add(userNameLabel);
		
		userNameText=new JTextField();
		userNameText.setBounds(150, 120, 150, 18);
		loginPanel.add(userNameText);
		
		passwordLabel=new JLabel("密码");
		passwordLabel.setBounds(100, 180, 200, 18);
		loginPanel.add(passwordLabel);
		
		passwordText=new JPasswordField();
		passwordText.setBounds(150, 180, 150, 18);
		loginPanel.add(passwordText);
		
		loginButton=new JButton("登录");
		loginButton.setBounds(150, 230, 60, 18);
		loginPanel.add(loginButton);
		
		exitButton=new JButton("退出");
		exitButton.setBounds(230, 230, 60, 18);
		loginPanel.add(exitButton);
		
		exitButton.addActionListener(handleAction);
		loginButton.addActionListener(handleAction);
		setTitle("宁悦宿舍管理系统");
		this.setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(this.EXIT_ON_CLOSE);
	}
	private class HandleAction implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==loginButton){
				String pass=new String(passwordText.getPassword());
				if(jdbcLogin.Judge(userNameText.getText(),pass)){
					nowPeople=new personManage(userNameText.getText(),null,null,pass);
					MainUi=new ManagementUi();
					Login.this.setVisible(false);
				}else{
					JOptionPane.showMessageDialog(Login.this,"不是系统的合法用户或密码错误" );
				}
				
			}else if(e.getSource()==exitButton) {
				Login.this.dispose();
			}
		}
	}
	
	public static void main(String [] args){
		Login in=new Login();
	}
}

class LoginPanel extends JPanel {
	protected ImageIcon icon = new ImageIcon("resource/login.jpg");
	public int width = icon.getIconWidth(), height = icon.getIconHeight();
	public LoginPanel() {
		super();
		setSize(width, height);
	}
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Image img = icon.getImage();
		 g.drawImage(img, 0, 0, icon.getIconWidth(),
			      icon.getIconHeight(), icon.getImageObserver());
	}
}

在的登录的时候,需要连接数据库,在登录模块,我专门为它建立了一个数据库的连接类,来进行数据库的建立。


public class jdbcLogin {
	private static HashMap<String,String> people=new HashMap<String,String>();
	public static boolean Judge(String user,String pass){
		String SQL = "SELECT * FROM personManage";
		boolean flag=false;
		try {
			jdbcDrive.jdbcConnection();
			jdbcDrive.statement=jdbcDrive.connection.createStatement();
			jdbcDrive.resultset=jdbcDrive.statement.executeQuery(SQL);
		    while (jdbcDrive.resultset.next()) {
		    	people.put(jdbcDrive.resultset.getString(1).trim(),jdbcDrive.resultset.getString(4).trim());
		    }
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			jdbcDrive.jdbcConnectionClose();
		}
		if(people.containsKey(user)){
			if(people.get(user).equals(pass)){
				flag=true;
			}
		}else{
			flag=false;
		}
		return flag;
	}
}

如果数据库存在对应的账号,则返回true,让用户登录,否则返回false。

数据库建立好了之后,需要进行管理人员的创立,修改,删除。



管理员的界面用表的形式,来进行相应的管理,管理的过程中,点击表中的数据,会相应的显示在下面的文本框里,在这里进行增删改查。每次完成曾删改查,都需要点击查询进行刷新,下面是整体的代码。


public class personManageUi {
	private JFrame mainJframe;
	private Container con;
	private JScrollPane JSpane;
	private MyJTable DataTable;
	private String [] names={"管理员账号","管理员姓名","管理员性别","管理人员密码"};
	private JButton btn_Query,btn_Modify,btn_Add,btn_Delete;
	private JPanel jpn;
	private JPanel messageJpn;
	private JTextField nameText,passText,sexText,idText;
	private personManage personChange;//保存要修改的数据的信息
	public personManageUi(){
		mainJframe=new JFrame("管理员管理");
		mainJframe.setSize(800, 600);
		mainJframe.setVisible(true);
		mainJframe.setResizable(false);
		mainJframe.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
		mainJframe.setLocation(screenSize.width/2-400, screenSize.height/2-300);
		con=mainJframe.getContentPane();
		con.setLayout(new GridLayout(3,1));
		DataTable=new MyJTable();
		DefaultTableModel df=new DefaultTableModel(names,0);
		df.setRowCount(20);
		DataTable.setModel(df);
		DataTable.addMouseListener(new table_selectActionListener());
		JSpane=new JScrollPane(DataTable);
		con.add(JSpane);
		messageJpn=new JPanel();
		messageJpn.setLayout(new GridBagLayout());
		setComponent(new JLabel("管理人员账号:"),0,0,1,1,false);
		idText=new JTextField();
		setComponent(idText,1,0,1,150,false);
		setComponent(new JLabel("管理人员姓名:"),2,0,1,1,false);
		nameText=new JTextField();
		setComponent(nameText,3,0,1,150,true);
		setComponent(new JLabel("管理人员性别:"),0,1,1,1,false);
		sexText=new JTextField();
		setComponent(sexText,1,1,1,150,false);
		setComponent(new JLabel("管理人员密码:"),2,1,1,1,false);
		passText=new JTextField();
		setComponent(passText,3,1,1,150,true);
		con.add(messageJpn);
		jpn=new JPanel();
		jpn.setLayout(new FlowLayout());
		con.add(jpn);
		btn_Add=new JButton("添加");
		btn_Add.addActionListener(new btn_AddActionListener());
		jpn.add(btn_Add);
		jpn.add(new JLabel("     "));
		btn_Query=new JButton("查询");
		btn_Query.addActionListener(new btn_QueryActionListener());
		jpn.add(btn_Query);
		jpn.add(new JLabel("     "));
		btn_Modify=new JButton("修改");
		btn_Modify.addActionListener(new btn_ModifyActionListener());
		jpn.add(btn_Modify);
		jpn.add(new JLabel("     "));
		btn_Delete=new JButton("删除");
		btn_Delete.addActionListener(new btn_DeleteActionListener());
		jpn.add(btn_Delete);
	}
	/*
	 * 完成查询按钮的监听事件
	 */
	private class btn_QueryActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			jdbcPersonManage jp=new jdbcPersonManage(null);
			personManage people;
			try {
				jp.personQuery();
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(mainJframe,"无法连接到数据库" );
			}finally{
				jdbcDrive.jdbcConnectionClose();
			}
			ArrayList<personManage> personlist=jp.getPersonlist();
			for(int i=0;i<personlist.size();i++){
				people=personlist.get(i);
				String [] data={people.getPersonManageId(),people.getPersonManageName(),
						people.getPersonManageSex(),"**********"};
				for(int j=0;j<4;j++){
					DataTable.setValueAt(data[j], i, j);
				}
			}
			for(int i=0;i<4;i++){
				DataTable.setValueAt("", personlist.size(), i);
			}
		}
	}	
	/*
	 * 完成对修改按钮的监听事件
	 */
	private class btn_ModifyActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			jdbcPersonManage jpm=new jdbcPersonManage(null);
			try{
			if(personChange.getPersonManageName()!=nameText.getText()){
					jpm.personModify(personChange.getPersonManageId(),
							"personManageName",nameText.getText());
			}
			if(personChange.getPersonManageSex()!=sexText.getText()){
				    jpm.personModify(personChange.getPersonManageId()
				    		, "personManageSex", sexText.getText());
			}
			if(personChange.getPersonManagepassword()!=passText.getText()){
				jpm.personModify(personChange.getPersonManagepassword()
			    		, "personManagepassword",passText.getText());
			}
			JOptionPane.showMessageDialog(mainJframe,"修改成功" );
			 idText.setText("");
             nameText.setText("");
             sexText.setText("");
             passText.setText("");
		}catch(Exception e1){
			System.out.println("修改失败");
			JOptionPane.showMessageDialog(mainJframe,"修改失败" );
		}finally{
			jdbcDrive.jdbcConnectionClose();
		}
	}
	}
	/*
	 * 完成对表格的监听事件
	 */
	private class table_selectActionListener extends  MouseAdapter{
		public void mouseClicked(MouseEvent event)
        {
                int row = DataTable.rowAtPoint(event.getPoint());
                if(row!=-1){
                personChange=new personManage.personManageBuild()
                		.addManageId((String) DataTable.getValueAt(row, 0))
                		.addManageName((String) DataTable.getValueAt(row, 1))
                		.addManageSex((String) DataTable.getValueAt(row, 2))
                		.addManagePass((String) DataTable.getValueAt(row, 3))
                		.CreateManageBuild();
                idText.setText(personChange.getPersonManageId());
                nameText.setText(personChange.getPersonManageName());
                sexText.setText(personChange.getPersonManageSex());
                passText.setText(personChange.getPersonManagepassword());
                }
        }
	}
	/*
	 * 完成对增加按钮的监听
	 */
	private class btn_AddActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			personManage p=new personManage
					.personManageBuild()
					.addManageId(idText.getText().trim())
					.addManageName(nameText.getText().trim())
					.addManageSex(sexText.getText().trim())
					.addManagePass(passText.getText().trim())
					.CreateManageBuild();
			jdbcPersonManage jpm;
			try {
				jpm = new jdbcPersonManage(p);
				jpm.personAdd();
				idText.setText("");
				nameText.setText("");
				passText.setText("");
				sexText.setText("");
				JOptionPane.showMessageDialog(mainJframe,"添加成功" );
			} catch (Exception e1) {
				JOptionPane.showMessageDialog(mainJframe,"无法连接到数据库" );
			}finally{
				jdbcDrive.jdbcConnectionClose();
			}
		}
	}
	/*
	 * 完成对删除按钮的监听
	 */
	private class btn_DeleteActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
	
			personManage p=new personManage
					.personManageBuild()
					.addManageId(idText.getText().trim())
					.CreateManageBuild();
			jdbcPersonManage jpm=
					new jdbcPersonManage(p);
			try {
				jpm.personDelete();
				idText.setText("");
				nameText.setText("");
				passText.setText("");
				sexText.setText("");
				JOptionPane.showMessageDialog(mainJframe, "删除成功");
			} catch (SQLException e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(mainJframe, "删除失败");
			}
		}
	}

	private void setComponent(JComponent component, int gridx, int gridy,
			int gridwidth, int ipadx, boolean fill) {
		final GridBagConstraints gridBagConstrains = new GridBagConstraints();
		gridBagConstrains.gridx = gridx;
		gridBagConstrains.gridy = gridy;
		gridBagConstrains.insets = new Insets(10, 15, 3, 1);
		if (gridwidth > 1)
			gridBagConstrains.gridwidth = gridwidth;
		if (ipadx > 0)
			gridBagConstrains.ipadx = ipadx;
		if (fill)
			gridBagConstrains.fill = GridBagConstraints.HORIZONTAL;
		messageJpn.add(component, gridBagConstrains);
	}
	public static void main(String [] args){
		new personManageUi();
	}
}
学生管理部分实现和这个差不多,需要说一下宿舍管理的部分,在宿舍管理的模块,借助树,来进行完整的修改。



具体的代码如下:


public class dormitoryUpdateUi extends JFrame{
	private JScrollPane JSPane;
	private JTree Tree;
	private JPanel panel;
	private DefaultMutableTreeNode tmpNode,root;
	private DefaultTreeModel insert;
	private ArrayList<Dormitory> dormitory;
	private JTextField id;
	private JTextField sex;
	private JTextField ynumber;
	private JTextField snumber;
	private JTextField student;
	private JTextField money;
	private JTextField note;
	private JButton btn_query,btn_Modify,btn_Delete;
	private String attribute;
	private String changeAttribute;
	public dormitoryUpdateUi(){
		this.setTitle("宿舍更新");
		this.setVisible(true);
		this.setSize(800, 600);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setLayout(new BorderLayout());
		root=new DefaultMutableTreeNode("文赢13号楼                  ");
		insert=new DefaultTreeModel(root);
		Tree=new JTree(root);
		Tree.addTreeSelectionListener(new treeSelectActionListener());
		JSPane=new JScrollPane(Tree);
		this.add(JSPane, BorderLayout.WEST);
		panel=new JPanel();
		//panel.setPreferredSize(getPreferredSize());
		this.add(panel, BorderLayout.CENTER);
		panel.setLayout(new GridBagLayout());
		setComponent(new JLabel("宿舍号:"),0,0,1,1,false);
		id=new JTextField();
		id.addActionListener(new jf_ActionListener());
		setComponent(id,1,0,3,1,true);
		setComponent(new JLabel("住宿性别名:"),0,1,1,1,false);
		sex=new JTextField();
		sex.addActionListener(new jf_ActionListener());
		setComponent(sex,1,1,3,150,true);
		setComponent(new JLabel("应住人数:"),0,2,1,1,false);
		ynumber=new JTextField();
		ynumber.addActionListener(new jf_ActionListener());
		setComponent(ynumber,1,2,3,150,true);
		setComponent(new JLabel("实住人数:"),0,3,1,1,false);
		snumber=new JTextField();
		snumber.addActionListener(new jf_ActionListener());
		setComponent(snumber,1,3,3,150,true);
		setComponent(new JLabel("住宿费:"),0,4,1,1,false);
		money=new JTextField();
		money.addActionListener(new jf_ActionListener());
		setComponent(money,1,4,3,1,true);
		setComponent(new JLabel("舍长:"),0,5,1,1,false);
		student=new JTextField();
		student.addActionListener(new jf_ActionListener());
		setComponent(student,1,5,3,1,true);
		setComponent(new JLabel("备注"),0,6,1,1,false);
		note=new JTextField();
		note.addActionListener(new jf_ActionListener());
		setComponent(note,1,6,3,150,true);
		btn_query=new JButton("查询");
		btn_query.addActionListener(new btn_queryActionListener());
		setComponent(btn_query,0,7,1,1,false);
		btn_Modify=new JButton("修改");
		btn_Modify.addActionListener(new btn_ModifyActionListener());
		setComponent(btn_Modify,1,7,1,1,false);
		btn_Delete=new JButton("删除");
		btn_Delete.addActionListener(new btn_DeleteActionListener());
		setComponent(btn_Delete,2,7,1,1,false);
	}
	
	private class btn_queryActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			jdbcDormitory jd=new jdbcDormitory();
			HashMap<Integer,ArrayList<Dormitory> > number=new HashMap<Integer,ArrayList<Dormitory>>();
			ArrayList<Dormitory> floordormitory;
		    int temp;//统计楼数
			try {
				jd.dormitoryQuery("");
				dormitory=jd.getDormitory();
				for(Dormitory d:dormitory){
					temp=Integer.parseInt(d.getDormitoryId().substring(2,3));
					if(!number.containsKey(temp)){
						floordormitory=new ArrayList<Dormitory>();
						floordormitory.add(d);
						number.put(temp, floordormitory);
					}else{
					  number.get(temp).add(d);
					}
				}
				Object[] floors;
				floors=number.keySet().toArray();
				for(int i=0;i<floors.length;i++)
				{
					floordormitory=number.get(floors[i]);
					tmpNode=new DefaultMutableTreeNode(floors[i]+"楼");
					insert.insertNodeInto(tmpNode, root, 0);
					insert.reload();
					for(int j=0;j<floordormitory.size();j++){
						tmpNode.add(new DefaultMutableTreeNode(floordormitory.get(j).getDormitoryId()));
					}
					floordormitory=null;
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}finally{
				jdbcDrive.jdbcConnectionClose();
			}
		}
	}
	private class treeSelectActionListener implements TreeSelectionListener{
		@Override
		public void valueChanged(TreeSelectionEvent e) {
			if(e.getPath().getLastPathComponent().toString().length()==5){
			Dormitory d=null;
			String message=e.getPath().getLastPathComponent().toString();
			for(Dormitory dor: dormitory){
				if(dor.getDormitoryId().equals(message)){
					d=dor;
				}
			}
			 id.setText(d.getDormitoryId());
			 sex.setText(d.getDormitorySex());
		     ynumber.setText(d.getDormitoryPeople());
			 snumber.setText(d.getDormitoryPeopleNow());
			 student.setText(d.getStudentId());
			 money.setText(d.getMoney());
			 note.setText(d.getDormitoryNote());
			}
		}
	}
	
	private class btn_ModifyActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			jdbcDormitory jd=new jdbcDormitory();
			try{
				jd.dormitoryModify(id.getText().trim()
						, attribute,changeAttribute);
			JOptionPane.showMessageDialog(dormitoryUpdateUi.this,"修改成功" );
			id.setText("");
			sex.setText("");
		    ynumber.setText("");
		    snumber.setText("");
			student.setText("");
		    money.setText("");
			note.setText("");
		}catch(Exception e1){
			e1.printStackTrace();
			System.out.println("修改失败");
			JOptionPane.showMessageDialog(dormitoryUpdateUi.this,"修改失败" );
		}finally{
			jdbcDrive.jdbcConnectionClose();
		}
		}
	}
	
	private class jf_ActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			HashMap<JTextField,String> change=new HashMap<JTextField,String>();
			change.put(id, "Dnumber");
			change.put(sex, "Dsex");
			change.put(ynumber, "Dynumber ");
			change.put(snumber, "Dsnumber ");
			change.put(student, "Dstudent ");
			change.put(money, "Dmoney ");
			change.put(note, "Dnote");
			attribute=change.get((JTextField)e.getSource());
			changeAttribute=((JTextField)e.getSource()).getText().trim();
		}
	}
	private class btn_DeleteActionListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Dormitory d=new Dormitory
					.DormitoryBuilder()
					.addDormitoryId(id.getText().trim())
					.CreateDormitory();
			jdbcDormitory js=new jdbcDormitory();
			try {
				js.dormitoryQuery("where Dnumber ="+"'"+""+d.getDormitoryId()+"'");
				if(Integer.parseInt(js.getDormitory().get(0).getDormitoryPeopleNow())!=0){
					JOptionPane.showMessageDialog(dormitoryUpdateUi.this, "宿舍有成员居住,无法删除");
				}else{
				js.dormitoryDelete(d);
				id.setText("");
				sex.setText("");
				ynumber.setText("");
				snumber.setText("");
				money.setText("");
				note.setText("");
				student.setText("");
				JOptionPane.showMessageDialog(dormitoryUpdateUi.this, "删除成功");
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(dormitoryUpdateUi.this, "删除失败");
			}
		}
		
	}
	private void setComponent(JComponent component, int gridx, int gridy,
			int gridwidth, int ipadx, boolean fill) {
		final GridBagConstraints gridBagConstrains = new GridBagConstraints();
		gridBagConstrains.gridx = gridx;
		gridBagConstrains.gridy = gridy;
		gridBagConstrains.insets = new Insets(20, 20, 3, 1);
		if (gridwidth > 1)
			gridBagConstrains.gridwidth = gridwidth;
		if (ipadx > 0)
			gridBagConstrains.ipadx = ipadx;
		if (fill)
			gridBagConstrains.fill = GridBagConstraints.HORIZONTAL;
		panel.add(component, gridBagConstrains);
	}
	public static void main(String [] args){
		dormitoryUpdateUi d=new dormitoryUpdateUi();
	}
}


下面是完整的源代码地址:http://download.csdn.net/download/deramer1/9872418





展开阅读全文

没有更多推荐了,返回首页