資料庫課程設計-宿舍管理系統

寧悅發表於2017-06-17
最近寫完了資料庫的課程設計,想把整個原始碼的編輯過程發出來。程式很簡單,需要有很多完善的地方,在這裡,我想和大家分享寫這個程式的心路歷程。

首先,在開始寫程式之前。我們需要先寫一些工具類,來輔助完成整個程式的構建,在這裡我把連線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





相關文章