gpt4 book ai didi

java - 将行添加到 DefaultTableModel

转载 作者:行者123 更新时间:2023-11-29 07:10:11 25 4
gpt4 key购买 nike

http://codepaste.net/ujhdq2

    private Connection Econn;
private DefaultTableModel examTable = new DefaultTableModel();
public StudentInfoFrame(int eid) {
initComponents();
this.e_id = eid;
try
{
jTable2.getParent().setBackground(Color.black);
SimpleDataSource.init();
Econn = SimpleDataSource.getConnection();
jTable2.setModel(examTable);
retrieveExams();
}
catch(SQLException | ClassNotFoundException e)
{
System.out.println(e);
}
}

private void retrieveExams()
{
try
{
Statement stat = Econn.createStatement();
String query = "SELECT date, name,forename,surname,status,Exam "+
"FROM studentexam sx INNER JOIN Exam e ON sx.ex_id = e.ex_id " +
"INNER JOIN employee em ON e.head = em.e_id WHERE st_id = "+this.e_id;
ResultSet result = stat.executeQuery(query);
if(result.first())
{
while(result.next())
{
String headname = result.getString("forename")+" "+result.getString("surname");
String name = result.getString("name");
int status = result.getInt("status");
String pres;
if(status == 1)
{
pres = "Yes";
}
else
{
pres = "No";
}
String exam;
if(result.getInt("Exam") == 1)
{
exam = "Yes";
}
else
{
exam = "No";
}
Date date = result.getDate("date");
int day = date.getDay();
int year = date.getYear()+1900;
int month = date.getMonth()+1;
String datum = year+"-"+month+"-"+day;
int row = examTable.getRowCount()+1;
examTable.insertRow(row, new Object[] { name,headname,datum, exam,pres });
}
}

}

这给了我这个错误:

线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException 中的异常:1 > 0。

有什么想法吗?

最佳答案

替换

int row = examTable.getRowCount()+1;

int row = examTable.getRowCount();

在 Java 中一切都是基于 0 的索引

关于java - 将行添加到 DefaultTableModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14631968/

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