目录

引言

一、定义学生类

二、编写学生接口

三、连接数据库,编写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;}@Overridepublic 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 blocke.printStackTrace();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();}return null;}/***获取Statement对象*/public static Statement getStatement(Connection con) {try {return con.createStatement();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}/*** 获取PreparedStatement对象*/public static PreparedStatement getPreparedStatement(Connection con,String sql) {try {return con.prepareStatement(sql);} catch (SQLException e) {// TODO Auto-generated catch blocke.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 blocke.printStackTrace();}}if(stat!=null) {try {stat.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(con!=null) {try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void close(Connection con,Statement stat) {if(stat!=null) {try {stat.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(con!=null) {try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void close(Connection con) {if(con!=null) {try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void close(Connection con,PreparedStatement stat,ResultSet rs) {if(rs!=null) {try {rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(stat!=null) {try {stat.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(con!=null) {try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void close(Connection con,PreparedStatement stat) {if(stat!=null) {try {stat.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(con!=null) {try {con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.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{@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic 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 blocke.printStackTrace();} finally {DBUtil.close(con, stat);}return false;}@Overridepublic 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 blocke.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);//设置窗口居中显示}
}

JDBC+java+swing实现学生信息管理系统相关推荐

  1. Java课程设计-基于Java Swing的学生信息管理系统-版本二

    Java课程设计-基于Java Swing的学生信息管理系统-版本二 1.介绍 2.相关技术 3. 代码仓库 4.所需环境 5.安装教程 6.运行截图 7.相关博客 1.介绍 设计一个简单学生个人信息 ...

  2. Java Swing Mysql学生信息管理系统

    通过对Swing的理解 此篇为大家推荐基于JAVA Swing Mysql学生信息管理系统 本视频教程一共分为四个阶段,每个阶段都会是上一个阶段的扩展,每一个阶段的系统都可独立作为一个完整的系统 第一 ...

  3. Java+Swing+mysql学生信息管理系统

    Java+Swing+mysql学生信息管理系统 一.系统介绍 二.功能展示 1.管理员登陆 2.学生信息查询 3.学生信息添加 4.学生信息修改 5.删除 三.系统实现 1.StudentFrame ...

  4. Java swing实现学生信息管理系统(管理员+学生)

    Java swing实现学生信息管理系统(管理员+学生) (ps:数据库中数据为个人编造,不涉及个人隐私问题,有需要源代码的请联系我) (代码中加入一些美化,整体功能不变) (表结构设计放在数据库截图 ...

  5. java制作管理系统视频_阶段1:手把手快速做一个Java swing mysql学生信息管理系统附带完整源码及视频开发教程【猿来入此自营】...

    <p> <span style="color:#666666;font-family:"font-size:16px;background-color:#FFFF ...

  6. Java课程设计-基于Swing的学生信息管理系统

    Java课程设计-基于Swing的学生信息管理系统 1.介绍 2.相关技术 3.项目地址 4.所需环境 5.安装教程 6.运行截图 7.相关博客 本代码是整理其他人项目,如有问题请及时联系笔者. 1. ...

  7. Java课程设计——学生信息管理系统

    一.项目简介 二.项目构架图 三.团队成员任务简介 四.代码 五.项目运行图片 一.项目简介: 实现一个功能简单的学生信息管理系统,该系统具有按照账户名密码登录功能,登录后,可以添加,删除,修改.查询 ...

  8. java+SQL做学生信息管理系统(增删改查)

    java+SQL做学生信息管理系统(增删改查) [过程中需要用到的所有工具数据库以及数据库管理器等等] https://pan.baidu.com/s/1cLKJPKXauLCl-Vwah6wFIQ ...

  9. 基于Eclipse+Java+Swing+Mysql图书信息管理系统

    基于Eclipse+Java+Swing+Mysql图书信息管理系统 一.系统介绍 二.功能展示 1.主页 2.新增图书信息 3.删除图书信息 三.数据库 四.其他系统实现 五.获取源码 一.系统介绍 ...

最新文章

  1. 烂大街的Spring循环依赖该如何回答?
  2. 【开源】QuickPager ASP.NET2.0分页控件V2.0.0.3 【增加了使用说明】
  3. ASP.NET Web API 使用Swagger生成在线帮助测试文档
  4. Alpha(5/10)
  5. Price determination entry point - how is 4.85 calculated
  6. Nodejs学习(三)-安装nodejs supervisor,提高点效率吧。
  7. 轻轻松松看懂Spring AOP源码
  8. 类的带参方法有哪几部分构成?
  9. 如何看待阿里云加入Linux基金会金牌会员?
  10. 你必须收藏的 GitHub 技巧
  11. 解决 No module named PyQt5.QtWebKitWidgets
  12. 多态、抽象类、接口_DAY09
  13. 在苹果Mac上找不到文件存储位置怎么办?
  14. LINUX编译automake
  15. 鲸鱼优化算法_鲸鱼优化算法:一种群体智能最优化方法
  16. 统计学和计算机的关系,浅议统计学与其他学科的关系
  17. php导入mib表,Linux snmp导入MIB库
  18. Spring warmth
  19. php文件是不是死链,怎么判断网站的链接是不是死链接? 百度搜索标准死链官方文档...
  20. Java学习预科知识

热门文章

  1. 2022年9月最新【国际版阿里云的注册流程】分享
  2. 区块链 | 为什么说百度云BaaS是全新的云服务平台?
  3. 完整正则表达式语法列表
  4. Android apk瘦身之使用TinyPng压缩图片
  5. 目标定位算法(二)之基于测距的定位算法
  6. 1.性能测试项目实战
  7. [已解决] c#签名时出错
  8. 【英语】--动起来的英语进度
  9. 高级产品经理十八种能力
  10. 欧拉函数定理及其性质