1

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 });
            }
            }

        }

This gives me this error:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 > 0.

Any idea?

1
  • write your code here, don't link it. Commented Jan 31, 2013 at 17:53

3 Answers 3

3

Replace

int row = examTable.getRowCount()+1;

with

int row = examTable.getRowCount();

Everything is 0-based index in Java

Sign up to request clarification or add additional context in comments.

Comments

3

DefaultTableModel#insertRow inserts an entry at a given index. It is not possible to insert an entry beyond the model's current row count

Replace

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

with

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

Look at these lines:

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

Your SQL would suggest that you are expecting a single record to be returned from your query. However, by calling ResultSet#first, the cursor is advanced past the first possible row so your while loop is never entered. Instead you could replace these lines with:

if (result.next()) {

Aside from this, use a PreparedStatement rather than a Statement to protect against SQL Injection attacks.

8 Comments

Somehow it does not give me any results. No rows are being added. Also my table does not show itself when running the app.
See update. The issue of the table now showing itself could be a layout issue but I cannot comment on that as you have not included the code where it gets added.
The layout is fixed, yet I still see no entries (and yes the query actually gives entries). Ive followed your instructions correctly.
I'd say to place a breakpoint before addRow to make sure that you're entering the if statement scope and check the values being passed to the call itself.
Ive put a system out println in the else-statement if the resutl.next() returns false. For some reason it doesnt find any results, YET if I enter the query in my workbench it does give me multiple entries (with id 1).
|
0

Old thread but unanswered. I signed up just for this.
Everytime you update(add, insert, delete) your Table Model:

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

You need to refresh the table with the updated model:

jTable2.setModel(examTable);

This does not occur automatically in my experience.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.