學生管理系統(課程設計附帶原始碼)

2021-03-10 12:00:02

1. 問題要求及任務描述
1.1. 題目要求
學生管理系統
對學生資訊管理系統,要求完成以下基本任務:

  1. 改寫程式為良好程式風格(檔案註釋,函數註釋,語句註釋)。
  2. 將功能補充完全(基於檔案處理,完成重新整理和儲存功能)。
  3. 將學生資訊改為更好的資料組織,而非離散形式(結構體)

1.2. 主要任務

  1. 能夠對學生的有關資料進行輸入,查詢,修改,刪除。
  2. 能夠為學校提供強大的查詢功能,以便管理人員瞭解和掌握學生的具體 情況。
  3. 能夠對資料庫和登陸記錄進行清理。
  4. 可以對使用者進行修改密碼,新增使用者。
    2.1. 採用解決問題的方法
    首先就是學生資訊的錄入,要求將資訊用檔案儲存,在這裡我設計了一個類Student,有9個成員變數分別是
    private int id; //學生編號
    private String stuName;//學生姓名
    private String stuXue;//學生學院
    private String stuIdcarde;//學生學號
    private String stuSex;//學生性別
    private String stuAge;//學生年齡
    private String stuAddress;//學生地址
    private String stuPhone;//學生聯絡方式
    private String stuTypeid;//學生班級,
    除了id(自加,且唯一)是int型別,其他都是string型別,同時裡面還有成員函數,
    public int add(Connection con,Student student)throws Exception
    public int update(Connection con,Student student)throws Exception
    分別向程式裡面讀資料和寫入資料,還有負責添一條資料的成員函數add()可以新增一條資訊,同時我將學生的資訊有list容器存放。。
    然後我是先做查詢的,因為這有做了查詢才能刪除和修改,查詢我也是按照學號查詢,查詢出來的資訊在右邊的範例編輯框中展示出來,然後對編輯框進行一些屬性的調整,例如可下滑(當文字長度過長時可下滑)、可換行、唯讀,然後學號和姓名等都設定可輸入編輯框,只有當對應查詢方式有效時才能進行查詢,,若有效會進行查詢將對應結果輸出,無輸出結果會有對應的提示框彈出。然後我做了刪除和修改,這是用按照學號查詢的方法來查詢的。 在這裡我們可以看到,通過對學生學號的搜尋從而查詢所要查詢的學生資訊


2.2. 主要演演算法和處理流程圖
學生管理系統主要流程圖
2.3功能演示
請新增圖片描述
請新增圖片描述
請新增圖片描述
2.4專案原始碼
1.dao類


```java
在這裡插入程式碼片
package hiai.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import hiai.model.GradeType;
import hiai.model.Student;
import hiai.util.StringUtil;

/**
 * 
 * 
 * 對學生的新增,刪除,修改。
 * @author zzz
 *
 */
public class StudentDao {
	/**
	 * 
	 * 學生新增
	 */
	public int add(Connection con,Student student)throws Exception {
		
		String sql="insert into stu_student values(null,?,?,?,?,?,?,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, student.getStuName());
		pstmt.setString(2, student.getStuXue());
		pstmt.setString(3, student.getStuIdcarde());
		pstmt.setString(4, student.getStuSex());
		pstmt.setString(5, student.getStuAge());
		pstmt.setString(6, student.getStuAddress());
		pstmt.setString(7, student.getStuPhone());
		pstmt.setString(8, student.getStuTypeid());
		return pstmt.executeUpdate();
	}
	/**
	 * 查詢學生集合
	 */
	public ResultSet list(Connection con,Student student)throws Exception {
		StringBuffer sb=new StringBuffer("select * from stu_student");
		if(StringUtil.isNotEmpty(student.getStuIdcarde())) {
			sb.append(" and stuIdcard like '%"+student.getStuIdcarde()+"%'");
			
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		
		return pstmt.executeQuery();
		
	}
	/**
	 * 學生刪除
	 */
	public int delete (Connection con,String id)throws Exception{
		String sql="delete from stu_student where id=?";//刪除操作
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, id);
		
		return pstmt.executeUpdate();
	}
	
	/**
	 * 
	 * 學生修改
	 */
	public int update(Connection con,Student student)throws Exception{
		String sql="update stu_student set stuName=?,stuXue=?,stuIdcard=?,stuSex=?,stuAge=?,stuAddress=?,stuPhone=?,stuTypeid=? where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, student.getStuName());
		pstmt.setString(2, student.getStuXue());
		pstmt.setString(3, student.getStuIdcarde());
		pstmt.setString(4, student.getStuSex());
		pstmt.setString(5, student.getStuAge());
		pstmt.setString(6, student.getStuAddress());
		pstmt.setString(7, student.getStuPhone());
		pstmt.setString(8, student.getStuTypeid());
		pstmt.setInt(9, student.getId());	
		return pstmt.executeUpdate();
		
	}
	

}
package hiai.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import hiai.model.GradeType;
import hiai.util.StringUtil;

/**
 * 班級類別類
 * 班級新增,刪除,更新,查詢的操作
 * 
 * @author zzz
 *
 */

public class GradeTypeDao {
	/*
	 * 班級新增
	 */
	public int add(Connection con,GradeType gradeType)throws Exception{
		String sql="insert into stu_grade values (null,?,?,?)";//班級新增sql語句
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, gradeType.getGradeXue());
		pstmt.setString(2, gradeType.getGradeName());
		pstmt.setString(3, gradeType.getGradeDesc());
		return pstmt.executeUpdate();	
	}
	/*
	 * 
	 * 查詢班級集合
	 */
	public ResultSet list(Connection con,GradeType gradeType)throws Exception{
		StringBuffer sb=new StringBuffer("select * from stu_grade");
		if(StringUtil.isNotEmpty(gradeType.getGradeName())) {
			sb.append(" and gradeName like '%"+gradeType.getGradeName()+"%'");
			
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and","where"));
		return pstmt.executeQuery();
		
		
		
	}
	/*
	 * 班級刪除
	 */
	public int delete (Connection con,String id)throws Exception{
		String sql="delete from stu_grade where id=?";//刪除操作
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, id);
		
		return pstmt.executeUpdate();
	}
	/*
	 * 班級修改
	 */
	public int update(Connection con,GradeType gradeType)throws Exception{
		String sql="update stu_grade set gradeXue=?,gradeName=?,gradeDesc=? where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, gradeType.getGradeXue());
		pstmt.setString(2, gradeType.getGradeName());
		pstmt.setString(3, gradeType.getGradeDesc());
		pstmt.setInt(4, gradeType.getId());
		return pstmt.executeUpdate();
		
	}
}
package hiai.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.mysql.cj.xdevapi.PreparableStatement;

import hiai.model.User;

/**
 * 使用者存取資料庫
 * @author zzz
 *
 */
public class UserDao {
	/*
	 * 登陸驗證,判斷使用者名稱和密碼是否正確。
	 */
	public User login(Connection con,User user) throws Exception{
		User resultUser=null;
		String sql="select * from stu_user where username =? and password=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
		pstmt.setString(2, user.getPassWord());
		ResultSet rs= pstmt.executeQuery();
		if(rs.next()) {
			resultUser=new User();
			resultUser.setId(rs.getInt("id"));
			resultUser.setUserName(rs.getString("userName"));
			resultUser.setPassWord(rs.getString("passWord"));
			
			
		}
		
		return resultUser;
		
		
	}

}



2.model類

package hiai.model;
/**
 * 
 * 
 * 班級分類實體
 * @author zzz
 *
 */

public class GradeType {
	private int id;//編號
	private String gradeXue;//學院名稱
	private String gradeName;//班級名稱
	private String gradeDesc;//班級備註
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public GradeType(String gradeXue, String gradeName, String gradeDesc) {
		super();
		this.gradeXue = gradeXue;
		this.gradeName = gradeName;
		this.gradeDesc = gradeDesc;
	}
	public GradeType() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getGradeXue() {
		return gradeXue;
	}
	public void setGradeXue(String gradeXue) {
		this.gradeXue = gradeXue;
	}
	public String getGradeName() {
		return gradeName;
	}
	public void setGradeName(String gradeName) {
		this.gradeName = gradeName;
	}
	public String getGradeDesc() {
		return gradeDesc;
	}
	public void setGradeDesc(String gradeDesc) {
		this.gradeDesc = gradeDesc;
	}
	public GradeType(int id, String gradeXue, String gradeName, String gradeDesc) {
		super();
		this.id = id;
		this.gradeXue = gradeXue;
		this.gradeName = gradeName;
		this.gradeDesc = gradeDesc;
	}
	public String toString() {
		return gradeName;
		
		
	}
	

}
package hiai.model;
/**
 * 學生模型
 * @author zzz
 *
 */
public class Student {
	private int id; //學生編號
	private String stuName;//學生姓名
	private String stuXue;//學生學院
	private String stuIdcarde;//學生學號
	private String stuSex;//學生性別
	private String stuAge;//學生年齡
	private String stuAddress;//學生地址
	private String stuPhone;//學生聯絡方式
	private String stuTypeid;//學生班級
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	/**
	 * @return the stuName
	 */
	public String getStuName() {
		return stuName;
	}
	/**
	 * @param stuName the stuName to set
	 */
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	/**
	 * @return the stuXue
	 */
	public String getStuXue() {
		return stuXue;
	}
	/**
	 * @param stuXue the stuXue to set
	 */
	public void setStuXue(String stuXue) {
		this.stuXue = stuXue;
	}
	/**
	 * @return the stuIdcarde
	 */
	public String getStuIdcarde() {
		return stuIdcarde;
	}
	/**
	 * @param stuIdcarde the stuIdcarde to set
	 */
	public void setStuIdcarde(String stuIdcarde) {
		this.stuIdcarde = stuIdcarde;
	}
	/**
	 * @return the stuSex
	 */
	public String getStuSex() {
		return stuSex;
	}
	/**
	 * @param stuSex the stuSex to set
	 */
	public void setStuSex(String stuSex) {
		this.stuSex = stuSex;
	}
	/**
	 * @return the stuAge
	 */
	public String getStuAge() {
		return stuAge;
	}
	/**
	 * @param stuAge the stuAge to set
	 */
	public void setStuAge(String stuAge) {
		this.stuAge = stuAge;
	}
	/**
	 * @return the stuAddress
	 */
	public String getStuAddress() {
		return stuAddress;
	}
	/**
	 * @param stuAddress the stuAddress to set
	 */
	public void setStuAddress(String stuAddress) {
		this.stuAddress = stuAddress;
	}
	/**
	 * @return the stuPhone
	 */
	public String getStuPhone() {
		return stuPhone;
	}
	/**
	 * @param stuPhone the stuPhone to set
	 */
	public void setStuPhone(String stuPhone) {
		this.stuPhone = stuPhone;
	}
	public Student(String stuName, String stuXue, String stuIdcarde, String stuSex, String stuAge, String stuAddress,
			String stuPhone, String stuTypeid) {
		super();
		this.stuName = stuName;
		this.stuXue = stuXue;
		this.stuIdcarde = stuIdcarde;
		this.stuSex = stuSex;
		this.stuAge = stuAge;
		this.stuAddress = stuAddress;
		this.stuPhone = stuPhone;
		this.stuTypeid = stuTypeid;
	}
	public Student(int id, String stuName, String stuXue, String stuIdcarde, String stuSex, String stuAge,
			String stuAddress, String stuPhone, String stuTypeid) {
		super();
		this.id = id;
		this.stuName = stuName;
		this.stuXue = stuXue;
		this.stuIdcarde = stuIdcarde;
		this.stuSex = stuSex;
		this.stuAge = stuAge;
		this.stuAddress = stuAddress;
		this.stuPhone = stuPhone;
		this.stuTypeid = stuTypeid;
	}
	/**
	 * @return the stuTypeid
	 */
	public String getStuTypeid() {
		return stuTypeid;
	}
	/**
	 * @param stuTypeid the stuTypeid to set
	 */
	public void setStuTypeid(String stuTypeid) {
		this.stuTypeid = stuTypeid;
	}
	
	
	
}
package hiai.model;
/**
 * 使用者的實體,密碼,使用者名稱
 * @author 默默
 * 
 *
 */

public class User {
	private int id; 
	private String userName;
	private String passWord;
	private int is_admin;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}

	public User(String userName, String passWord) {
		super();
		this.userName = userName;
		this.passWord = passWord;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public int getIs_admin() {
		return is_admin;
	}
	public void setIs_admin(int is_admin) {
		this.is_admin = is_admin;
	}
	

}

3.util類

package hiai.util;

import java.sql.Connection;
import java.sql.DriverManager;

/*
 * 連結資料庫
 */
public class DbUTil {
	private String dbUrl="jdbc:mysql://localhost:3306/xueshengguanli ?serverTimezone=GMT%2B8";
	private String dbUserName="root";
	private String dbPassword="XU2638375241";//資料庫密碼
	//private String jdbcName="com.mysql.jdbc.Driver";//驅動名稱
	//資料庫連線
	public Connection getCon() throws Exception{
		//Class.forName(jdbcName);
		Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
		return con;	
	}
	//資料庫的關閉
   public void closeCon(Connection con) throws Exception{
	   if(con!=null) {
		   
		   con.close();   
	   }   
   }
   public static void main(String[] args) {
	   
	   DbUTil dbUtil=new DbUTil();
	   try {
		   dbUtil.getCon();
		   System.out.println("資料庫連線成功");
		
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}
	   
	   
   }
}
package hiai.util;

public class StringUtil {
	
	/*
	 * 判斷是否為空
	 */
	public static boolean isEmpty(String str) {
		if(str==null||"".equals(str.trim())) {
			return true;
			
		}
		else {
		return false;
		}
		
		
	}
	public static boolean isNotEmpty(String str) {
		if(str!=null&&!"".equals(str.trim())) {
			return true;
			
		}
		else {
		return false;
		}
		
		
		
	}
	
	

}

4.view類

package hiai.view;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import hiai.dao.GradeTypeDao;
import hiai.model.GradeType;
import hiai.util.DbUTil;
import hiai.util.StringUtil;

import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;

public class GradeTypeAddFrm extends JInternalFrame {
	private JTextField gradeXue;
	private final JLabel lblNewLabel_1 = new JLabel("New label");
	private JTextField gradeName;
	private JTextArea gradeDesc;
	private DbUTil dbUtil =new DbUTil();
	private GradeTypeDao gradeTypeDao=new GradeTypeDao();

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

	/**
	 * Create the frame.
	 */
	public GradeTypeAddFrm() {
		setIconifiable(true);
		setClosable(true);
		setTitle("\u73ED\u7EA7\u6DFB\u52A0");
		setBounds(100, 100, 738, 422);
		
		JLabel lblNewLabel = new JLabel("\u5B66\u9662\u540D\u79F0\uFF1A");
		lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 16));
		
		gradeXue = new JTextField();
		gradeXue.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("\u73ED\u7EA7\u540D\u79F0\uFF1A");
		lblNewLabel_2.setFont(new Font("宋體", Font.PLAIN, 16));
		
		JLabel lblNewLabel_3 = new JLabel("\u5907\u6CE8\uFF1A");
		lblNewLabel_3.setFont(new Font("宋體", Font.PLAIN, 16));
		
		gradeName = new JTextField();
		gradeName.setColumns(10);
		
		JButton btnNewButton = new JButton("\u6DFB\u52A0");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gradeTypeAddActionPerfromed();
				
			}
		});
		
		JButton btnNewButton_1 = new JButton("\u91CD\u7F6E");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetActionPerformed(e);
				
			}
		});
		
		 gradeDesc = new JTextArea();
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(52)
					.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
					.addGap(99)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_2)
								.addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 95, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
									.addComponent(gradeName, Alignment.LEADING)
									.addComponent(gradeXue, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
									.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
									.addComponent(gradeDesc, GroupLayout.PREFERRED_SIZE, 331, GroupLayout.PREFERRED_SIZE))))
						.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
					.addGap(145))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 0, GroupLayout.PREFERRED_SIZE)
					.addGap(14)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(gradeXue, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(gradeName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(47)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(gradeDesc, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 147, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addGap(73))
		);
		getContentPane().setLayout(groupLayout);
		
		//優化文字域邊框
		gradeDesc.setBorder(new LineBorder(new Color(127,157,185),1,false));
		

	}
	/*
	 * 
	 * 班級新增
	 */
	private void gradeTypeAddActionPerfromed() {
		// TODO Auto-generated method stub
		String gradeXue=this.gradeXue.getText();//得到使用者輸入學院的資訊
		String gradeName=this.gradeName.getText();//班級名稱
		String gradeDesc=this.gradeDesc.getText();//班級備註
		//判斷是否為空
		if(StringUtil.isEmpty(gradeXue)) {
			JOptionPane.showMessageDialog(null, "學院名稱不能為空");
			return ;
			
		}
		if(StringUtil.isEmpty(gradeName)) {
			JOptionPane.showMessageDialog(null, "班級名稱不能為空");
			return;
			
		}
		if(StringUtil.isEmpty(gradeDesc)) {
			JOptionPane.showMessageDialog(null, "班級備註不能為空");
			return;	
		}
		GradeType gradeType=new GradeType( gradeXue,  gradeName,  gradeDesc);
		Connection con= null;
		try {
			con=dbUtil.getCon();
			int   n =gradeTypeDao.add(con, gradeType);
			if(n==1) {
				JOptionPane.showMessageDialog(null, "班級新增成功");
				resetValues();
				
			}
			else {
				JOptionPane.showMessageDialog(null, "班級新增失敗");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);//關閉資料庫
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		
		
		
	}

	/**
	 * 重置事件操作
	 * @param e
	 */

	private void resetActionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		this.resetValues();
		
	}
	private void resetValues() {
		this.gradeXue.setText("");
		this.gradeName.setText("");
		this.gradeDesc.setText("");
		
		
		
		
		
	}
}




package hiai.view;
import java.awt.Color;
/**
 * 維護視窗
 * 
 */
import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JScrollPane;
import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import hiai.dao.GradeTypeDao;
import hiai.model.GradeType;
import hiai.util.DbUTil;
import hiai.util.StringUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GradeTypeManageFrm extends JInternalFrame {
	private JTextField s_gradeNameTxt;
	private JTable gradeTypeTable;
	private DbUTil dbUtil =new DbUTil();
	private GradeTypeDao gradeTypeDao=new GradeTypeDao();
	private JTextField gradeIdTxt;
	private JTextField gradeXueTxt;
	private JTextField gradeNameTxt;
	private JTextField gradeDescTxt;


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

	/**
	 * Create the frame.
	 */
	public GradeTypeManageFrm() {
		setIconifiable(true);
		setClosable(true);
		setTitle("\u73ED\u7EA7\u7EF4\u62A4");
		setBounds(100, 100, 760, 490);
		
		JLabel lblNewLabel = new JLabel("\u73ED\u7EA7\u540D\u79F0");
		lblNewLabel.setFont(new Font("宋體", Font.BOLD, 13));
		
		s_gradeNameTxt = new JTextField();
		s_gradeNameTxt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		s_gradeNameTxt.setColumns(10);
		
		JButton btnNewButton = new JButton("\u67E5\u8BE2");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gradeNameSearchActionPerformed(e);
			}
		});
		btnNewButton.setFont(new Font("宋體", Font.BOLD, 13));
		
		JScrollPane pane = new JScrollPane();
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setToolTipText("\u8868\u5355\u64CD\u4F5C");
		
		gradeTypeTable = new JTable();
		gradeTypeTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				gradeTypeTableMousePressed(e);
			}
		});
		gradeTypeTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u5B66\u9662\u540D\u79F0", "\u73ED\u7EA7\u540D\u79F0", "\u73ED\u7EA7\u5907\u6CE8"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, true, true
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		pane.setViewportView(gradeTypeTable);
		
		JButton btnNewButton_1 = new JButton("\u4FEE\u6539");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gradeTypeUpdateActionEvent(e);
			}
		});
		
		JButton btnNewButton_2 = new JButton("\u5220\u9664");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gradeTypeDeleteActionPerformed(e);
			}
		});
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addContainerGap(138, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
							.addGap(60)
							.addComponent(s_gradeNameTxt, GroupLayout.PREFERRED_SIZE, 193, GroupLayout.PREFERRED_SIZE)
							.addGap(59)
							.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
						.addComponent(panel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
						.addComponent(pane, Alignment.TRAILING))
					.addGap(143))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(25)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
						.addComponent(s_gradeNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE))
					.addGap(19)
					.addComponent(pane, GroupLayout.PREFERRED_SIZE, 121, GroupLayout.PREFERRED_SIZE)
					.addGap(10)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 145, GroupLayout.PREFERRED_SIZE)
					.addGap(56)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1)
						.addComponent(btnNewButton_2))
					.addContainerGap(39, Short.MAX_VALUE))
		);
		
		JLabel lblNewLabel_1 = new JLabel("\u73ED\u7EA7\u7F16\u53F7");
		
		gradeIdTxt = new JTextField();
		gradeIdTxt.setEditable(false);
		gradeIdTxt.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("\u5B66\u9662\u540D\u79F0");
		
		gradeXueTxt = new JTextField();
		gradeXueTxt.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("\u73ED\u7EA7\u540D\u79F0");
		
		gradeNameTxt = new JTextField();
		gradeNameTxt.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("\u73ED\u7EA7\u5907\u6CE8\uFF1A");
		
		gradeDescTxt = new JTextField();
		gradeDescTxt.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
							.addGap(27)
							.addComponent(gradeIdTxt, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(gradeXueTxt, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_4, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE))
							.addGap(18)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(gradeNameTxt, GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
								.addComponent(gradeDescTxt, GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE))))
					.addGap(54))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(lblNewLabel_2)
						.addComponent(gradeXueTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(gradeIdTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(gradeNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_4)
						.addComponent(gradeDescTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(13, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		getContentPane().setLayout(groupLayout);
		//呼叫初始化
		this.fillTable(new GradeType());

		//優化文字域邊框
		gradeDescTxt.setBorder(new LineBorder(new Color(127,157,185),1,false));
		


	}
	/**
	 * 
	 * 刪除操作
	 * @param e
	 */
    private void gradeTypeDeleteActionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
    	String id=gradeIdTxt.getText();
        if(StringUtil.isEmpty(id)) {
			
			JOptionPane.showMessageDialog(null, "請選擇要刪除的記錄");
			return;
		}
		int n=JOptionPane.showConfirmDialog(null, "確定要刪除該條件記錄嗎?");
		if(n==0) {
			Connection con=null;
			try {
				con=dbUtil.getCon();
				//
				//
				int deleteNum=gradeTypeDao.delete(con, id);
				if(deleteNum==1) {
					JOptionPane.showMessageDialog(null, "刪除成功");
					resetValue();
					fillTable(new GradeType());
				}
				else {
					
					JOptionPane.showMessageDialog(null, "刪除失敗");
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}finally {
				try {
					dbUtil.closeCon(con);//關閉資料庫
				}
				catch (Exception e1) {
					// TODO: handle exception
					e1.printStackTrace();
				}
			}
			
		}
	}

	/*
     * 
     * 事件修改生成
     */
	private void gradeTypeUpdateActionEvent(ActionEvent evt) {
		// TODO Auto-generated method stub
		String id=gradeIdTxt.getText();
		String gradeXue=gradeXueTxt.getText();
		String gradeName=gradeNameTxt.getText();
		String gradeDesc=gradeDescTxt.getText();
		if(StringUtil.isEmpty(id)) {
			
			JOptionPane.showMessageDialog(null, "請選擇要修改的記錄");
			return;
		}
        if(StringUtil.isEmpty(gradeXue)) {
			
			JOptionPane.showMessageDialog(null, "學院名稱不能為空!");
			return;
		}
        if(StringUtil.isEmpty(gradeName)) {
			
			JOptionPane.showMessageDialog(null, "班級名稱不能為空!");
			return;
		}
         if(StringUtil.isEmpty(gradeDesc)) {
	
	        JOptionPane.showMessageDialog(null, "班級備註不能為空!");
	        return;
        }
         GradeType  gradeType=new GradeType( Integer.parseInt(id),  gradeXue,  gradeName,  gradeDesc);
         Connection con=null;
         try {
			con=dbUtil.getCon();
			int modifyNum=gradeTypeDao.update(con, gradeType);
			if(modifyNum==1) {
				JOptionPane.showMessageDialog(null, "修改成功");
				resetValue();
				fillTable(new GradeType());
			}
			else {
				JOptionPane.showMessageDialog(null, "修改失敗");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         finally {
 			try {
 				dbUtil.closeCon(con);//關閉資料庫
 			}
 			catch (Exception e) {
 				// TODO: handle exception
 				e.printStackTrace();
 			}
 		}
	}
	/*
	 * 重置操作
	 */
	private void resetValue() {
		// TODO Auto-generated method stub
		this.gradeIdTxt.setText("");
		this.gradeXueTxt.setText("");
		this.gradeNameTxt.setText("");
		this.gradeDescTxt.setText("");
		
	}

	/**
	 * 表格的單擊事件
	 * @param e
	 */	private void gradeTypeTableMousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		int row= gradeTypeTable.getSelectedRow();
		gradeIdTxt.setText((String)gradeTypeTable.getValueAt(row, 0));
		gradeXueTxt.setText((String)gradeTypeTable.getValueAt(row, 1));
		gradeNameTxt.setText((String)gradeTypeTable.getValueAt(row, 2));
		gradeDescTxt.setText((String)gradeTypeTable.getValueAt(row, 3));
	}

	/*
	 * 
	 * 班級查詢
	 */
	private void gradeNameSearchActionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String s_gradeName= this.s_gradeNameTxt.getText();
		GradeType gradeType=new GradeType();
		gradeType.setGradeName(s_gradeName);
		this.fillTable(gradeType);
		
	}

	/**
	 * 初始化表格
	 * 
	 */
	private void fillTable(GradeType gradeType) {
		DefaultTableModel dtm= (DefaultTableModel)gradeTypeTable.getModel();
		dtm.setRowCount(0);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs= gradeTypeDao.list(con, gradeType);
			while(rs.next()) {
				Vector v=new Vector();
			    v.add(rs.getString("id"));
				v.add(rs.getString("gradeXue"));
				v.add(rs.getString("gradeName"));
				v.add(rs.getString("gradedesc"));
				dtm.addRow(v);
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		finally {
			try {
				dbUtil.closeCon(con);//關閉資料庫
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		
	}
}



 package hiai.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import javax.swing.LayoutStyle.ComponentPlacement;

public class hiailnterFrm extends JInternalFrame {

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

	/**
	 * Create the frame.
	 */
	public hiailnterFrm() {
		setForeground(Color.WHITE);
		setIconifiable(true);
		setClosable(true);
		setTitle("\u4F5C\u8005\u8BF4");
		setBounds(100, 100, 686, 441);
		
		JLabel lblNewLabel = new JLabel("\u4F5C\u8005\u8BF4");
		lblNewLabel.setFont(new Font("宋體", Font.BOLD, 18));
		
		JLabel lblNewLabel_1 = new JLabel("\u672C\u7A0B\u5E8F\u662F\u7531\u4F5C\u80058\u5C11\u4E00\u624B\u521B\u529E\u5982\u9700\u4F7F\u7528\u8BF7\u8054\u7CFB");
		lblNewLabel_1.setFont(new Font("宋體", Font.PLAIN, 16));
		
		JLabel lblNewLabel_2 = new JLabel("\u4E0D\u5F97\u79C1\u81EA\u8F6C\u8F7D");
		lblNewLabel_2.setFont(new Font("宋體", Font.PLAIN, 16));
		
		JLabel lblNewLabel_3 = new JLabel("\u8054\u7CFB\u65B9\u5F0F \uFF1A");
		lblNewLabel_3.setFont(new Font("宋體", Font.PLAIN, 16));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(242)
							.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(111)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 435, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 323, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(128, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(33)
					.addComponent(lblNewLabel)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblNewLabel_2)
					.addGap(18)
					.addComponent(lblNewLabel_3)
					.addContainerGap(240, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);

	}
}
package hiai.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import hiai.dao.UserDao;
import hiai.model.User;
import hiai.util.DbUTil;
import hiai.util.StringUtil;

import java.awt.Toolkit;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;

public class LogOnFrm extends JFrame {

	private JPanel contentPane;
	private JTextField userName;
	private JPasswordField passWord;
	private DbUTil dbUtil=new DbUTil();
	private UserDao userDao=new UserDao();
	

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

	/**
	 * Create the frame.
	 */
	public LogOnFrm() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(LogOnFrm.class.getResource("/images/3785578738346ce49929ad358ab12747.jpeg")));
		setTitle("\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 554, 403);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JButton btnNewButton = new JButton("\u767B\u5F55");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loginActionPerformed(e);
			}
		});
		btnNewButton.setFont(new Font("宋體", Font.PLAIN, 15));
		
		JButton btnNewButton_1 = new JButton("\u91CD\u7F6E");
		btnNewButton_1.setFont(new Font("宋體", Font.PLAIN, 15));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				resetActionPerformed(e);
			}
		});
		
		userName = new JTextField();
		userName.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D");
		lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 18));
		
		JLabel lblNewLabel_1 = new JLabel("\u5BC6\u7801");
		lblNewLabel_1.setFont(new Font("宋體", Font.PLAIN, 18));
		
		JLabel lblNewLabel_2 = new JLabel("\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF");
		lblNewLabel_2.setFont(new Font("宋體", Font.BOLD, 26));
		
		passWord = new JPasswordField();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(97)
							.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
							.addGap(117)
							.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(103)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel)
								.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE))
							.addGap(59)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(passWord)
								.addComponent(userName, GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(172)
							.addComponent(lblNewLabel_2)))
					.addContainerGap(109, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
					.addGap(31)
					.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
					.addGap(39)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE)
						.addComponent(userName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(54)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(passWord, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addGap(52))
		);
		contentPane.setLayout(gl_contentPane);
		//設定視窗居中顯示
		this.setLocationRelativeTo(null);
	}
	protected void loginActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		String userName=this.userName.getText();
		String passWord=new String(this.passWord.getPassword());
		
		if(StringUtil.isEmpty(userName)) {
			JOptionPane.showMessageDialog(null, "使用者名稱不能為空");
			return;
			
			
		}
		if(StringUtil.isEmpty(passWord)) {
			JOptionPane.showMessageDialog(null, "密碼不能為空");
			return;	
		}
		User user=new User(userName,passWord);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			User currenUser=userDao.login(con, user);
			if(currenUser!=null) {

				dispose();//銷燬當前視窗
				new MainFrm().setVisible(true);//建立新的視窗
			}
			else {
				JOptionPane.showMessageDialog(null, "使用者名稱或密碼錯誤");
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);//關閉資料庫
			}
			catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		
	}

	private void resetActionPerformed(ActionEvent evt) {
		// TODO Auto-generated method stub
		this.userName.setText("");
		this.passWord.setText("");
	}

}





package hiai.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.mysql.cj.xdevapi.Table;

import java.awt.Toolkit;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.CardLayout;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import java.awt.FlowLayout;
/**
 * 系統主介面
 * @author zzz
 *
 */
public class MainFrm extends JFrame {
	private JDesktopPane table=null;

	private JPanel contentPane;

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

	/**
	 * Create the frame.
	 */
	public MainFrm() {
		setAutoRequestFocus(false);
		setTitle("\u5B66\u751F\u7BA1\u7406\u7CFB\u7EDF\u4E3B\u754C\u9762");
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrm.class.getResource("/images/3785578738346ce49929ad358ab12747.jpeg")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 846, 701);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnNewMenu_3 = new JMenu("\u7CFB\u7EDF\u8BBE\u7F6E");
		menuBar.add(mnNewMenu_3);
		
		JMenuItem mntmNewMenuItem_5 = new JMenuItem("\u9000\u51FA\u7A0B\u5E8F");
		mntmNewMenuItem_5.addActionListener(new ActionListener() {
			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {
				
				int a=JOptionPane.showConfirmDialog(null, "是否退出程式");
				if(a==0) {
					
					
				System.exit(0);;//銷燬程式
				}
				
			}


		});
		mnNewMenu_3.add(mntmNewMenuItem_5);
		
		JMenuBar menuBar_1 = new JMenuBar();
		mnNewMenu_3.add(menuBar_1);
		
		JMenu mnNewMenu = new JMenu("\u73ED\u7EA7\u7BA1\u7406");
		menuBar.add(mnNewMenu);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("\u73ED\u7EA7\u6DFB\u52A0");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				GradeTypeAddFrm gradeTypeAddFrm=new GradeTypeAddFrm();
				gradeTypeAddFrm.setVisible(true);
				table.add(gradeTypeAddFrm);
				
			}
			
			
		});
		mnNewMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("\u73ED\u7EA7\u7EF4\u62A4");
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
     			GradeTypeManageFrm gradeTypeManageFrm=new GradeTypeManageFrm();
				gradeTypeManageFrm.setVisible(true);
				table.add(gradeTypeManageFrm);
				
			}
		});
		mnNewMenu.add(mntmNewMenuItem_1);
		
		JMenu mnNewMenu_1 = new JMenu("\u5B66\u751F\u7BA1\u7406");
		menuBar.add(mnNewMenu_1);
		
		JMenuItem mntmNewMenuItem_2 = 
				new JMenuItem("\u5B66\u751F\u6DFB\u52A0");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StudentAddFrm studentAddFrm =new StudentAddFrm();
				studentAddFrm.setVisible(true);
				table.add(studentAddFrm);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_2);
		
		JMenuItem mntmNewMenuItem_3 = new JMenuItem("\u5B66\u751F\u7EF4\u62A4");
		mntmNewMenuItem_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StudentManageFrm studentAddFrm =new StudentManageFrm();
				studentAddFrm.setVisible(true);
				table.add(studentAddFrm);
				
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_3);
		
		JMenu mnNewMenu_2 = new JMenu("\u5173\u4E8E\u6211\u4EEC");
		menuBar.add(mnNewMenu_2);
		
		JMenuItem mntmNewMenuItem_4 = new JMenuItem("\u4F5C\u8005\u8BF4");
		mntmNewMenuItem_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				hiailnterFrm hiai=new hiailnterFrm();
				hiai.setVisible(true);
				table.add(hiai);
				
			}
		});
		mnNewMenu_2.add(mntmNewMenuItem_4);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		 contentPane.setLayout(new CardLayout(0, 0));
		
		 table = new JDesktopPane();
		table.setBackground(Color.GRAY);
		contentPane.add(table, "name_1386392240138500");
		table.setLayout(new BoxLayout(table, BoxLayout.X_AXIS));
		
		//設定視窗預設最大化
		//this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		
	}
}
package hiai.view;
/*
 * 學生新增視窗
 */
import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import hiai.dao.GradeTypeDao;
import hiai.dao.StudentDao;
import hiai.model.GradeType;
import hiai.model.Student;
import hiai.util.DbUTil;
import hiai.util.StringUtil;

import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

public class StudentAddFrm extends JInternalFrame {
	private JTextField stuNameTxt;
	private JTextField stuXueTxt;
	private JTextField stuIdcardTxt;
	private JTextField stuAgeTxt;
	private JTextField stuPhoneTxt;
	private JTextField stuAddressTxt;
	private DbUTil dbUtil =new DbUTil();
	private GradeTypeDao gradeTypeDao=new GradeTypeDao();
	private StudentDao studentDao=new StudentDao();
	private JRadioButton manJcb;
	private JRadioButton femaleJcb; 
	private JTextField stuTypeidTxt;


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

	/**
	 * Create the frame.
	 */
	public StudentAddFrm() {
		setClosable(true);
		setIconifiable(true);
		setTitle("\u5B66\u751F\u6DFB\u52A0");
		setBounds(100, 100, 750, 476);
		
		JLabel lblNewLabel = new JLabel("\u5B66\u751F\u59D3\u540D");
		lblNewLabel.setFont(new Font("宋體", Font.PLAIN, 15));
		
		stuNameTxt = new JTextField();
		stuNameTxt.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("\u5B66\u751F\u5B66\u9662");
		lblNewLabel_1.setFont(new Font("宋體", Font.PLAIN, 15));
		
		stuXueTxt = new JTextField();
		stuXueTxt.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("\u5B66\u751F\u73ED\u7EA7");
		lblNewLabel_2.setFont(new Font("宋體", Font.PLAIN, 15));
		
		JLabel lblNewLabel_3 = new JLabel("\u5B66\u751F\u5B66\u53F7");
		lblNewLabel_3.setFont(new Font("宋體", Font.PLAIN, 15));
		
		JLabel lblNewLabel_4 = new JLabel("\u5B66\u751F\u6027\u522B");
		lblNewLabel_4.setFont(new Font("宋體", Font.PLAIN, 15));
		
	    manJcb = new JRadioButton("\u7537");
		
		femaleJcb = new JRadioButton("\u5973");
		femaleJcb.setSelected(true);
		
		stuIdcardTxt = new JTextField();
		stuIdcardTxt.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("\u5B66\u751F\u5E74\u9F84");
		lblNewLabel_5.setFont(new Font("宋體", Font.PLAIN, 15));
		
		stuAgeTxt = new JTextField();
		stuAgeTxt.setColumns(10);
		
		JLabel lblNewLabel_6 = new JLabel("\u8054\u7CFB\u65B9\u5F0F");
		lblNewLabel_6.setFont(new Font("宋體", Font.PLAIN, 15));
		
		stuPhoneTxt = new JTextField();
		stuPhoneTxt.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel("\u5BB6\u5EAD\u4F4F\u5740");
		lblNewLabel_7.setFont(new Font("宋體", Font.PLAIN, 15));
		
		stuAddressTxt = new JTextField();
		stuAddressTxt.setColumns(10);
		
		JButton btnNewButton = new JButton("\u6DFB\u52A0");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				studentAddActionPerformed(e);
				
			}
		});
		
		JButton btnNewButton_1 = new JButton("\u91CD\u7F6E");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				studentReseActionPerFormed(e);
			}
		});
		
		stuTypeidTxt = new JTextField();
		stuTypeidTxt.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(26)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(lblNewLabel_7, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(lblNewLabel_6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(lblNewLabel_4, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(lblNewLabel_2, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
						.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED, 336, Short.MAX_VALUE)
							.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
										.addComponent(stuPhoneTxt)
										.addGroup(groupLayout.createSequentialGroup()
											.addGap(10)
											.addComponent(manJcb, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(femaleJcb, GroupLayout.PREFERRED_SIZE, 40, GroupLayout.PREFERRED_SIZE)))
									.addGap(136))
								.addGroup(groupLayout.createSequentialGroup()
									.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
										.addComponent(stuTypeidTxt, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
										.addComponent(stuNameTxt, GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE))
									.addGap(35)))
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_5)
								.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_3))
							.addGap(29)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
									.addComponent(stuIdcardTxt, 168, 168, 168)
									.addComponent(stuXueTxt, GroupLayout.PREFERRED_SIZE, 168, GroupLayout.PREFERRED_SIZE))
								.addComponent(stuAgeTxt, GroupLayout.DEFAULT_SIZE, 175, Short.MAX_VALUE)))
						.addComponent(stuAddressTxt, GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE))
					.addGap(107))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(28)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblNewLabel_3)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(stuXueTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_1)
								.addComponent(lblNewLabel)
								.addComponent(stuNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(13)
							.addComponent(stuIdcardTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(11)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(stuTypeidTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_4)
						.addComponent(manJcb)
						.addComponent(femaleJcb)
						.addComponent(stuAgeTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_5))
					.addGap(26)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_6)
						.addComponent(stuPhoneTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_7)
						.addComponent(stuAddressTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(78)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1)
						.addComponent(btnNewButton))
					.addContainerGap(93, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	 /**
	 * 呼叫初始化班級下拉框
	 */
		
		this.fillGradeType();

	}
	/***
	 * 學生新增操作
	 */
	
	private void studentAddActionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String stuName=this.stuNameTxt.getText();
		String stuXue=this.stuXueTxt.getText();
		String stuIdcard=this.stuIdcardTxt.getText();
		String stuAge=this.stuAgeTxt.getText();
		String stuAddress=this.stuAddressTxt.getText();
		String stuPhone=this.stuPhoneTxt.getText();
		String stuTypeid=this.stuTypeidTxt.getText();
		
		//判斷不為空
		if(StringUtil.isEmpty(stuName)) {
			JOptionPane.showMessageDialog(null, "學生姓名不能為空");
			return;	
		}
		if(StringUtil.isEmpty(stuXue)) {
			JOptionPane.showMessageDialog(null, "學生學院不能為空");
			return;	
		}
		if(StringUtil.isEmpty(stuIdcard)) {
			JOptionPane.showMessageDialog(null, "學生學號不能為空");
			return;	
		}
		if(StringUtil.isEmpty(stuAge)) {
			JOptionPane.showMessageDialog(null, "學生年齡不能為空");
			return;	
		}
		if(StringUtil.isEmpty(stuAddress)) {
			JOptionPane.showMessageDialog(null, "家庭住址不能為空");
			return;	
		}
		if(StringUtil.isEmpty(stuPhone)) {
			JOptionPane.showMessageDialog(null, "聯絡方式不能為空");
			return;	
		}
		if(StringUtil.isEmpty(stuTypeid)) {
			JOptionPane.showMessageDialog(null, "學生班級不能為空");
			return;	
		}
		
		//判斷使用者輸入的性別
		String stuSex=null;
		if(manJcb.isSelected()) {
			stuSex="男";	
		}
		else if(femaleJcb.isSelected()) {
			stuSex="女";
		}
		Student student =new Student( stuName,  stuXue,  stuIdcard,stuSex,  stuAge,  stuAddress,
			 stuPhone,  stuTypeid);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			int addNum=studentDao.add(con, student);
			if(addNum==1) {
				JOptionPane.showMessageDialog(null, "學生新增成功");	
			}else {
				JOptionPane.showMessageDialog(null, "學生新增失敗");
				
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
	}

	/***
	 * 重置操作
	 * @param e
	 */
	private void studentReseActionPerFormed(ActionEvent e) {
		// TODO Auto-generated method stub
		this.resetValue();
		
	}
	public void resetValue() {
		this.stuNameTxt.setText("");
		this.stuXueTxt.setText("");
		this.stuAddressTxt.setText("");
		this.stuIdcardTxt.setText("");
		this.stuAgeTxt.setText("");
		this.stuPhoneTxt.setText("");
		this.stuTypeidTxt.setText("");
		
		
	}
	/**
	 * 
	 * 初始化班級下拉框
	 */
	private void fillGradeType() {
		Connection con=null;
		GradeType gradeType=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=gradeTypeDao.list(con, new GradeType());
			while(rs.next()) {
				gradeType=new GradeType();
				gradeType.setId(rs.getInt("id"));
				gradeType.setGradeName(rs.getString("gradeName"));
				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
			
	}
}




package hiai.view;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import hiai.dao.GradeTypeDao;
import hiai.dao.StudentDao;
import hiai.model.GradeType;
import hiai.model.Student;
import hiai.util.DbUTil;
import hiai.util.StringUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class StudentManageFrm extends JInternalFrame {
	private JTextField s_studentcardTxt;
	private JTable studentTypeTable;
	private DbUTil dbUtil =new DbUTil();
	private GradeTypeDao gradeTypeDao=new GradeTypeDao();
	private StudentDao studentDao=new StudentDao();
	private JTextField studentIdTxt;
	private JTextField studentNameTxt;
	private JTextField studentXueTxt;
	private JTextField studentIdcardTxt;
	private JTextField studentSexTxt;
	private JTextField studentAgeTxt;
	private JTextField studentAddressTxt;
	private JTextField studentPhoneTxt;
	private JTextField studentTypeidTxt;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					StudentManageFrm frame = new StudentManageFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public StudentManageFrm() {
		setIconifiable(true);
		setClosable(true);
		setTitle("\u5B66\u751F\u7EF4\u62A4");
		setBounds(100, 100, 752, 560);
		
		JLabel lblNewLabel = new JLabel("\u5B66\u751F\u5B66\u53F7");
		lblNewLabel.setFont(new Font("微軟雅黑", Font.PLAIN, 15));
		
		s_studentcardTxt = new JTextField();
		s_studentcardTxt.setColumns(10);
		
		JButton btnNewButton = new JButton("\u67E5\u8BE2");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				studentcardSearchActionPerformed(e);
			}
		});
		btnNewButton.setFont(new Font("宋體", Font.PLAIN, 15));
		
		JScrollPane scrollPane = new JScrollPane();
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JButton btnNewButton_1 = new JButton("\u4FEE\u6539");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				studentxiugai();
				
				
				
				
			}
		});
		
		JButton btnNewButton_2 = new JButton("\u5220\u9664");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				studentdelete(e);
			}
		});
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(62)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(Alignment.LEADING, groupLayout.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 629, Short.MAX_VALUE)
							.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
								.addGap(32)
								.addComponent(s_studentcardTxt, GroupLayout.PREFERRED_SIZE, 403, GroupLayout.PREFERRED_SIZE)
								.addGap(18)
								.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap(49, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(105)
					.addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 306, Short.MAX_VALUE)
					.addComponent(btnNewButton_2, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
					.addGap(135))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(19)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(s_studentcardTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton))
					.addGap(32)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_1)
						.addComponent(btnNewButton_2))
					.addContainerGap(40, Short.MAX_VALUE))
		);
		
		JLabel lblNewLabel_1 = new JLabel("\u5B66\u751F\u7F16\u53F7");
		
		studentIdTxt = new JTextField();
		studentIdTxt.setEditable(false);
		studentIdTxt.setColumns(10);
		
		studentNameTxt = new JTextField();
		studentNameTxt.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("\u5B66\u751F\u59D3\u540D");
		
		JLabel lblNewLabel_3 = new JLabel("\u5B66\u751F\u5B66\u9662");
		
		studentXueTxt = new JTextField();
		studentXueTxt.setColumns(10);
		
		JLabel lblNewLabel_4 = new JLabel("\u5B66\u751F\u5B66\u53F7");
		
		studentIdcardTxt = new JTextField();
		studentIdcardTxt.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("\u5B66\u751F\u6027\u522B");
		
		studentSexTxt = new JTextField();
		studentSexTxt.setColumns(10);
		
		JLabel lblNewLabel_6 = new JLabel("\u5B66\u751F\u5E74\u9F84");
		
		studentAgeTxt = new JTextField();
		studentAgeTxt.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel("\u5B66\u751F\u5730\u5740");
		
		studentAddressTxt = new JTextField();
		studentAddressTxt.setColumns(10);
		
		JLabel lblNewLabel_8 = new JLabel("\u8054\u7CFB\u65B9\u5F0F");
		
		studentPhoneTxt = new JTextField();
		studentPhoneTxt.setColumns(10);
		
		JLabel lblNewLabel_9 = new JLabel("\u5B66\u751F\u73ED\u7EA7");
		
		studentTypeidTxt = new JTextField();
		studentTypeidTxt.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(studentIdTxt, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_4, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(studentIdcardTxt)))
							.addGap(18)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(studentNameTxt, GroupLayout.PREFERRED_SIZE, 91, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_5, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(studentSexTxt)))
							.addGap(18)
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(studentXueTxt, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_6, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(studentAgeTxt))))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_7, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(studentAddressTxt))
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblNewLabel_8, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(studentPhoneTxt, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
							.addGap(44)
							.addComponent(lblNewLabel_9, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(studentTypeidTxt)))
					.addContainerGap(123, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(studentIdTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(studentNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2)
						.addComponent(lblNewLabel_3)
						.addComponent(studentXueTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(28)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_4)
						.addComponent(studentIdcardTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_5)
						.addComponent(studentSexTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_6)
						.addComponent(studentAgeTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_7)
						.addComponent(studentAddressTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_8)
						.addComponent(studentPhoneTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_9)
						.addComponent(studentTypeidTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(26, Short.MAX_VALUE))
		);
		panel.setLayout(gl_panel);
		
		studentTypeTable = new JTable();
		studentTypeTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				
				studentTableMousePressed();
				
				
			}
		});
		studentTypeTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u5B66\u751F\u5B66\u9662", "\u5B66\u751F\u5B66\u53F7", "\u5B66\u751F\u6027\u522B", "\u5B66\u751F\u5E74\u9F84", "\u5B66\u751F\u5730\u5740", "\u5B66\u751F\u8054\u7CFB\u65B9\u5F0F", "\u5B66\u751F\u73ED\u7EA7"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, true, false, false, false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		studentTypeTable.getColumnModel().getColumn(7).setPreferredWidth(100);
		scrollPane.setViewportView(studentTypeTable);
		getContentPane().setLayout(groupLayout);
		this.fillTable(new Student());
		//優化文字域邊框
				studentAddressTxt.setBorder(new LineBorder(new Color(127,157,185),1,false));
	}
	private void studentdelete(ActionEvent evt) {
		// TODO Auto-generated method stub
		String id=studentIdTxt.getText();
		if(StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "請選擇您要刪除的記錄");
			return;
		}
		int n=JOptionPane.showConfirmDialog(null, "確定要刪除這條記錄嗎");
		if(n==0) {
			Connection con=null;
			try {
				con=dbUtil.getCon();
				int deleteNum=studentDao.delete(con, id);
				if(deleteNum==1) {
					JOptionPane.showMessageDialog(null, "刪除成功");
					resetValue();
					fillTable(new Student());
				}
				else {
					
					JOptionPane.showMessageDialog(null, "刪除失敗");
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}finally {
				try {
					dbUtil.closeCon(con);//關閉資料庫
				}
				catch (Exception e1) {
					// TODO: handle exception
					e1.printStackTrace();
				}
			}
			
		}
	}

	/**
	 * 修改操作
	 */
	private void studentxiugai() {
		// TODO Auto-generated method stub
		String id=studentIdTxt.getText();
		String s_Name=studentNameTxt.getText();
		String s_Xue=studentXueTxt.getText();
		String s_Idcard=studentIdcardTxt.getText();
		String s_Sex=studentSexTxt.getText();
		String s_Age=studentAgeTxt.getText();
		String s_Address=studentAddressTxt.getText();
		String s_Phone=studentPhoneTxt.getText();
		String s_Typeid=studentTypeidTxt.getText();
		if(StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "請選擇您要修改的記錄");
			return;
		}
		if(StringUtil.isEmpty(s_Name)) {
			JOptionPane.showMessageDialog(null, "學生名稱不能為空");
			return;
		}
		if(StringUtil.isEmpty( s_Xue)) {
			JOptionPane.showMessageDialog(null, "學生學院不能為空");
			return;
		}
		if(StringUtil.isEmpty(s_Idcard)) {
			JOptionPane.showMessageDialog(null, "學生學號不能為空");
			return;
		}
		if(StringUtil.isEmpty(s_Sex)) {
			JOptionPane.showMessageDialog(null, "學生性別不能為空");
			return;
		}
		if(StringUtil.isEmpty(s_Age)) {
			JOptionPane.showMessageDialog(null, "學生年齡不能為空");
			return;
		}
		if(StringUtil.isEmpty(s_Address)) {
			JOptionPane.showMessageDialog(null, "家庭住址不能為空");
			return;
		}
		if(StringUtil.isEmpty(s_Phone)) {
			JOptionPane.showMessageDialog(null, "聯絡方式不能為空");
			return;
		}
		if(StringUtil.isEmpty (s_Typeid)) {
			JOptionPane.showMessageDialog(null, "學生班級不能為空");
			return;
		}
		
		Student student=new Student( Integer.parseInt(id), s_Name ,  s_Xue,  s_Idcard,  s_Sex,  s_Age,
				s_Address, s_Phone, s_Typeid);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			int mod=studentDao.update(con, student);
			if(mod==1) {
				JOptionPane.showMessageDialog(null, "修改成功");
				resetValue();
			    fillTable(new Student());   
			}else {
				
				JOptionPane.showMessageDialog(null, "修改失敗");
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
		
		
		
		/**
		 * 重置操作
		 */
	

	private void resetValue() {
		// TODO Auto-generated method stub
		this.studentIdTxt.setText("");
		this.studentNameTxt.setText("");
		this.studentXueTxt.setText("");
		this.s_studentcardTxt.setText("");
		this.studentSexTxt.setText("");
		this.studentAgeTxt.setText("");
		this.studentAddressTxt.setText("");
		this.studentPhoneTxt.setText("");
		this.studentTypeidTxt.setText("");
		
	}

	/**
	 * 表格單擊事件
	 */
	
	
	private void studentTableMousePressed() {
		// TODO Auto-generated method stub
		
		int row= studentTypeTable.getSelectedRow();
		studentIdTxt.setText((String)studentTypeTable.getValueAt(row, 0));
		studentNameTxt.setText((String)studentTypeTable.getValueAt(row, 1));
		studentXueTxt.setText((String)studentTypeTable.getValueAt(row, 2));
		studentIdcardTxt.setText((String)studentTypeTable.getValueAt(row, 3));
		studentSexTxt.setText((String)studentTypeTable.getValueAt(row, 4));
		studentAgeTxt.setText((String)studentTypeTable.getValueAt(row, 5));
		studentAddressTxt.setText((String)studentTypeTable.getValueAt(row, 6));
		studentPhoneTxt.setText((String)studentTypeTable.getValueAt(row, 7));
		studentTypeidTxt.setText((String)studentTypeTable.getValueAt(row, 8));
		
	}

	
	/**
	 * 查詢操作
	 * @param e
	 */
	private void studentcardSearchActionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String s_studentcard= this.s_studentcardTxt.getText();
		Student student=new Student();
		student.setStuIdcarde(s_studentcard);
		this.fillTable(student);
		
		
	}

	/**
	 * 初始化表格
	 */
	private void fillTable(Student student) {
		DefaultTableModel dtm=(DefaultTableModel)studentTypeTable.getModel();
		dtm.setRowCount(0);
		Connection con=null;
		
		try {
			con=dbUtil.getCon();
			ResultSet rs=studentDao.list(con, student);
			//String gradeType=rs.getString("stuTypeid");
			//System.out.println(gradeType);
			while(rs.next()) {
			Vector v=new Vector();
			v.add(rs.getString("id"));
			v.add(rs.getString("stuName"));
			v.add(rs.getString("stuXue"));
			v.add(rs.getString("stuIdcard"));
			v.add(rs.getString("stuSex"));
			v.add(rs.getString("stuAge"));
			v.add(rs.getString("stuAddress"));
			v.add(rs.getString("stuPhone"));
			v.add(rs.getString("stuTypeid"));
			dtm.addRow(v);
			
			}
			
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}