gpt4 book ai didi

java - ArrayList 到 JTable

转载 作者:行者123 更新时间:2023-12-01 12:18:02 25 4
gpt4 key购买 nike

我正在尝试从 SQL srv 加载 JTable 中的数据。我在尝试转换 ArrayList 并将其加载到 JTable 时遇到麻烦。当我尝试表下方的代码时,应显示列表显示“[L].java.lang.String@bf053f”

 private void searchCourseGrades() {
StudentsWithGradeTable.setModel(dtmCoursesGrades);
String ccode = SearchCidTextfield.getText();

try {
dtmCoursesGrades.setDataVector(
controller.findAllStudentsFromStudied(ccode),
coursesGradesColumns);

if (ccode.equals("")) {
JOptionPane.showMessageDialog(this,
"Please enter a Course Code");

} else {
dtmCoursesGrades.setRowCount(0);
dtmCoursesGrades.fireTableDataChanged();
Studied findStudentFromStudied = null;
findStudentFromStudied = controller
.findCourseFromStudied(ccode);

String[] crsgr = new String[2];

for (int i = 0; i < crsgr.length; i++) {
crsgr[0] = findStudentFromStudied.getSpnr();
crsgr[1] = findStudentFromStudied.getGrade();
dtmCoursesGrades.addRow(new Object []{crsgr});
dtmCoursesGrades.fireTableDataChanged();
}
}

} catch (SQLException exception) {
JOptionPane.showMessageDialog(this,
"Cannot find a course with Course Code " + ccode
+ ". Please try a new number.");
}

}

MainForm 引用的 Controller 如下所示:

private String[][] finishedArrayList(ArrayList<Studied> finished) {
String[][] finishedStudents = new String[finished.size()][3];

for (int i = 0; i < finished.size(); i++) {
Studied finish = finished.get(i);

finishedStudents[i][0] = finish.getSpnr();
finishedStudents[i][1] = finish.getCcode();
finishedStudents[i][2] = finish.getGrade();
}

return finishedStudents;
}

public String[][] findAllStudentsFromStudied(String ccode)
throws SQLException {
return finishedArrayList(dal.findAllStudentsFromStudied(ccode));

Controller 引用的 DAL 如下所示:

 public ArrayList<Studies> findAllStudentsFromStudies(String ccode)
throws SQLException {

ArrayList<Studies> stlist = new ArrayList<Studies>();
Statement stmt = con.createStatement();
String sqlStr = "SELECT * FROM Studies WHERE ccode = '" + ccode + "'";
ResultSet rs = stmt.executeQuery(sqlStr);

while (rs.next()) {
Studies st = new Studies();
st.setSpnr(rs.getString(1));

stlist.add(st);
}
rs.close();
stmt.close();
return stlist;
}

最佳答案

尝试 dtmCoursesGrades.addRow(new Object []{crsgr[0], crsgr[1]}); 而不是 dtmCoursesGrades.addRow(new Object []{crsgr});

关于java - ArrayList 到 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26864420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com