0

I am doing a Practical Assessment Task for my Grade 12 IT class. I am struggling with the code to populate my jTable (From the swing controls) in my GUI. I am using Netbeans as an IDE. The database connects using a jdbc:odbc bridge. This code is in a java class in my project.

**please note that i am relatively inexperienced when it comes to coding

Here is the code i have currently (but it gives no output in my jtable) if you can fix it, please explain what you did or if you can please give me code that will work :)

 db.setConnection();
    Statement stmt = null;

    try {
        con = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}"
                + ";DBQ=src/TheChangeProjectDB.accdb");
        stmt = con.createStatement();
    } catch (SQLException ex) {
        JOptionPane.showMessageDialog(null, "Error: " + ex);
    }


    String sql = "SELECT * FROM KanyisaLearners";
    try {

        ResultSet rs = stmt.executeQuery(sql);
        ResultSetMetaData md = rs.getMetaData();
        int columns = md.getColumnCount();
        for (int i = 1; i <= columns; i++) {
            columnNames.addElement(md.getColumnName(i));
        }
        while (rs.next()) {
            Vector row = new Vector(columns);
            for (int i = 0; i <= columns; i++) {
                row.addElement(rs.getObject(i));
            }
            data.addElement(row);
        }
        rs.close();
        stmt.close();
    } catch (Exception e) {
        System.out.println(e);
    }

    JTable table = new JTable(data, columnNames);

    JScrollPane scrollPane = new JScrollPane(table);
    getContentPane().add(scrollPane);

    JPanel buttonPanel = new JPanel();
    getContentPane().add(buttonPanel, BorderLayout.SOUTH);

         KanyisaHoofskerm frame = new KanyisaHoofskerm();
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.pack();
    frame.setTitle("Learners");
    frame.setVisible(true);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Shandré
  • 1
  • 1
  • what is wrong in your code? you have to get the table model and add data to that and set the model back to the table – Thusitha Thilina Dayaratne Jul 31 '14 at 11:01
  • I really don't know exactly what is wrong. I didn't code this myself; I got this code from the internet and adapted it. The error I get is: "java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid descriptor index" but no output on the Jtable at all – Shandré Aug 01 '14 at 10:38

2 Answers2

1

Given this lines:

JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(scrollPane); // your class extends from JFrame?

JPanel buttonPanel = new JPanel();
getContentPane().add(buttonPanel, BorderLayout.SOUTH);

And then this ones:

KanyisaHoofskerm frame = new KanyisaHoofskerm(); // new frame local variable
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
...
frame.setVisible(true);

I strongly suspect your class extends from JFrame which you are adding your table to. If so then you don't make it visible but a new frame local variable instead. Consequently there's not table displayed at all, at least as far as I can see.

If this is the case please take a look to this question with the same problem and Extends JFrame vs. creating it inside the the program topic.

Regarding your code to do database call:

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • Thanks dic19. I'll make the changes you suggested, hopefully it helps because I'm kinda at my wits' end! – Shandré Aug 01 '14 at 10:40
0

Your code gives an Exception when adding row. change your code by using a descriptor instead of using index.

 while (rs.next()) {
        Vector row = new Vector(columns);
        for (int i = 0; i < columns; i++) {
            row.addElement(rs.getObject(columnNames.get(i)));
        }
        data.addElement(row);
    }