Reshad
Reshad

Reputation: 2652

getting the database info in a Swing table as output

I am new to Java and I am practicing some new stuff.. I've started working with the database. therefore I made a to do list application with the MVC pattern.

In my Model I get all the results. In My view I try to output this data as a nice table. The problem is that I don't get any output except for a hardcoded piece of code..

here is the code of my view

JTable table = null;

public ToDoListView(ToDoListModel model) {
    this.model = model;

    setBackground(Color.WHITE);

    JTable table = new JTable();

    DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{},new String[]{"To do","Date added"});

    table.setModel(tableModel);

    // this one below is outputted
    tableModel.addRow(new Object[]{"something","1-1-2012"});

    // this should give me all the results.. 
    for(int i = 0; i < model.getRows().size(); i++) {
        tableModel.addRow(model.getRows());
        System.out.println("added");
    }

    add(table);

}

in my Model I have this

private Vector<String> rijen = new Vector<String>();

    public void getValue() {
    Connection con = null;
    Statement st = null;
    ResultSet rs = null;
     try {

        con = db.connectToAndQueryDatabase("test", "root", "root");
        System.out.println("connection established");

        st = con.createStatement();
        String query = "SELECT id, item, datum FROM toDoList";
        rs = st.executeQuery(query);

        while(rs.next()) {
            System.out.println(rs.getInt("id") + "\n" + rs.getString("item") + "\n" + rs.getDate("datum"));
            rijen.add(rs.getInt("id") + "");
            rijen.add(rs.getString("item"));
            rijen.add(rs.getDate("datum") + "");
        }

    public Vector<String> getRows() {
    return rijen;
}

This is all the relevant code.. I don't know what I miss or what I do wrong. Could someone show me how I could solve it :)?

Upvotes: 0

Views: 1166

Answers (2)

Robin
Robin

Reputation: 36611

In your ToDoModel class you add all data in one large Vector

    while(rs.next()) {
        System.out.println(rs.getInt("id") + "\n" + rs.getString("item") + "\n" + rs.getDate("datum"));
        rijen.add(rs.getInt("id") + "");
        rijen.add(rs.getString("item"));
        rijen.add(rs.getDate("datum") + "");
    }

Then you loop over that Vector to add all those items to the TableModel, but that loop is incorrect

for(int i = 0; i < model.getRows().size(); i++) {
    tableModel.addRow(model.getRows());
    System.out.println("added");
}

You always add the whole vector instead of just the data for that row.

Combine that with the answer of @user unknown and you might be able to fix your problem

Upvotes: 2

user unknown
user unknown

Reputation: 36229

// This JTable attribut ...
JTable table = null;

public ToDoListView (ToDoListModel model) {
    this.model = model;
    setBackground (Color.WHITE);

    // is hidden by this local variable: 
    JTable table = new JTable();

Upvotes: 2

Related Questions