基于java和mysql的swing+mysql学生管理系统java基础gui(java+gui+mysql)

私信源码获取及调试交流

私信源码获取及调试交流

运行环境

Java≥8、MySQL≥5.7

开发工具

eclipse/idea/myeclipse/sts等均可配置运行

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

功能说明

基于java的Swing+MySQL学生管理系统java基础gui(java+gui+mysql)

功能介绍:

学生信息管理,姓名,编号,性别,成绩,学号住址

		this.address = address;
	}

	public Student(String name, String sn, Integer calssId) {
		super();
		this.name = name;
		this.sn = sn;
		this.classId = calssId;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	public String getSex() {
		return sex;
	}

public class SchoolClassManageInterFrm extends JInternalFrame {
	private JTable schoolClassTable;
	private JTextArea classDescTxt ;
	
	private DbUtil dbUtil=new DbUtil();
	private SchoolClassDao schoolClassDao=new SchoolClassDao();
	private StudentDao studentDao=new StudentDao();
	private JTextField s_classNameTxt;
	private JTextField idTxt;
	private JTextField classNameTxt;

	/**
	 * Create the frame.
	 */
	public SchoolClassManageInterFrm() {
		setClosable(true);
		setIconifiable(true);
		
		JButton button_2 = new JButton("\u5220\u9664");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				studentDeleteActionPerformed(evt);
			}
		});
		button_2.setIcon(new ImageIcon(StudentManageInterFrm.class.getResource("/images/delete.png")));
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(19)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(button_1)
							.addGap(18)
							.addComponent(button_2)
							.addGap(386))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(label_6)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(addressTxt))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
										.addGroup(gl_panel_1.createSequentialGroup()
											.addComponent(label_4)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(deptTxt))
										.addGroup(gl_panel_1.createSequentialGroup()
											.addComponent(lblNewLabel)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)))
									.addGap(26)
									.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
										.addGroup(gl_panel_1.createSequentialGroup()
											.addComponent(lblNewLabel_1)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(nameTxt, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE))
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(LogOnFrm.class.getResource("/images/reset.png")));
		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(111)
							.addComponent(lblNewLabel))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(101)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNewLabel_1)
								.addComponent(lblNewLabel_2)
								.addComponent(btnNewButton))
							.addGap(32)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(btnNewButton_1)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
									.addComponent(passwordTxt)
									.addComponent(userNameTxt, GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)))))
					.addContainerGap(111, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(30)
					.addComponent(lblNewLabel)
					.addGap(26)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addComponent(lblNewLabel_1)
							.addGap(29)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNewLabel_2)
								.addComponent(passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(36)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton)
						.addComponent(btnNewButton_1))
					.addContainerGap(60, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
		
		// 设置JFrame居中显示
		this.setLocationRelativeTo(null);
	}
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	public Integer getClassId() {
		return classId;
	}

	public void setClassId(Integer classId) {
		this.classId = classId;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getAddress() {
		return address;
	 */
	private void fillTable(Student book){
		DefaultTableModel dtm=(DefaultTableModel) studentTable.getModel();
		dtm.setRowCount(0); // 设置成0行
		Connection con=null;
		try{
			con=dbUtil.getCon();
			ResultSet rs=studentDao.list(con, book);
			while(rs.next()){
				Vector v=new Vector();
				v.add(rs.getString("id"));
				v.add(rs.getString("name"));
				v.add(rs.getString("sn"));
				v.add(rs.getString("sex"));
				v.add(rs.getString("dept"));
				v.add(rs.getString("address"));
				v.add(rs.getString("className"));
				dtm.addRow(v);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

		setBounds(100, 100, 450, 467);
		
		JLabel label = new JLabel("学生姓名");
		
		studentTxt = new JTextField();
		studentTxt.setColumns(10);
		
		JLabel label_1 = new JLabel("学生学号");
		
		snTxt = new JTextField();
		snTxt.setColumns(10);
		
		JLabel label_2 = new JLabel("学生性别");
		
		manJrb = new JRadioButton("男");
		buttonGroup.add(manJrb);
		manJrb.setSelected(true);
		
		femaleJrb = new JRadioButton("女");
		buttonGroup.add(femaleJrb);
		
		JLabel label_3 = new JLabel("成绩分数");
		
		deptTxt = new JTextField();
		deptTxt.setColumns(10);
		
		JLabel label_4 = new JLabel("家庭住址");
		
		addressTxt = new JTextArea();
		
		JLabel label_5 = new JLabel("所在班级");
		
	    schoolClassJcb = new JComboBox();
	    gradeTxt = new JTextField();
	    gradeTxt.setColumns(10);
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookAddActionPerformed(e);
			}
        try {
            dbUtil.getCon();
            System.out.println("数据库连接成功!");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
    }

}

public class ArtisanInterFrm extends JInternalFrame {

	/**
	 * Create the frame.
	 */
	public ArtisanInterFrm() {
		//getContentPane().setBackground(Color.RED);
		setIconifiable(true);
		setClosable(true);
		setTitle("关于系统");
		setBounds(100, 100, 450, 300);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(ArtisanInterFrm.class.getResource("/images/artisan.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(98)
		String className=(String)this.studentTable.getValueAt(row, 6);
		int n=this.schoolClassJcb.getItemCount();
		for(int i=0;i<n;i++){
			SchoolClass item=(SchoolClass)this.schoolClassJcb.getItemAt(i);
			if(item.getClassName().equals(className)){
				this.schoolClassJcb.setSelectedIndex(i);
			}
		}
	}

	/**
	 * 学生信息查询事件处理
	 * @param e
	 */
	private void bookSearchActionPerformed(ActionEvent evt) {
		String bookName=this.s_nameTxt.getText();
		String author=this.s_snTxt.getText();
		SchoolClass bookType=(SchoolClass) this.s_schoolClassJcb.getSelectedItem();
		int bookTypeId=bookType.getId();
		
		Student book=new Student(bookName,author,bookTypeId);
		this.fillTable(book);
	}

	/**
	 * 初始化下拉框
	 * @param type 下拉框类型 
	 */
	private void fillBookType(String type){
		Connection con=null;
		SchoolClass schoolClass=null;
		try{
			con=dbUtil.getCon();
			ResultSet rs=schoolClassDao.list(con, new SchoolClass());
			if("search".equals(type)){
				schoolClass=new SchoolClass();
				schoolClass.setClassName("请选择...");
				schoolClass.setId(-1);
				this.s_schoolClassJcb.addItem(schoolClass);
			}
			while(rs.next()){
				schoolClass=new SchoolClass();
				schoolClass.setClassName(rs.getString("className"));
				schoolClass.setId(rs.getInt("id"));
				if("search".equals(type)){
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(Connection con,String id)throws Exception{
		String sql="delete from t_student where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, id);
		return pstmt.executeUpdate();
	}
	
	/**
	 * 学生信息修改
	 * @param con
	 * @param student
	 * @return
	 * @throws Exception
	 */
	public int update(Connection con,Student student)throws Exception{
		String sql="update t_student set name=?,sn=?,sex=?,dept=?,address=?,classId=? where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, student.getName());
		pstmt.setString(2, student.getSn());
		pstmt.setString(3, student.getSex());
		pstmt.setString(4, student.getDept());
		pstmt.setString(5, student.getAddress());
		pstmt.setInt(6, student.getClassId());
		pstmt.setInt(7, student.getId());
		return pstmt.executeUpdate();
	}
	
	/**
	 * 指定班级下是否存在学生
	 * @param con
	 * @param studentTypeId
	 * @return
	 * @throws Exception
	 */
	public boolean existStudentByclassId(Connection con,String classId)throws Exception{
		String sql="select * from t_student where classId=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, classId);
		ResultSet rs=pstmt.executeQuery();
		return rs.next();
	}
}

		deptTxt = new JTextField();
		deptTxt.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("学生学号");
		
		snTxt = new JTextField();
		snTxt.setColumns(10);
		
		JLabel label_5 = new JLabel("所属班级");
		
		schoolClassJcb = new JComboBox();
		
		JLabel label_6 = new JLabel("家庭住址");
		
		addressTxt = new JTextArea();
		
		JButton button_1 = new JButton("\u4FEE\u6539");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				studentUpdateActionPerformed(evt);
			}
		});
		button_1.setIcon(new ImageIcon(StudentManageInterFrm.class.getResource("/images/modify.png")));
		
		JButton button_2 = new JButton("\u5220\u9664");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				studentDeleteActionPerformed(evt);
			}
		});
		button_2.setIcon(new ImageIcon(StudentManageInterFrm.class.getResource("/images/delete.png")));
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(19)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addComponent(button_1)
							.addGap(18)
							.addComponent(button_2)
	private JComboBox schoolClassJcb;
	private JTextArea addressTxt;
	private JTextField gradeTxt;
	private JRadioButton manJrb;
	private JRadioButton femaleJrb;
	
	private DbUtil dbUtil=new DbUtil();
	private SchoolClassDao schoolClassDao=new SchoolClassDao();
	private StudentDao studentDao=new StudentDao();

	

	/**
	 * Create the frame.
	 */
	public StudentAddInterFrm() {
		setClosable(true);
		setIconifiable(true);
		setTitle("添加学生信息");
		setBounds(100, 100, 450, 467);
		
		JLabel label = new JLabel("学生姓名");
		
		studentTxt = new JTextField();
		studentTxt.setColumns(10);
		
		JLabel label_1 = new JLabel("学生学号");
		
		snTxt = new JTextField();
		snTxt.setColumns(10);
		
		JLabel label_2 = new JLabel("学生性别");
		
		manJrb = new JRadioButton("男");
		buttonGroup.add(manJrb);
		manJrb.setSelected(true);
		
		femaleJrb = new JRadioButton("女");
		buttonGroup.add(femaleJrb);
		
		JLabel label_3 = new JLabel("成绩分数");
		
		deptTxt = new JTextField();
		deptTxt.setColumns(10);
		
		JLabel label_4 = new JLabel("家庭住址");
		
		addressTxt = new JTextArea();
		
	private void bookAddActionPerformed(ActionEvent evt) {
		String bookName=this.studentTxt.getText();
		String author=this.snTxt.getText();
		String price=this.deptTxt.getText();
		String bookDesc=this.addressTxt.getText();
		
		if(StringUtil.isEmpty(bookName)){
			JOptionPane.showMessageDialog(null, "学生姓名不能为空!");
			return;
		}
		
		if(StringUtil.isEmpty(author)){
			JOptionPane.showMessageDialog(null, "学生学号不能为空!");
			return;
		}
		
		if(StringUtil.isEmpty(price)){
			JOptionPane.showMessageDialog(null, "学生学院不能为空!");
			return;
		}
		
		String sex="";
		if(manJrb.isSelected()){
			sex="男";
		}else if(femaleJrb.isSelected()){
			sex="女";
		}
		
		SchoolClass bookType=(SchoolClass) schoolClassJcb.getSelectedItem();
		int bookTypeId=bookType.getId();
		
		Student book=new Student(bookName,author, sex, price , bookTypeId,  bookDesc);
		
		Connection con=null;
		try{
			con=dbUtil.getCon();
			int addNum=studentDao.add(con, book);
			if(addNum==1){
				JOptionPane.showMessageDialog(null, "学生添加成功!");
				resetValue();
			}else{
				JOptionPane.showMessageDialog(null, "学生添加失败!");
			}
		}catch(Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "学生添加失败!");
		}finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getDept() {
		return dept;
	}

	public void setDept(String dept) {
		this.dept = dept;
	}

	public Integer getClassId() {
		return classId;
	}

	public void setClassId(Integer classId) {
		this.classId = classId;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getAddress() {
		return address;
	}


	public String getCalssDesc() {
		return calssDesc;
	}

	public void setCalssDesc(String calssDesc) {
		this.calssDesc = calssDesc;
	}

	@Override
	public String toString() {
		return className;
	}

	
}

/**
 * 用户Dao类
 *
 */
public class UserDao {

	/**
	 * 登录验证
	 * @param con
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public User login(Connection con,User user)throws Exception{
		User resultUser=null;
		String sql="select * from t_user where userName=? and password=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, user.getUserName());
			JOptionPane.showMessageDialog(null, "学生学号不能为空!");
			return;
		}
		
		if(StringUtil.isEmpty(price)){
			JOptionPane.showMessageDialog(null, "学生学院不能为空!");
			return;
		}
		
		String sex="";
		if(manJrb.isSelected()){
			sex="男";
		}else if(femaleJrb.isSelected()){
			sex="女";
		}
		
		SchoolClass bookType=(SchoolClass) schoolClassJcb.getSelectedItem();
		int bookTypeId=bookType.getId();
		
		Student book=new Student(bookName,author, sex, price , bookTypeId,  bookDesc);
		
		Connection con=null;
		try{
			con=dbUtil.getCon();
			int addNum=studentDao.add(con, book);
			if(addNum==1){
				JOptionPane.showMessageDialog(null, "学生添加成功!");
				resetValue();
			}else{
				JOptionPane.showMessageDialog(null, "学生添加失败!");
			}
		}catch(Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "学生添加失败!");
		}finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
			sb.append(" and className like '%"+schoolClass.getClassName()+"%'");
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		return pstmt.executeQuery();
	}
	
	/**
	 * 删除班级
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(Connection con,String id)throws Exception{
		String sql="delete from t_school_class where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, id);
		return pstmt.executeUpdate();
	}
	
	/**
	 * 更新班级
	 * @param con
	 * @param schoolClass
	 * @return
	 * @throws Exception
	 */
	public int update(Connection con,SchoolClass schoolClass)throws Exception{
		String sql="update t_school_class set className=?,classDesc=? where id=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, schoolClass.getClassName());
		pstmt.setString(2, schoolClass.getCalssDesc());
		pstmt.setInt(3, schoolClass.getId());
		return pstmt.executeUpdate();
	}
}

/**
 * 学生实体
		}
		
		String sex="";
		if(manJrb.isSelected()){
			sex="男";
		}else if(femaleJrb.isSelected()){
			sex="女";
		}
		
		SchoolClass schoolClassTmp=(SchoolClass) schoolClassJcb.getSelectedItem();
		int scId=schoolClassTmp.getId();
		
		Student book=new Student(Integer.parseInt(id),  sname, sn, sex, dept,  scId,  address);
		
		Connection con=null;
		try{
			con=dbUtil.getCon();
			int addNum=studentDao.update(con, book);
			if(addNum==1){
				JOptionPane.showMessageDialog(null, "学生信息修改成功!");
				resetValue();
				this.fillTable(new Student());
			}else{
				JOptionPane.showMessageDialog(null, "学生信息修改失败!");
			}
		}catch(Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "学生信息修改失败!");
		}finally{
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 重置表单
	 */
	private void resetValue(){
		this.idTxt.setText("");
		this.nameTxt.setText("");
		this.snTxt.setText("");
		this.deptTxt.setText("");
		this.manJrb.setSelected(true);
		this.addressTxt.setText("");
		if(this.schoolClassJcb.getItemCount()>0){
			this.schoolClassJcb.setSelectedIndex(0);
		}

请添加图片描述
请添加图片描述

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐