简单学生管理系统 Oracle数据库实现
model包:Student实体封装学生信息,StudentTableModel实体存放学生信息集合表格
package com.stu.model;
public class Student {
private String sno;
private String sname;
private String ssex;
private int sage;
private String sdept;
public Student() {
}
public Student(String sno, String sname, String ssex, int sage, String sdept) {
this.sno = sno;
this.sname = sname;
this.ssex = ssex;
this.sage = sage;
this.sdept = sdept;
}
//用get和set方法封装数据成员
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
public String getSdept() {
return sdept;
}
public void setSdept(String sdept) {
this.sdept = sdept;
}
@Override
public String toString() {
return "Student{" + "sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", sage=" + sage + ", sdept=" + sdept + '}';
}
}
package com.stu.model;
import com.stu.util.DaoFactory;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class StudentTableModel extends AbstractTableModel {
//表格中第一行所要显示的内容存放在字符串数组columnNames中
final String[] columnNames = {"学号","姓名","性别","年龄","系部"};
//表格中各行的内容保存在集合列表中
List<Student> rowsData;
// 表格模型默认构造方法
public StudentTableModel() {
rowsData = DaoFactory.getStudentDao().getAllStudent();
}
// 表格模型带参数构造方法
public StudentTableModel(String sno) {
rowsData = new ArrayList<Student>();
rowsData.add(DaoFactory.getStudentDao().getStudent(sno));
}
// 获得行的数目
@Override
public int getRowCount() {
return rowsData.size();
}
// 获得列的数目
@Override
public int getColumnCount() {
return columnNames.length;
}
//获得某行某列的数据,而数据保存在集合列表rowsData中
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
switch (columnIndex) {
case 0:
return rowsData.get(rowIndex).getSno();
case 1:
return rowsData.get(rowIndex).getSname();
case 2:
return rowsData.get(rowIndex).getSsex();
case 3:
return rowsData.get(rowIndex).getSage();
case 4:
return rowsData.get(rowIndex).getSdept();
default:
return null;
}
}
//获得某列的名字,而目前各列的名字保存在字符串数组columnNames中
@Override
public String getColumnName(int column) {
return columnNames[column];
}
}