将行添加到DefaultTableModel

时间:2013-01-31 17:48:08

标签: java swing jtable defaulttablemodel

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

有什么想法吗?

3 个答案:

答案 0 :(得分:3)

DefaultTableModel#insertRow在给定索引处插入一个条目。无法在模型的当前行数

之外插入条目

替换

examTable.insertRow(row, new Object[] { name,headname,datum, exam,pres });

examTable.addRow(new Object[] { name,headname,datum, exam,pres });

看看以下几行:

if (result.first()) {
   while(result.next()) {

您的SQL表示您希望从查询中返回单条记录。但是,通过调用ResultSet#first,光标将超过第一个可能的行,因此永远不会输入while循环。相反,你可以用以下代码替换这些行:

if (result.next()) {

除此之外,请使用PreparedStatement而不是Statement来防范SQL Injection攻击。

答案 1 :(得分:3)

替换

int row = examTable.getRowCount()+1;

int row = examTable.getRowCount();

Java中的所有内容都是基于0的索引

答案 2 :(得分:0)

旧线程但没有答复。我为此报名了 每次更新(添加,插入,删除)表模型时:

top: 0

您需要使用更新的模型刷新表格:

position: static

根据我的经验,这不会自动发生。