我打算在我在本课程中创建的 JTable 中显示学生成绩。我的意图是在表单加载时有一个空表,在输入一些参数时有一个空表,查询数据库并将结果显示在 JTable 中。Empty 表正在显示,但在 Button Click 处没有发生任何事情。也不例外。它只是保持为空。这是我的代码...(部分)
public class ClassExamResults extends JInternalFrame implements ActionListener {
private JPanel jpCer = new JPanel();
private DefaultTableModel dtmCustomer;
private JTable tbCustomer;
private JScrollPane jspTable;
static Vector<Vector<String>> data = new Vector<Vector<String>>();
ClassExamResults() {
super("Class Exam Results", false, true, false, true);
setSize(1350, 620);
jpCer.setBounds(10, 0, 500, 150);
jpCer.setLayout(null);
jpCer.setBackground(Color.LIGHT_GRAY);
//Define table
tbCustomer = makeTable();
jspTable = new JScrollPane(tbCustomer);
jspTable.setBounds(170, 20, 1150, 550);
jpCer.add(jspTable);
getContentPane().add(jpCer);
setVisible(true);
}
private JTable makeTable() {
//String Type Array use to Give Table Column Names.
Vector<String> headers = new Vector<String>();
headers.add("ADMIS. NO.");
headers.add("NAME");
headers.add("EXAM");
headers.add("MATHS");
headers.add("ENG.");
headers.add("KISWA.");
headers.add("PHYC.");
headers.add("CHEM");
headers.add("BIO");
headers.add("HISTO.");
headers.add("GEOG.");
headers.add("CRE");
headers.add("COMP.");
headers.add("HOME SC.");
headers.add("BUSIN");
headers.add("AGRIC.");
headers.add("TOTAL");
headers.add("AVERAGE");
headers.add("POS.");
dtmCustomer = new DefaultTableModel(data, headers);
tbCustomer = new JTable(dtmCustomer) {
public boolean isCellEditable(int iRow, int iCol) {
return false; //Disable All Columns of Table.
}
};
tbCustomer.setRowHeight(25);
tbCustomer.setBackground(new java.awt.Color(89, 151, 207));
tbCustomer.setForeground(Color.BLACK);
tbCustomer.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
return tbCustomer;
}
public void actionPerformed(ActionEvent ae) {
Object obj = ae.getSource();
if (obj == btnFind) {
if (level.equalsIgnoreCase("SELECT")) {
JOptionPane.showMessageDialog(this, "Please Select Exam Level",
"Error", JOptionPane.ERROR_MESSAGE);
cboLevel.requestFocus();
} else if (year.equalsIgnoreCase("SELECT")) {
JOptionPane.showMessageDialog(this, "Please Select Exam Academic year",
"Error", JOptionPane.ERROR_MESSAGE);
cboYear.requestFocus();
} else {
Connection conn = null;
Vector<String> headers = new Vector<String>();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "USERNAME", "PASSWORD");
Statement st = conn.createStatement();
String results = "select stu_number as ADMISSION_NUMBER, "
+ " max(stu_name) STUDENT_NAME, "
+ "ex_name as EXAM_NAME, "
+ "max(case when ex_subject = 'MATHEMATICS' then stu_score else 0 end) as MATHEMATICS, "
+ " max(case when ex_subject = 'ENGLISH' then stu_score else 0 end) as ENGLISH, "
+ " max(case when ex_subject = 'KISWAHILI' then stu_score else 0 end) as KISWAHILI, "
+ " max(case when ex_subject = 'PHYSICS' then stu_score else 0 end) as PHYSICS, "
+ " max(case when ex_subject = 'CHEMISTRY' then stu_score else 0 end) as CHEMISTRY, "
+ " max(case when ex_subject = 'BIOLOGY' then stu_score else 0 end) as BIOLOGY, "
+ "max(case when ex_subject = 'HISTORY' then stu_score else 0 end) as HISTORY, "
+ " max(case when ex_subject = 'GEOGRAPHY' then stu_score else 0 end) as GEOGRAPHY, "
+ " max(case when ex_subject = 'CRE' then stu_score else 0 end) as CRE, "
+ " max(case when ex_subject = 'COMPUTER' then stu_score else 0 end) as COMPUTER, "
+ " max(case when ex_subject = 'HOME SCIENCE' then stu_score else 0 end) as HOME_SCIENCE, "
+ " max(case when ex_subject = 'BUSINESS' then stu_score else 0 end) as BUSINESS, "
+ " max(case when ex_subject = 'AGRICULTURE' then stu_score else 0 end) as AGRICULTURE, "
+ " sum(stu_score) as TOTAL_MARKS, "
+ " sum(stu_score)/count(stu_score) as AVERAGE, "
+ " rank () over (order by sum(stu_score)desc) as POSITION "
+ "from EXAMS_MASTER_REGISTER "
+ "where stu_level = '" + level + "' and "
+ " stu_stream = '" + stream + "' and "
+ " ex_semester = '" + semester + "' and "
+ " ex_name = '" + exam + "' and "
+ " academic_year = '" + year + "' "
+ "group by stu_number, ex_name "
+ "order by sum(stu_score) desc";
ResultSet rs = st.executeQuery(results);
while (rs.next()) {
Vector<String> d = new Vector<String>();
d.add(rs.getString("ADMISSION_NUMBER"));
d.add(rs.getString("STUDENT_NAME"));
d.add(rs.getString("EXAM_NAME"));
d.add(rs.getString("MATHEMATICS"));
d.add(rs.getString("ENGLISH"));
d.add(rs.getString("KISWAHILI"));
d.add(rs.getString("PHYSICS"));
d.add(rs.getString("CHEMISTRY"));
d.add(rs.getString("BIOLOGY"));
d.add(rs.getString("HISTORY"));
d.add(rs.getString("GEOGRAPHY"));
d.add(rs.getString("CRE"));
d.add(rs.getString("COMPUTER"));
d.add(rs.getString("HOME_SCIENCE"));
d.add(rs.getString("BUSINESS"));
d.add(rs.getString("AGRICULTURE"));
System.out.println(rs.getString("STUDENT_NAME"));
data.add(d);
}
} catch (Exception db) {
JOptionPane.showMessageDialog(this, db,
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
}
我知道查询执行是因为;System.out.println(rs.getString("STUDENT_NAME")); 返回正确的值。我还尝试从 toad 运行查询,它显示了正确的结果。我的错误在哪里?