JDBC+java+swing实现学生信息管理系统

目录

引言

一、定义学生类

二、编写学生接口

三、连接数据库,编写JDBC的工具类

三、编写接口实现类

四、下载WindowBuilder插件并安装,新建一个JFram文件,命名为StudentInformationFrame,在design界面中设置如下布局:

设置好布局之后,点击Source就会自动生成如下代码:


引言

       笔者最近刚学完java编程的基础,所以尝试做了一个简单的学生管理系统,主要实现了学生信息表的增删改查,如有不足之处,敬请各位读者批评指正。如下是效果图:

 

    大致思路如下:1、创建一个学生类  2、编写学生类的是实现接口  3、编写接口实现类  4、连接数据库  5、使用SWI制作界面

一、定义学生类

public class Student {
	private int Sno;//学号
	private String Sname;//姓名
	private int age;//年龄
	private String gender;//性别
	private String department;//系别
	private String courseSelected;//选课情况
	
	public Student() {
		super();
	}
	public Student(int sno, String sname, int age, String gender, String department, String courseSelected) {
		super();
		this.Sno = sno;
		this.Sname = sname;
		this.age = age;
		this.gender = gender;
		this.department = department;
		this.courseSelected = courseSelected;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public int getSno() {
		return Sno;
	}
	public void setSno(int sno) {
		this.Sno = sno;
	}
	public String getSname() {
		return Sname;
	}
	public void setSname(String sname) {
		this.Sname = sname;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public String getCourseSelected() {
		return courseSelected;
	}
	public void setCourseSelected(String courseSelected) {
		this.courseSelected = courseSelected;
	}
	@Override
	public String toString() {
		return "Student [Sno=" + Sno + ", Sname=" + Sname + ", age=" + age + ", gender=" + gender + ", department="
				+ department + ", courseSelected=" + courseSelected + "]";
	}
}

二、编写学生接口

public interface IStudentDao {
    //查询所有学生的信息
	public ArrayList<Student> selectStudentAll();
    //查询单个学生的信息
	public Student selectStudent(int sno);
    //插入一条学生记录
	public boolean insertStudent(Student s);
    //删除一条学生记录
	public boolean deleteStudent(int sno);
    //修改某条学生记录的信息
	public boolean updateStudent(Student s);
}

三、连接数据库,编写JDBC的工具类

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBUtil {
	private static Properties proper=new Properties();
	/**
	 * 静态代码块
	 */
	static {
		try {
			//加载properties文件
			proper.load(new FileInputStream("config/db.properties"));
			//1 加载外部JDBC驱动程序
			Class.forName(proper.getProperty("driverClassName"));
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 获取连接
	 * @return
	 */
	public static Connection getConnection() {
		try {
			//建立连接
			Connection con=DriverManager.getConnection(proper.getProperty("url")
			,proper.getProperty("username"),proper.getProperty("password"));
			return con;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 *获取Statement对象
	 */
	public static Statement getStatement(Connection con) {
		try {
			return con.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取PreparedStatement对象
	 */
	public static PreparedStatement getPreparedStatement(Connection con,String sql) {
		try {
			return con.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 关闭资源
	 */
	public static void close(Connection con,Statement stat,ResultSet rs) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(stat!=null) {
			try {
				stat.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void close(Connection con,Statement stat) {
		if(stat!=null) {
			try {
				stat.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void close(Connection con) {
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void close(Connection con,PreparedStatement stat,ResultSet rs) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(stat!=null) {
			try {
				stat.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void close(Connection con,PreparedStatement stat) {
		if(stat!=null) {
			try {
				stat.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con!=null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

三、编写接口实现类

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.yf.bean.Student;
import com.yf.utils.DBUtil;

public class StudentDaoImpl implements IStudentDao{
	@Override
	public ArrayList<Student> selectStudentAll() {
		Connection con = null;
		Statement stat = null;
		ResultSet rs = null;
		try {
			//1、获取连接
			con = DBUtil.getConnection();
			//2、创建Statement对象
			stat = DBUtil.getStatement(con);
			//3、定义SQL语句
			String sql = "select * from Student order by Sno";
			//返回查询到的结果集
			rs = stat.executeQuery(sql);
			ArrayList<Student> stuList = new ArrayList<Student>();
			while(rs.next()) {
				Student stu = new Student();
				stu.setSno(rs.getInt("Sno"));
				stu.setSname(rs.getString("Sname"));
				stu.setAge(rs.getInt("age"));
				stu.setGender(rs.getString("gender"));
				stu.setDepartment(rs.getString("department"));
				stu.setCourseSelected(rs.getString("courseSelected"));
				stuList.add(stu);
			}
			return stuList;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//关闭资源
			DBUtil.close(con, stat, rs);
		}
		return null;
	}

	@Override
	public Student selectStudent(int sno) {
		Connection con = null;
		PreparedStatement stat = null;
		ResultSet rs = null;
		try {
			//建立连接
			con = DBUtil.getConnection();
			//定义SQL语句
			String sql = "select * from Student where sno = ?";
			//创建预编译对象
			stat = con.prepareStatement(sql);
			stat.setInt(1,sno);
			rs = stat.executeQuery();
			//6、结果处理
			Student stu = new Student();
			while(rs.next()) {
				stu.setSno(rs.getInt("Sno"));
				stu.setSname(rs.getString("Sname"));
				stu.setAge(rs.getInt("age"));
				stu.setGender(rs.getString("gender"));
				stu.setDepartment(rs.getString("department"));
				stu.setCourseSelected(rs.getString("courseSelected"));
			}
			return stu;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, stat, rs);
		}
		return null;
	}

	@Override
	public boolean insertStudent(Student s) {
		Connection con = null;
		PreparedStatement stat = null;
		try {
			con = DBUtil.getConnection();
			String sql = "insert into Student(Sno,Sname,age,gender,department,courseSelected) values(?,?,?,?,?,?)";
			stat = con.prepareStatement(sql);
			stat.setInt(1,s.getSno());
			stat.setString(2,s.getSname());
			stat.setInt(3,s.getAge());
			stat.setString(4,s.getGender());
			stat.setString(5,s.getDepartment());
			stat.setString(6,s.getCourseSelected());
			if(stat.executeUpdate()>0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, stat);
		}
		return false;
	}

	@Override
	public boolean deleteStudent(int sno) {
		Connection con = null;
		PreparedStatement stat = null;
		
		con = DBUtil.getConnection();
		String sql = "delete from Student where Sno = ?";
		try {
			stat = con.prepareStatement(sql);
			stat.setInt(1,sno);
			if(stat.executeUpdate()>0) {
				//System.out.println("删除成功");
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.close(con, stat);
		}
		return false;
	}

	@Override
	public boolean updateStudent(Student s) {
		Student stu = selectStudent(s.getSno());
		Connection con = null;
		PreparedStatement stat = null;
		if(stu == null) {
			return false;
		}else {
			try {
				con = DBUtil.getConnection();
				String sql = "update Student set Sname=?,age = ?,gender=?,department=?,courseSelected=? where Sno = ?";
				stat = con.prepareStatement(sql);
				stat.setString(1,s.getSname());
				stat.setInt(2,s.getAge());
				stat.setString(3,s.getGender());
				stat.setString(4,s.getDepartment());
				stat.setString(5,s.getCourseSelected());
				stat.setInt(6,s.getSno());
				if(stat.execute()){
					return true;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				DBUtil.close(con, stat);
			}return false;
		}
	}
}

四、下载WindowBuilder插件并安装,新建一个JFram文件,命名为StudentInformationFrame,在design界面中设置如下布局:

设置好布局之后,点击Source就会自动生成如下代码:

public class StudentInformationFrame extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JTextField selectstubo;
	private JTextField stuNo;//学生编号
	private JTextField stuName_2;//
	private JTextField stuAge;
	private JTextField stuSex;
	private JTextField stuDepart;
	private JTextField select;
	private JTable table_1;
	private JTable table_2;
	private JTextField updatestuno;
	private JTextField updatestuname;
	private JTextField updatestusex;
	private JTextField updatestuage;
	private JTextField updatestudept;
	private JTextField updateselected;
	private JTable table_3;
	

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

	/**
	 * Create the frame.
	 * @param usertype 
	 */
	public StudentInformationFrame() {
		setFont(null);
		setBackground(new Color(240, 240, 240));
		setIconImage(Toolkit.getDefaultToolkit().getImage("image/stop.gif"));//窗口左上角图标
		this.setVisible(true);//设置窗口显示隐藏
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 800, 600);//设置窗口位置、尺寸(统一800*600)
		setResizable(false);//设置窗口不可缩放
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);//设置窗口颜色
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		//左栏
		JPanel Left = new JPanel();
		Left.setBackground(Color.PINK);
		Left.setBounds(0, 0, 154, 572);
		contentPane.add(Left);
		
		//右栏查询页
		JPanel Right1 = new JPanel();
		Right1.setBounds(152, 0, 642, 572);
		contentPane.add(Right1);
		//右栏插入页
		JPanel Right2 = new JPanel();
		Right2.setBounds(152, 0, 642, 572);
		contentPane.add(Right2);
		//右栏删除页
		JPanel Right3 = new JPanel();
		Right3.setBounds(152, 0, 642, 572);
		contentPane.add(Right3);
		//右栏修改页
		JPanel Right4 = new JPanel();
		Right4.setBounds(152, 0, 642, 572);
		contentPane.add(Right4);
		
		//菜单1:查询,点击即可查询全部学生记录的按钮
		JButton menu1 = new JButton("查询");
		menu1.setFont(new Font("宋体", Font.PLAIN, 16));
		menu1.setBounds(10, 10, 134, 50);
		menu1.addActionListener(new ActionListener() {
			Object[][] stuArray;
			public void actionPerformed(ActionEvent e) {
				StudentDaoImpl stuAll = new StudentDaoImpl();
				ArrayList<Student> stulist = stuAll.selectStudentAll();
				for (Student student : stulist) {
					student.toString();
				}
				Object[] array = stulist.toArray();
				stuArray = new Object[stulist.size()][6];
				for(int i=0;i<array.length;i++) {
					Student student = (Student)array[i];
					stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
				}
				
				table.setModel(new DefaultTableModel(
						stuArray,
						new String[] {
							"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
						}
					));
				Right1.setVisible(true);
				Right2.setVisible(false);
				Right3.setVisible(false);
				Right4.setVisible(false);
			}
		});
		Left.setLayout(null);
		Left.add(menu1);
		
		//菜单2:插入,点击即可跳转至插入界面
		JButton menu2 = new JButton("插入");
		menu2.setBounds(10, 105, 134, 50);
		menu2.setFont(new Font("宋体", Font.PLAIN, 16));
		menu2.addActionListener(new ActionListener() {
			Object[][] stuArray;
			public void actionPerformed(ActionEvent e) {
				if(!usertype) {
					JOptionPane.showMessageDialog(null, "当前用户权限不足!", "权限警告", JOptionPane.WARNING_MESSAGE);
					return;
				}
				StudentDaoImpl stuAll = new StudentDaoImpl();
				ArrayList<Student> stulist = stuAll.selectStudentAll();
				for (Student student : stulist) {
					student.toString();
				}
				Object[] array = stulist.toArray();
				stuArray = new Object[stulist.size()][6];
				for(int i=0;i<array.length;i++) {
					Student student = (Student)array[i];
					stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
				}
				
				table_1.setModel(new DefaultTableModel(
						stuArray,
						new String[] {
							"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
						}
					));
				Right2.setVisible(true);
				Right3.setVisible(false);
				Right1.setVisible(false);
				Right4.setVisible(false);
			}
		});
		Left.add(menu2);
		
		//菜单3:点击即可进入删除界面
		JButton menu3 = new JButton("删除");
		menu3.setFont(new Font("宋体", Font.PLAIN, 16));
		menu3.setBounds(10, 217, 134, 50);
		menu3.addActionListener(new ActionListener() {
			Object stuArray [][];
			public void actionPerformed(ActionEvent e) {
				if(!usertype) {
					JOptionPane.showMessageDialog(null, "当前用户权限不足!", "权限警告", JOptionPane.WARNING_MESSAGE);
					return;
				}
				StudentDaoImpl stuAll = new StudentDaoImpl();
				ArrayList<Student> stulist = stuAll.selectStudentAll();
				for (Student student : stulist) {
					student.toString();
				}
				Object[] array = stulist.toArray();
				stuArray = new Object[stulist.size()][6];
				for(int i=0;i<array.length;i++) {
					Student student = (Student)array[i];
					stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
				}
				
				table_2.setModel(new DefaultTableModel(
						stuArray,
						new String[] {
							"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
						}
					));
				//boolean flag = stu.deleteStudent();
				Right3.setVisible(true);
				Right1.setVisible(false);
				Right2.setVisible(false);
				Right4.setVisible(false);
			}
		});
		Left.add(menu3);
		//菜单4:点击即可进入修改界面
		JButton menu4 = new JButton("修改");
		menu4.setFont(new Font("宋体", Font.PLAIN, 16));
		menu4.addActionListener(new ActionListener() {
			Object stuArray [][];
			public void actionPerformed(ActionEvent arg0) {
				if(!usertype) {
					JOptionPane.showMessageDialog(null, "当前用户权限不足!", "权限警告", JOptionPane.WARNING_MESSAGE);
					return;
				}
				StudentDaoImpl stuAll = new StudentDaoImpl();
				ArrayList<Student> stulist = stuAll.selectStudentAll();
				for (Student student : stulist) {
					student.toString();
				}
				Object[] array = stulist.toArray();
				stuArray = new Object[stulist.size()][6];
				for(int i=0;i<array.length;i++) {
					Student student = (Student)array[i];
					stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
				}
				
				table_3.setModel(new DefaultTableModel(
						stuArray,
						new String[] {
							"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
						}
					));
				Right4.setVisible(true);
				Right1.setVisible(false);
				Right2.setVisible(false);
				Right3.setVisible(false);
			}
		});
		menu4.setBounds(10, 310, 134, 50);
		Left.add(menu4);
		
		//菜单5:主菜单界面
		JButton menu5 = new JButton("主菜单");
		menu5.setFont(new Font("宋体", Font.PLAIN, 16));
		menu5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//根据usertype用户类型切换主菜单
				if(usertype) {
					new TeacherMenuFrame(usertype,uname);
				}else {
					new StudentMenuFrame(usertype,uname);
				}
				StudentInformationFrame.this.dispose();
			}
		});
		menu5.setBounds(10, 410, 134, 50);
		Left.add(menu5);
		//菜单6:退出按钮
		JButton menu6 = new JButton("\u9000\u51FA");
		menu6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 退出
				if (JOptionPane.showConfirmDialog(null, "确认退出?", "确认", JOptionPane.OK_CANCEL_OPTION) == 0) {
					System.exit(0);
				}
			}
		});
		menu6.setFont(new Font("宋体", Font.PLAIN, 16));
		menu6.setBounds(10, 510, 134, 50);
		Left.add(menu6);
		Right3.setLayout(null);
		
/*
 * 每个页面中的按钮的相应操作
 */
//查询页面的按钮------------------------------------------------------------------------------------------------------
		//搜索按钮,查询单个学生的记录
		JButton btnNewButton = new JButton("搜索");
		btnNewButton.addActionListener(new ActionListener() {
			Object[][] stuArray; 
			public void actionPerformed(ActionEvent e) {
				StudentDaoImpl stu = new StudentDaoImpl();
				String stuId = selectstubo.getText();
				if(stuId.equals("")) {
					JOptionPane.showMessageDialog(null, "搜索内容不能为空", "提示", JOptionPane.ERROR_MESSAGE);
					return;
				}
				int stuid = Integer.parseInt(selectstubo.getText());
				Student student = stu.selectStudent(stuid);
				stuArray = new Object[1][6];
				stuArray[0] = new Object[] {student.getSno(),student.getSname(),student.getGender(),student.getDepartment(),student.getAge(),student.getCourseSelected()};
				Right1.setVisible(true);
				Right2.setVisible(false);
				Right3.setVisible(false);
				Right4.setVisible(false);
				table.setModel(new DefaultTableModel(
					stuArray,
					new String[] {
						"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
					}
				));
			}
		});
		btnNewButton.setBounds(421, 33, 78, 23);
		Right1.add(btnNewButton);
		
		//插入数据按钮,点击可实现页面跳转至插入操作
		JButton btnNewButton_6 = new JButton("插入数据");
		btnNewButton_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {	
				if(!usertype) {
					JOptionPane.showMessageDialog(null, "当前用户权限不足!", "权限警告", JOptionPane.WARNING_MESSAGE);
					return;
				}
				Right2.setVisible(true);
				Right1.setVisible(false);
				Right4.setVisible(false);
				Right3.setVisible(false);
			}
		});
		btnNewButton_6.setBounds(68, 495, 93, 23);
		Right1.add(btnNewButton_6);
		
		//删除数据按钮,点击跳转是删除数据操作
				JButton btnNewButton_7 = new JButton("\u5220\u9664\u6570\u636E");
				btnNewButton_7.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						if(!usertype) {
							JOptionPane.showMessageDialog(null, "当前用户权限不足!", "权限警告", JOptionPane.WARNING_MESSAGE);
							return;
						}
						Right3.setVisible(true);
						Right1.setVisible(false);
						Right4.setVisible(false);
						Right2.setVisible(false);
					}
				});
				btnNewButton_7.setBounds(190, 495, 93, 23);
				Right1.add(btnNewButton_7);
		
		//修改数据按钮,点击某条记录后再点击此按钮即可跳转至修改数据按钮
		JButton btnNewButton_8 = new JButton("修改数据");
		btnNewButton_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!usertype) {
					JOptionPane.showMessageDialog(null, "当前用户权限不足!", "权限警告", JOptionPane.WARNING_MESSAGE);
					return;
				}
				int row  =  table.getSelectedRow();
				String str = table.getValueAt(row,0).toString();
				String str1 = table.getValueAt(row,1).toString();
				String str2 = table.getValueAt(row,2).toString();
				String str3 = table.getValueAt(row,3).toString();
				String str4 = table.getValueAt(row,4).toString();
				String str5 = table.getValueAt(row,5).toString();
				updatestuno.setText(str);
				updatestuno.setEditable(false);
				updatestuname.setText(str1);
				updatestusex.setText(str2);
				updatestuage.setText(str3);
				updatestudept.setText(str4);
				updateselected.setText(str5);			
				Right4.setVisible(true);
				Right3.setVisible(false);
				Right1.setVisible(false);
				Right2.setVisible(false);
			}
		});
		btnNewButton_8.setBounds(306, 495, 93, 23);
		Right1.add(btnNewButton_8);
		
//删除页面---------------------------------------------------------------------------------------------------------
		//点击删除按钮,删除单个记录
		JButton btnNewButton_2 = new JButton("删除数据");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = table_2.getSelectedRow();
				String str = table_2.getValueAt(row,0).toString();
				int sno = Integer.valueOf(str);
				StudentDaoImpl stu = new StudentDaoImpl();
				boolean flag = stu.deleteStudent(sno);
				if(flag) {
					System.out.println("删除成功");
				}else {
					System.out.println("删除失败");
				}
			}
		});
		btnNewButton_2.setBounds(104, 54, 134, 23);
		Right3.add(btnNewButton_2);
		
		//点击刷新按钮,可刷新记录
		JButton btnNewButton_3 = new JButton("\u5237\u65B0\u8BB0\u5F55");
		btnNewButton_3.addActionListener(new ActionListener() {
			Object stuArray [][];
			public void actionPerformed(ActionEvent e) {
				StudentDaoImpl stuAll = new StudentDaoImpl();
				ArrayList<Student> stulist = stuAll.selectStudentAll();
				for (Student student : stulist) {
					student.toString();
				}
				Object[] array = stulist.toArray();
				stuArray = new Object[stulist.size()][6];
				for(int i=0;i<array.length;i++) {
					Student student = (Student)array[i];
					stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
				}
				
				table_2.setModel(new DefaultTableModel(
						stuArray,
						new String[] {
							"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
						}
					));
			}
		});
		btnNewButton_3.setBounds(337, 54, 134, 23);
		Right3.add(btnNewButton_3);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(54, 110, 527, 325);
		Right3.add(scrollPane_2);
		
		//删除操作的表
		table_2 = new JTable();
		table_2.setRowHeight(30);
		scrollPane_2.setViewportView(table_2);
		table_2.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
			},
			new String[] {
				"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u9009\u8BFE\u60C5\u51B5"
			}
		));
		Right1.setLayout(null);
		
		JScrollPane scrollPane_4 = new JScrollPane();
		scrollPane_4.setBounds(68, 85, 475, 379);
		Right1.add(scrollPane_4);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane_4.setViewportView(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
			},
			new String[] {
				"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u9009\u8BFE\u60C5\u51B5"
			}
		));
		table.getColumnModel().getColumn(0).setPreferredWidth(85);
		table.getColumnModel().getColumn(2).setPreferredWidth(76);
		table.getColumnModel().getColumn(4).setPreferredWidth(92);
		table.setRowHeight(35);
		
		JLabel lblNewLabel = new JLabel("\u8F93\u5165\u67E5\u8BE2\u5B66\u751F\u7684\u7F16\u53F7");
		lblNewLabel.setBounds(73, 35, 140, 18);
		Right1.add(lblNewLabel);
		
		selectstubo = new JTextField();
		selectstubo.setBounds(204, 34, 163, 21);
		Right1.add(selectstubo);
		selectstubo.setColumns(10);
		
		Right1.setVisible(true);
		Right2.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("\u5B66\u751F\u7F16\u53F7");
		lblNewLabel_1.setBounds(70, 60, 81, 15);
		Right2.add(lblNewLabel_1);
		
		stuNo = new JTextField();
		stuNo.setBounds(128, 57, 109, 21);
		Right2.add(stuNo);
		stuNo.setColumns(10);
		
		JLabel label_1 = new JLabel("\u5B66\u751F\u59D3\u540D");
		label_1.setBounds(247, 60, 66, 15);
		Right2.add(label_1);
		
		stuName_2 = new JTextField();
		stuName_2.setColumns(10);
		stuName_2.setBounds(305, 57, 109, 21);
		Right2.add(stuName_2);
		
		JLabel label_2 = new JLabel("\u5E74    \u9F84");
		label_2.setBounds(424, 60, 48, 15);
		Right2.add(label_2);
		
		stuAge = new JTextField();
		stuAge.setColumns(10);
		stuAge.setBounds(482, 57, 109, 21);
		Right2.add(stuAge);
		
		JLabel label_3 = new JLabel("\u6027    \u522B");
		label_3.setBounds(70, 92, 67, 15);
		Right2.add(label_3);
		
		stuSex = new JTextField();
		stuSex.setColumns(10);
		stuSex.setBounds(128, 89, 109, 21);
		Right2.add(stuSex);
		
		stuDepart = new JTextField();
		stuDepart.setColumns(10);
		stuDepart.setBounds(305, 85, 109, 21);
		Right2.add(stuDepart);
		
		select = new JTextField();
		select.setColumns(10);
		select.setBounds(482, 88, 109, 21);
		Right2.add(select);
		
		JLabel label_4 = new JLabel("\u6240\u5C5E\u9662\u6821");
		label_4.setBounds(247, 85, 66, 15);
		Right2.add(label_4);
		
		JLabel label_5 = new JLabel("\u9009\u8BFE\u60C5\u51B5");
		label_5.setBounds(423, 85, 66, 15);
		Right2.add(label_5);
		
		//点击添加记录
		JButton btnNewButton_1 = new JButton("\u6DFB\u52A0");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int sno = Integer.parseInt(stuNo.getText());
				String sname = stuName_2.getText();
				int sage = Integer.parseInt(stuAge.getText());
				String sgender = stuSex.getText();
				String sdepart = stuDepart.getText();
				String selected = select.getText();
				Student student = new Student(sno,sname,sage,sgender,sdepart,selected);
				StudentDaoImpl stu = new StudentDaoImpl();
				boolean flag = stu.insertStudent(student);
				if(flag) {
					System.out.println("插入成功");
				}else {
					System.out.println("插入失败");
				}
			}
		});
		btnNewButton_1.setBounds(371, 138, 101, 23);
		Right2.add(btnNewButton_1);
		
		//刷新记录
		JButton button = new JButton("\u5237\u65B0\u8BB0\u5F55");
		button.addActionListener(new ActionListener() {
			Object[][] stuArray;
			public void actionPerformed(ActionEvent e) {
				StudentDaoImpl stuAll = new StudentDaoImpl();
				ArrayList<Student> stulist = stuAll.selectStudentAll();
				for (Student student : stulist) {
					student.toString();
				}
				Object[] array = stulist.toArray();
				stuArray = new Object[stulist.size()][6];
				for(int i=0;i<array.length;i++) {
					Student student = (Student)array[i];
					stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
				}
				
				table_1.setModel(new DefaultTableModel(
						stuArray,
						new String[] {
							"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
						}
					));
			}
		});
		button.setBounds(490, 138, 101, 23);
		Right2.add(button);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(71, 181, 520, 320);
		Right2.add(scrollPane_1);
		
		table_1 = new JTable();
		scrollPane_1.setViewportView(table_1);
		table_1.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
			},
			new String[] {
				"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u6821", "\u9009\u8BFE\u60C5\u51B5"
			}
		));
		table_1.setRowHeight(30);
		Right2.setVisible(false);
		Right4.setLayout(null);
				
		JLabel lblNewLabel_3 = new JLabel("\u5B66\u751F\u7F16\u53F7");
		lblNewLabel_3.setBounds(76, 34, 66, 15);
		Right4.add(lblNewLabel_3);
		
		updatestuno = new JTextField();
		updatestuno.setBounds(147, 31, 178, 21);
		Right4.add(updatestuno);
		updatestuno.setColumns(10);
		
		updatestuname = new JTextField();
		updatestuname.setBounds(147, 59, 178, 21);
		Right4.add(updatestuname);
		updatestuname.setColumns(10);
		
		JLabel lblNewLabel_5 = new JLabel("\u4FEE\u6539\u6027\u522B");
		lblNewLabel_5.setBounds(76, 90, 70, 15);
		Right4.add(lblNewLabel_5);
		
		updatestusex = new JTextField();
		updatestusex.setBounds(147, 87, 178, 21);
		Right4.add(updatestusex);
		updatestusex.setColumns(10);
		
		JLabel lblNewLabel_6 = new JLabel("\u4FEE\u6539\u5E74\u9F84");
		lblNewLabel_6.setBounds(76, 115, 66, 15);
		Right4.add(lblNewLabel_6);
		
		updatestuage = new JTextField();
		updatestuage.setBounds(147, 112, 178, 21);
		Right4.add(updatestuage);
		updatestuage.setColumns(10);
		
		JLabel lblNewLabel_7 = new JLabel("\u4FEE\u6539\u9662\u7CFB");
		lblNewLabel_7.setBounds(76, 140, 66, 15);
		Right4.add(lblNewLabel_7);
		
		updatestudept = new JTextField();
		updatestudept.setBounds(147, 137, 178, 21);
		Right4.add(updatestudept);
		updatestudept.setColumns(10);
		
		JLabel lblNewLabel_8 = new JLabel("\u4FEE\u6539\u9009\u8BFE\u60C5\u51B5");
		lblNewLabel_8.setBounds(64, 165, 72, 15);
		Right4.add(lblNewLabel_8);
		
		updateselected = new JTextField();
		updateselected.setBounds(147, 162, 178, 21);
		Right4.add(updateselected);
		updateselected.setColumns(10);
		
		JLabel lblNewLabel_9 = new JLabel("");
		lblNewLabel_9.setBounds(337, 37, 60, 15);
		Right4.add(lblNewLabel_9);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(64, 207, 507, 328);
		Right4.add(scrollPane_3);
		
		table_3 = new JTable();
		scrollPane_3.setViewportView(table_3);
		table_3.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
			},
			new String[] {
					"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u9009\u8BFE\u60C5\u51B5"
			}
		));
		table_3.setRowHeight(30);
		
		//点击修改按钮,修改记录
		JButton btnNewButton_4 = new JButton("\u70B9\u51FB\u4FEE\u6539");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int stuno = Integer.valueOf(updatestuno.getText());
				String stuname = updatestuname.getText();
				String stusex = updatestusex.getText();
				int stuage = Integer.parseInt(updatestuage.getText());
				//System.out.println(updatestuage.getText()+stuage);
				String studept = updatestudept.getText();
				String stuselected = updateselected.getText();
				if(updatestuno.getText().length()==0) {
					JOptionPane.showMessageDialog(null, "学号不能为空");
				}
				StudentDaoImpl stu = new StudentDaoImpl();
				Student student = new Student();
				student = stu.selectStudent(stuno);
				student.setSno(stuno);
				student.setSname(stuname);
				student.setGender(stusex);
				student.setAge(stuage);
				student.setDepartment(studept);
				student.setCourseSelected(stuselected);
				if(!stu.updateStudent(student)) {
					System.out.println("修改成功");
				}else {
					System.out.println("修改失败");
				}
			}
		});
		btnNewButton_4.setBounds(358, 111, 178, 23);
		
		Right4.add(btnNewButton_4);
		
		//修改页面:点击刷新按钮
		JButton btnNewButton_5 = new JButton("\u70B9\u51FB\u5237\u65B0");
		btnNewButton_5.addActionListener(new ActionListener() {
			Object [][]stuArray;
			public void actionPerformed(ActionEvent e) {
				StudentDaoImpl stuAll = new StudentDaoImpl();
				ArrayList<Student> stulist = stuAll.selectStudentAll();
				for (Student student : stulist) {
					student.toString();
				}
				Object[] array = stulist.toArray();
				stuArray = new Object[stulist.size()][6];
				for(int i=0;i<array.length;i++) {
					Student student = (Student)array[i];
					stuArray[i] = new Object[]{student.getSno(),student.getSname(),student.getGender(),student.getAge(),student.getDepartment(),student.getCourseSelected()};
				}
				
				table_3.setModel(new DefaultTableModel(
						stuArray,
						new String[] {
							"\u5B66\u751F\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u6240\u5C5E\u9662\u7CFB", "\u6559\u5BA4\u7F16\u53F7"
						}
					));
			}
		});
		btnNewButton_5.setBounds(358, 161, 178, 23);
		Right4.add(btnNewButton_5);
		
		JLabel lblNewLabel_10 = new JLabel("\u4FEE\u6539\u59D3\u540D");
		lblNewLabel_10.setBounds(76, 62, 66, 15);
		Right4.add(lblNewLabel_10);
		
		JButton btnNewButton_9 = new JButton("\u70B9\u51FB\u9009\u62E9\uFF0C\u81EA\u52A8\u586B\u5165\u6570\u636E");
		btnNewButton_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = table_3.getSelectedRow();
				String str = table_3.getValueAt(row,0).toString();
				String str1 = table_3.getValueAt(row,1).toString();
				String str2 = table_3.getValueAt(row,2).toString();
				String str3 = table_3.getValueAt(row,3).toString();
				String str4 = table_3.getValueAt(row,4).toString();
				String str5 = table_3.getValueAt(row,5).toString();
				updatestuno.setText(str);
				updatestuno.setEditable(false);
				updatestuname.setText(str1);
				updatestusex.setText(str2);
				updatestuage.setText(str3);
				updatestudept.setText(str4);
				updateselected.setText(str5);
			}
		});
		btnNewButton_9.setBounds(358, 58, 178, 23);
		Right4.add(btnNewButton_9);
		Right4.setVisible(false);
		Right3.setVisible(false);
		setLocationRelativeTo(null);//设置窗口居中显示
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值