學生學籍管理系統~~功能介面

Dkangel發表於2017-05-23

之前介紹了登入介面的設計,現在介紹一下注冊管理員和錄入資訊的部分。

設計註冊管理員的思想是:預設一個擁有建立使用者的管理員,這個管理員可以建立資料庫登入使用者(相當於建立教師),而這些使用者只能登入和進行其他操作沒有建立使用者的功能(類似於教師不能建立教師), 建立使用者成功後,會將賬號密碼和暱稱儲存到My_Adminstrator表中。

登入介面的作用是驗證是否能登入資料庫,登入成功後將賬號、密碼通過引數的形式通過其他類的建構函式傳遞過去,再在其他類中重新連線資料庫。程式碼中我去掉了import。

public class Register extends JFrame implements ActionListener{	
	Font font = new Font("楷體_GB2312",Font.BOLD+Font.ITALIC,20);
	Font font1 = new Font("楷體_GB2312",Font.BOLD+Font.ITALIC,15);
	
	JTextField register_account;
	JTextField register_name;
	JTextField register_password;
	
	String account;
	String password;
	String connectDB = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Databaseproject";
	String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	Connection conR;
    Statement stmtR;
	
	JButton JB_sure = new JButton("確定");
	JButton JB_reset = new JButton("重置");
	
	String path = "C:/Users/yangcheng/workspace/DatabaseExperiment/src"
			+ "/Graphical_User_Interface/choice.png";
	String path1 = "C:/Users/yangcheng/workspace/DatabaseExperiment/src"
			+ "/Graphical_User_Interface/成功.png";
	
	JPanel jpl = new JPanel();
	JPanel jpl1 = new JPanel();
	JPanel jpl2 = new JPanel();
	JPanel jpl3 = new JPanel();
	
	public Register(String ac,String pw){
		account = ac;
		password = pw;
		try {
	        Class.forName(JDriver);
	        conR = DriverManager.getConnection(connectDB, account, password);
	        stmtR = conR.createStatement();
	    } catch (ClassNotFoundException | SQLException e) {
	    	dispose();
	    }
		this.setTitle("管理員註冊");
		this.setSize(280,230);
		this.setLayout(new GridLayout(4,2,15,20));
		this.setLocationRelativeTo(null);
		
		ImageIcon bg = new ImageIcon(path);                        //背景圖片
		JLabel label = new JLabel(bg);
		label.setBounds(0,0,bg.getIconWidth(),bg.getIconHeight());
		this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
		JPanel imgP = (JPanel)this.getContentPane();
		imgP.setOpaque(false);
		
		JLabel lba = new JLabel("賬號:");							//賬號
		register_account = new JTextField(10);						
		lba.setFont(font);	
		register_account.setFont(font1);
		jpl.add(lba);
		jpl.add(register_account);
		jpl.setOpaque(false);
		this.add(jpl);
		
		JLabel lbn = new JLabel("姓名:");							//使用者名稱
		register_name = new JTextField(10);						
		lbn.setFont(font);	
		register_name.setFont(font1);
		jpl1.add(lbn);
		jpl1.add(register_name);
		jpl1.setOpaque(false);
		this.add(jpl1);
		
		JLabel lbp = new JLabel("密碼:");							//密碼
		register_password = new JTextField(10);						
		lbp.setFont(font);
		register_password.setFont(font1);
		jpl2.add(lbp);
		jpl2.add(register_password);
		jpl2.setOpaque(false);
		this.add(jpl2);
		
		JB_sure.setActionCommand("sure");
		JB_sure.addActionListener(this);
		jpl3.add(JB_sure);
		
		JB_reset.setActionCommand("reset");
		JB_reset.addActionListener(this);
		jpl3.add(JB_reset);
		
		jpl3.setOpaque(false);
		this.add(jpl3);		
        this.setVisible(true);
		this.setResizable(false);
	}
	
	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand().equals("sure")){
			String user = register_account.getText();
			String name = register_name.getText();
			String pwd = register_password.getText();			
			if(!user.equals("") && !name.equals("") && !pwd.equals("")){
				try{
					ResultSet rs = stmtR.executeQuery("select * from My_Adminstrator");
					while (rs.next()) {
						if(rs.getString("Account").equals(user)){
							JOptionPane.showMessageDialog(null,"該使用者已經存在");
							stmtR.close();
					        conR.close();
							dispose();
							break;
			            }
					}
					String str = "create login "+user+" with password = '"+pwd                   //建立登入使用者,並指定資料庫
								 +"', default_database=Databaseproject";
					
					String str1 = "create user "+user+" for login "+ user+" with default_schema=Databaseproject";//建立使用者
					
					String str2 = "exec sp_addrolemember 'db_owner', '"+user+"'";//賦予許可權
					
					String sql = "insert into My_Adminstrator values('"+user+"','"+name+"','"+pwd+"')";//新增到表中
					
					stmtR.executeUpdate(str);
					stmtR.executeUpdate(str1);
					stmtR.executeUpdate(str2);
					stmtR.executeUpdate(sql);
					
					JOptionPane.showMessageDialog(null,"註冊成功");
					stmtR.close();
			        conR.close();
			        dispose();
				}catch(Exception ex){
					JOptionPane.showMessageDialog(null,"使用者許可權不夠");
					try {
						stmtR.close();
						conR.close();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
			        dispose();
				}
			}else{
				JOptionPane.showMessageDialog(null,"資訊不能為空");
			}
		}
		
		if(e.getActionCommand().equals("reset")){
			register_account.setText(null);
			register_name.setText(null);
			register_password.setText(null);
		}
	}
}

註冊介面: 程式碼的邏輯很簡單,通過文字框的形式獲取賬號、姓名、密碼,判斷是否為空,資料庫表中是否存在該使用者,如果不存在且是超級管理員則成功,反之則失敗。


由於錄入資訊的程式碼比較繁瑣比較多,我就挑一個最少的介紹吧,學籍介面,程式碼中我去掉了import。

public class School_Manage extends JFrame implements ActionListener{
	String account;
	String password;
	String connectDB = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Databaseproject";
	String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	Connection conR;
    Statement statement;
	
	Font font0 = new Font("楷體_GB2312",Font.BOLD+Font.ITALIC,40);
	Font fontjb = new Font("楷體_GB2312",Font.BOLD,15);
	Font fontarea = new Font("楷體_GB2312",Font.BOLD,25);
	Font fontjt = new Font("楷體_GB2312",Font.BOLD,20);
	
	String path = "C:/Users/yangcheng/workspace/DatabaseExperiment/src"
			+ "/Graphical_User_Interface/背景.png";
	Font font = new Font("楷體_GB2312",Font.BOLD+Font.ITALIC,20);
	
	JPanel jpl = new JPanel(null);  //將內容載入到上面,起到介面重新整理作用
	
	JButton JB_add = new JButton("錄入");
	JButton JB_change = new JButton("修改");
	JButton JB_search = new JButton("查詢");
	JButton JB_display = new JButton("輸出");
	JButton JB_back = new JButton("返回");
	
	JRadioButton jrbmilifalse;
	JRadioButton jrbmilitrue;
	JTextField jtid;													//學號
	JTextField jtawardtext;												//獎學金
	JTextField jtloantext;												//貸款
	JRadioButton jrbregtrue;
	JRadioButton jrbregfalse;
	JTextField jtunusualtext;											//異動情況
	JTextField jtmilitarytext;											//軍訓情況
	JTextField jtgraduationtext;										//是否畢業
	JRadioButton jrbgradfalse;
	JRadioButton jrbgradtrue;

	JScrollPane jsinfor;
	JTextArea jtinfor;
	
	int snum; String strnum;
	String saward;
	String sloan;
	String sregister;													
	String sunusual;													
	String smilitary;													
	String sgraduation;	
	//判斷點選的是哪個gong
	boolean flag_add = false;
	boolean flag_change = false;
	boolean flag_search = false;
	boolean flag_display = false;
	
	public School_Manage(String ac, String pw){
		this.setTitle("學籍管理系統");	
		this.setSize(516 , 530);
		this.setLayout(new FlowLayout(FlowLayout.CENTER,30,10));
		this.setLocationRelativeTo(null);
		
		account = ac;
		password = pw;
		try {
	        Class.forName(JDriver);
	        conR = DriverManager.getConnection(connectDB, account, password);
	        statement = conR.createStatement();
	    } catch (ClassNotFoundException | SQLException e) {
	    	dispose();
	    }
		
		ImageIcon bg = new ImageIcon(path);                            //背景圖片
		JLabel label = new JLabel(bg);
		label.setBounds(0,0,bg.getIconWidth(),bg.getIconHeight());
		this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
		JPanel imgP = (JPanel)this.getContentPane();
		imgP.setOpaque(false);
		
		JLabel lb0 = new JLabel("學生學籍管理");                           //標題
		lb0.setFont(font0);
		this.add(lb0);
		
		JButton jbt = new JButton();							 		//換行
		jbt.setPreferredSize(new Dimension(400,1));  
		jbt.setBorderPainted(false);
		jbt.setContentAreaFilled(false);
		this.add(jbt);
		
		JB_add.setBorderPainted(false);									//錄入
		JB_add.setActionCommand("jbadd");
		JB_add.addActionListener(this);
		JB_add.setFont(fontjb);
		this.add(JB_add);
		
		JB_change.setBorderPainted(false);								//修改
		JB_change.setActionCommand("jbchange");
		JB_change.addActionListener(this);
		JB_change.setFont(fontjb);
		this.add(JB_change);
		
		JB_search.setBorderPainted(false);								//查詢
		JB_search.setActionCommand("jbsearch");
		JB_search.addActionListener(this);
		JB_search.setFont(fontjb);
		this.add(JB_search);
		
		JB_display.setBorderPainted(false);								//輸出
		JB_display.setActionCommand("jbdisplay");
		JB_display.addActionListener(this);
		JB_display.setFont(fontjb);
		this.add(JB_display);
		
		JB_back.setBorderPainted(false);								//返回
		JB_back.setActionCommand("jbback");
		JB_back.addActionListener(this);
		JB_back.setFont(fontjb);
		this.add(JB_back);
		
		JButton jbt1 = new JButton();							 		//換行
		jbt1.setPreferredSize(new Dimension(400,0));  
		jbt1.setBorderPainted(false);
		jbt1.setContentAreaFilled(false);
		this.add(jbt1);
		
		this.setVisible(true);
		this.setResizable(false);
	}

//***********************************************************************
	public JPanel createAdd(){											//錄入介面
		jpl.setLayout(new FlowLayout(FlowLayout.CENTER,30,5));
		jpl.setPreferredSize(new Dimension(516, 530));
		
		//學號****************************************
		JLabel lbid = new JLabel("學號:");   							//學號
		lbid.setFont(fontjt);
		jtid = new JTextField(5);
		jtid.setFont(fontjb);
		jpl.add(lbid);
		jpl.add(jtid);
		
		//換行
		JButton jbt6 = new JButton();		
		jbt6.setPreferredSize(new Dimension(400,0));  
		jbt6.setBorderPainted(false);
		jbt6.setContentAreaFilled(false);
		jpl.add(jbt6);
		//獎學金***************************************
		JLabel lbaward = new JLabel("獎學金:");   
		lbaward.setFont(fontjt);
		jpl.add(lbaward);
		
		ButtonGroup bgaward = new ButtonGroup();
		
		JRadioButton jrbawardfalse = new JRadioButton("無",false);
		jrbawardfalse.setFont(fontjt);
		jrbawardfalse.setContentAreaFilled(false);
		jrbawardfalse.setActionCommand("awardfalse");
		jrbawardfalse.addActionListener(this);
		jpl.add(jrbawardfalse);
		
		JRadioButton jrbawardtrue = new JRadioButton("有",false);
		jrbawardtrue.setFont(fontjt);
		jrbawardtrue.setContentAreaFilled(false);
		jrbawardtrue.setActionCommand("awardtrue");
		jrbawardtrue.addActionListener(this);
		jpl.add(jrbawardtrue);
		
		jtawardtext = new JTextField(5);
		jtawardtext.setFont(fontjb);
		jpl.add(jtawardtext);
		
		bgaward.add(jrbawardtrue);
		bgaward.add(jrbawardfalse);
		
		//換行
		JButton jbt = new JButton();		
		jbt.setPreferredSize(new Dimension(400,0));  
		jbt.setBorderPainted(false);
		jbt.setContentAreaFilled(false);
		jpl.add(jbt);
		
		//貸款***************************************
		JLabel lbloan = new JLabel("貸款:");   
		lbloan.setFont(fontjt);
		jpl.add(lbloan);
		
		ButtonGroup bgloan = new ButtonGroup();
		
		JRadioButton jrbloanfalse = new JRadioButton("無",false);
		jrbloanfalse.setFont(fontjt);
		jrbloanfalse.setContentAreaFilled(false);
		jrbloanfalse.setActionCommand("loanfalse");
		jrbloanfalse.addActionListener(this);
		jpl.add(jrbloanfalse);
		
		JRadioButton jrbloantrue = new JRadioButton("有",false);
		jrbloantrue.setFont(fontjt);
		jrbloantrue.setContentAreaFilled(false);
		jrbloantrue.setActionCommand("loantrue");
		jrbloantrue.addActionListener(this);
		jpl.add(jrbloantrue);
		
		jtloantext = new JTextField(5);
		jtloantext.setFont(fontjb);
		jpl.add(jtloantext);
		
		bgloan.add(jrbloantrue);
		bgloan.add(jrbloanfalse);
		
		//換行
		JButton jbt5 = new JButton();		
		jbt5.setPreferredSize(new Dimension(400,0));  
		jbt5.setBorderPainted(false);
		jbt5.setContentAreaFilled(false);
		jpl.add(jbt5);
		
		//註冊情況***************************************
		JLabel lbregister = new JLabel("註冊情況:");   
		lbregister.setFont(fontjt);
		jpl.add(lbregister);
		
		ButtonGroup bgregister = new ButtonGroup();
		
		jrbregtrue = new JRadioButton("是",false);
		jrbregtrue.setFont(fontjt);
		jrbregtrue.setContentAreaFilled(false);
		jrbregtrue.setActionCommand("registertrue");
		jrbregtrue.addActionListener(this);
		jpl.add(jrbregtrue);
		
		jrbregfalse = new JRadioButton("否",false);
		jrbregfalse.setFont(fontjt);
		jrbregfalse.setContentAreaFilled(false);
		jrbregfalse.setActionCommand("registerfalse");
		jrbregfalse.addActionListener(this);
		jpl.add(jrbregfalse);
		
		bgregister.add(jrbregtrue);
		bgregister.add(jrbregfalse);
		
		//換行
		JButton jbt1 = new JButton();							 		
		jbt1.setPreferredSize(new Dimension(400,0));  
		jbt1.setBorderPainted(false);
		jbt1.setContentAreaFilled(false);
		jpl.add(jbt1);
		
		//異動情況***************************************
		JLabel lbunusual = new JLabel("異動情況:");
		lbunusual.setFont(fontjt);
		jpl.add(lbunusual);
		
		ButtonGroup bgunusual = new ButtonGroup();
		
		JRadioButton jrbunfalse = new JRadioButton("無",false);
		jrbunfalse.setFont(fontjt);
		jrbunfalse.setContentAreaFilled(false);
		jrbunfalse.setActionCommand("unusualfalse");
		jrbunfalse.addActionListener(this);
		jpl.add(jrbunfalse);
		
		JRadioButton jrbuntrue = new JRadioButton("有",false);
		jrbuntrue.setFont(fontjt);
		jrbuntrue.setContentAreaFilled(false);
		jrbuntrue.setActionCommand("unusualtrue");
		jrbuntrue.addActionListener(this);
		jpl.add(jrbuntrue);
		
		jtunusualtext = new JTextField(5);
		jtunusualtext.setFont(fontjb);
		jpl.add(jtunusualtext);
		
		bgunusual.add(jrbuntrue);
		bgunusual.add(jrbunfalse);
		
		//換行
		JButton jbt2 = new JButton();							 		
		jbt2.setPreferredSize(new Dimension(400,0));  
		jbt2.setBorderPainted(false);
		jbt2.setContentAreaFilled(false);
		jpl.add(jbt2);
		
		//軍訓情況***************************************
		JLabel lbmilitary = new JLabel("軍訓情況:");
		lbmilitary.setFont(fontjt);
		jpl.add(lbmilitary);
		
		ButtonGroup bgmilitary = new ButtonGroup();
		
		jrbmilifalse = new JRadioButton("無",false);
		jrbmilifalse.setFont(fontjt);
		jrbmilifalse.setContentAreaFilled(false);
		jrbmilifalse.setActionCommand("militaryfalse");
		jrbmilifalse.addActionListener(this);
		jpl.add(jrbmilifalse);
		
		jrbmilitrue = new JRadioButton("有",false);
		jrbmilitrue.setFont(fontjt);
		jrbmilitrue.setContentAreaFilled(false);
		jrbmilitrue.setActionCommand("militarytrue");
		jrbmilitrue.addActionListener(this);
		jpl.add(jrbmilitrue);
		
		jtmilitarytext = new JTextField(5);
		jtmilitarytext.setFont(fontjb);
		jpl.add(jtmilitarytext);
		
		bgmilitary.add(jrbmilifalse);
		bgmilitary.add(jrbmilitrue);
		
		//換行
		JButton jbt3 = new JButton();							 		
		jbt3.setPreferredSize(new Dimension(400,0));  
		jbt3.setBorderPainted(false);
		jbt3.setContentAreaFilled(false);
		jpl.add(jbt3);
		
		//畢業情況***************************************
		JLabel lbgraduation = new JLabel("畢業情況:");   
		lbgraduation.setFont(fontjt);
		jpl.add(lbgraduation);
		
		ButtonGroup bggraduation = new ButtonGroup();
		
		jrbgradfalse = new JRadioButton("否",false);
		jrbgradfalse.setFont(fontjt);
		jrbgradfalse.setContentAreaFilled(false);
		jrbgradfalse.setActionCommand("graduationfalse");
		jrbgradfalse.addActionListener(this);
		jpl.add(jrbgradfalse);
		
		jrbgradtrue = new JRadioButton("是",false);
		jrbgradtrue.setFont(fontjt);
		jrbgradtrue.setContentAreaFilled(false);
		jrbgradtrue.setActionCommand("graduationtrue");
		jrbgradtrue.addActionListener(this);
		jpl.add(jrbgradtrue);
		
		bggraduation.add(jrbgradfalse);
		bggraduation.add(jrbgradtrue);
		
		//換行
		JButton jbt4 = new JButton();							 		
		jbt4.setPreferredSize(new Dimension(400,0));  
		jbt4.setBorderPainted(false);
		jbt4.setContentAreaFilled(false);
		jpl.add(jbt4);
		
		if(flag_add || flag_change){
			JButton JB_sure = new JButton("確定");
			JB_sure.setBorderPainted(false);								//確定
			JB_sure.setActionCommand("jbSsure");
			JB_sure.addActionListener(this);
			JB_sure.setFont(fontjb);
			jpl.add(JB_sure);
		}		
		return jpl;
	}
	
	public JPanel createChange(){										//修改介面
		jpl = createAdd();
		jtid.setText(strnum);
		jtawardtext.setText(saward);
		jtloantext.setText(sloan);
		if(sregister.equals("是")){
			jrbregtrue.setSelected(true);
		}else{
			jrbregfalse.setSelected(true);
		}
		jtunusualtext.setText(sunusual);
		jtmilitarytext.setText(smilitary);
		if(sgraduation.equals("是")){
			jrbgradtrue.setSelected(true);
		}else{
			jrbgradfalse.setSelected(true);
		}		
		return jpl;
	}
	
	public JPanel createSearch(){										//查詢介面
		jpl = createAdd();
		jtid.setText(strnum);
		jtawardtext.setText(saward);
		jtloantext.setText(sloan);
		if(sregister.equals("是")){
			jrbregtrue.setSelected(true);
		}else{
			jrbregfalse.setSelected(true);
		}
		jtunusualtext.setText(sunusual);
		jtmilitarytext.setText(smilitary);
		if(sgraduation.equals("是")){
			jrbgradtrue.setSelected(true);
		}else{
			jrbgradfalse.setSelected(true);
		}		
		return jpl;
	}
	
	public JPanel createDisplay(){										//輸出介面
		jpl.setLayout(new FlowLayout(FlowLayout.CENTER,30,5));
		jpl.setPreferredSize(new Dimension(516, 530));
		
		JLabel lbinfor = new JLabel("學生學籍表全部資訊:");
		lbinfor.setFont(fontarea);
		jpl.add(lbinfor);
		
		JButton jbt = new JButton();							 		//換行
		jbt.setPreferredSize(new Dimension(400,0));  
		jbt.setBorderPainted(false);
		jbt.setContentAreaFilled(false);
		jpl.add(jbt);
		
		jtinfor = new JTextArea(15,40);
		jtinfor.setFont(fontjb);
		jtinfor.setText("學生學籍資訊如下:\n");
		jtinfor.setCaretPosition(0);
		
		jsinfor = new JScrollPane(jtinfor);                               //以滾動框的形式輸出的資訊
		jpl.add(jsinfor);		
		return jpl;
	}
//***********************************************************************	
	
	public void actionPerformed(ActionEvent e){
		if(e.getActionCommand().equals("jbadd")){						//錄入操作
			flag_add = true;
			flag_change = false;
			flag_search = false;
			flag_display = false;
			
			jpl.removeAll();
			jpl.repaint();
			jpl = createAdd();
			jpl.setOpaque(false);
			this.add(jpl);
			this.validate();
		}
		
		if(e.getActionCommand().equals("jbchange")){					//修改操作
			flag_add = false;
			flag_change = true;
			flag_search = false;
			flag_display = false;
			
			strnum = JOptionPane.showInputDialog("輸入要更新的學號:");
			boolean flag = false;
			if(strnum.equals("")){
				JOptionPane.showMessageDialog(null,"學號不能為空!!!");
			}else{
				snum = Integer.valueOf(strnum).intValue();
				try {
					ResultSet result_snum = statement.executeQuery("select snum from School_Manage");
					while (result_snum.next()) {
						int num = Integer.valueOf(result_snum.getString("snum")).intValue();
						if(num == snum){
							flag = true;
							break;
			            }
					}
					if(flag){
						ResultSet result_snum_infor = statement.executeQuery("select * "
													+"from School_Manage where snum = '"+snum+"'");
						while(result_snum_infor.next()){
							saward = result_snum_infor.getString("saward");
							sloan = result_snum_infor.getString("sloan");
							sregister = result_snum_infor.getString("sregister");
							sunusual = result_snum_infor.getString("sunusual");
							smilitary = result_snum_infor.getString("smilitary");
							sgraduation = result_snum_infor.getString("sgraduation");
						}
						jpl.removeAll();
						jpl.repaint();
						
						jpl = createChange();
						
						jpl.setOpaque(false);
						this.add(jpl);
						this.validate();
					}else{
						JOptionPane.showMessageDialog(null,"學生檔案資訊不存在,請建立");
						jpl.removeAll();
						jpl.repaint();
						jpl.setOpaque(false);
						this.add(jpl);
						this.validate();
					}
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(null,"資料庫操作失敗!!!");
				}
			}
		}
		
		if(e.getActionCommand().equals("jbsearch")){					//查詢操作
			flag_add = false;
			flag_change = false;
			flag_search = true;
			flag_display = false;
			
			boolean flag = false;
			strnum = JOptionPane.showInputDialog("輸入要查詢的學號:");
			if(strnum.equals("")){
				JOptionPane.showMessageDialog(null,"學號不能為空!!!");
			}else{
				snum = Integer.valueOf(strnum).intValue();
				try {
					ResultSet result_snum = statement.executeQuery("select * from School_Manage");
					while (result_snum.next()) {
						int num = Integer.valueOf(result_snum.getString("snum")).intValue();
						if(num == snum){
							flag = true;
							break;
			            }
					}
					if(flag){
						ResultSet result_snum_infor = statement.executeQuery("select * "
													+"from School_Manage where snum = '"+snum+"'");
						while(result_snum_infor.next()){
							saward = result_snum_infor.getString("saward");
							sloan = result_snum_infor.getString("sloan");
							sregister = result_snum_infor.getString("sregister");
							sunusual = result_snum_infor.getString("sunusual");
							smilitary = result_snum_infor.getString("smilitary");
							sgraduation = result_snum_infor.getString("sgraduation");
						}
						jpl.removeAll();
						jpl.repaint();
						jpl = createSearch();
						jpl.setOpaque(false);
						this.add(jpl);
						this.validate();
					}else{
						JOptionPane.showMessageDialog(null,"學生學籍資訊不存在,請建立");
						jpl.removeAll();
						jpl.repaint();
						jpl.setOpaque(false);
						this.add(jpl);
						this.validate();
					}
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(null,"資料庫操作失敗!!!");
				}
			}
		}
		
		if(e.getActionCommand().equals("jbdisplay")){					//輸出操作
			flag_add = false;
			flag_change = false;
			flag_search = false;
			flag_display = true;
			
			boolean flag = false;
			try {
				ResultSet result_infor_exist = statement.executeQuery("select * from School_Manage order by snum");
				while (result_infor_exist.next()) {
					flag = true;
					break;
				}
				
				if(flag){
					jpl.removeAll();
					jpl.repaint();
					jpl = createDisplay();
					ResultSet result_infor = statement.executeQuery("select * from School_Manage order by snum");
					while(result_infor.next()){
						strnum = result_infor.getString("snum");
						saward = result_infor.getString("saward");
						sloan = result_infor.getString("sloan");
						sregister = result_infor.getString("sregister");
						sunusual = result_infor.getString("sunusual");
						smilitary = result_infor.getString("smilitary");
						sgraduation = result_infor.getString("sgraduation");
						
						jtinfor.append("學號:"+strnum+"\n");
						jtinfor.append("獎學金:"+saward+"\n");
						jtinfor.append("貸款:"+sloan+"\n");
						jtinfor.append("註冊情況:"+sregister+"\n");
						jtinfor.append("異動情況:"+sunusual+"\n");
						jtinfor.append("軍訓情況:"+smilitary+"\n");
						jtinfor.append("是否畢業:"+sgraduation+'\n'+"*****"
										+ "****************"+'\n');
					}
					jpl.setOpaque(false);
					this.add(jpl);
					this.validate();
				}else{
					JOptionPane.showMessageDialog(null,"學生檔案表中沒有資訊!!!");
					jpl.removeAll();
					jpl.repaint();
					jpl.setOpaque(false);
					this.add(jpl);
					this.validate();
				}
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(null,"資料庫操作失敗!!!");
			}
		}
		
		if(e.getActionCommand().equals("jbback")){						//返回操作
			try {
				statement.close();
				conR.close();
				dispose();
			} catch (SQLException e1) {
			}
			dispose();
		}
		
		if(e.getActionCommand().equals("awardfalse")){					//獎學金
			jtawardtext.setText("無");
		}
		if(e.getActionCommand().equals("awardtrue")){					
			jtawardtext.setText("具體內容:");
		}
		
		if(e.getActionCommand().equals("loanfalse")){					//貸款
			jtloantext.setText("無");
		}
		if(e.getActionCommand().equals("loantrue")){					
			jtloantext.setText("具體內容:");
		}
		
		if(e.getActionCommand().equals("registerfalse")){				//註冊情況
			sregister = "否";
		}
		if(e.getActionCommand().equals("registertrue")){	
			sregister = "是";
		}
		
		if(e.getActionCommand().equals("unusualfalse")){				//異動情況
			jtunusualtext.setText("無");
		}
		if(e.getActionCommand().equals("unusualtrue")){
			jtunusualtext.setText("具體情況:");
		}
		
		if(e.getActionCommand().equals("militaryfalse")){				//軍訓情況
			jtmilitarytext.setText("無");
		}
		if(e.getActionCommand().equals("militarytrue")){	
			jtmilitarytext.setText("成績:");
		}
		
		if(e.getActionCommand().equals("graduationfalse")){				//是否畢業
			sgraduation = "否";
		}
		if(e.getActionCommand().equals("graduationtrue")){	
			sgraduation = "是";
		}
		
		if(e.getActionCommand().equals("jbSsure")){								//確定操作
			boolean exit_R = false;
			boolean exit_S = true;
			
			if(jtid.getText().equals("")){
				JOptionPane.showMessageDialog(null,"學號不能為空!!!");
			}else{
				try{
					snum = Integer.valueOf(jtid.getText()).intValue();
					saward = jtawardtext.getText();
					sloan = jtloantext.getText();
					sunusual = jtunusualtext.getText();
					smilitary = jtmilitarytext.getText();

					String insert = "insert into School_Manage values('"+snum+"','"+saward+"','"+sloan+"','"
							+sregister+"','"+sunusual+"','"+smilitary+"','"+sgraduation+"')";
					if(flag_add){
						ResultSet result_snum_R = statement.executeQuery("select snum from Record_Manage");
						while (result_snum_R.next()){
							int num_R = Integer.valueOf(result_snum_R.getString("snum")).intValue();
							if(snum == num_R){
								exit_R = true;
								break;
				            }
						}
						if(exit_R){
							ResultSet result_snum_S = statement.executeQuery("select snum from School_Manage");
							while (result_snum_S.next()) {
								int num_S = Integer.valueOf(result_snum_S.getString("snum")).intValue();
								if(snum == num_S){
									exit_S = false;
									break;
					            }
							}
							if(exit_S){
								statement.executeUpdate(insert);
								JOptionPane.showMessageDialog(null,"錄入成功!!!");
							}else{
								JOptionPane.showMessageDialog(null,"學生學籍資訊存在,無法重新錄入!!!");
							}
						}else{
							JOptionPane.showMessageDialog(null,"學生檔案中不存在該學生,無法錄入學籍資訊!!!");
						}
					}
					if(flag_change){
						String delete = "delete from School_Manage where snum = '"+snum+"'";
						statement.executeUpdate(delete);
						statement.executeUpdate(insert);
						JOptionPane.showMessageDialog(null,"修改成功!!!");
					}
				} catch (SQLException e1) {
					JOptionPane.showMessageDialog(null,"資料庫操作失敗!!!");
				}
			}
		}
	}
}

部分效果截圖:

往資料庫裡存資料是一個計較繁瑣的過程,邏輯一定要理清楚,不然可能出現各種各樣的bug,基本過程是通過學號判斷是否存在該學生,錄入:判斷該學生的學籍資訊是否存在,如果存在則錄入失敗;修改:輸入學號,不存在則失敗,存在則顯示學生學籍資訊,可以對其資訊進行修改;查詢:通過學號查詢資訊;輸出:用滾動框輸出學籍表中所有資訊。

修改資訊這裡有個偷懶的方法,由於學籍表沒有外來鍵等約束,修改時先delete然後insert,這樣就節省了程式碼,但是如果該表有其他表的外來鍵等,這樣的操作反而會讓自己很煩惱,親身經歷,所以還是老老實實敲update程式碼吧!






相關文章