Search code examples
javadatabaseswingjtabledb4o

Java db4o - fill jTable


I'm developing an application that has a db4o file database but now i'm stuck, because I need to fill a Swing JTable with information from database file, and I can't figure it out.

With SQL code you use a setModelFromResultSet method to update data in table, but what about db4o?...

I have a Native Query returning an ObjectSet. Can I use it to fill the table?... Or convert it to a ResultSet?... Or maybe an array?

Help me out here, i'm running out of ideas.

Thanks in advance, code follows...

public void showAll() {
    String DB4OFILENAME = "C:/cemiGEST/Databases/PessoasDB.cmg";

    File folder = new File("C:/cemiGEST/Databases/");
    if (!folder.exists()) {
        folder.mkdirs();
    }

    Db4oEmbedded.newConfiguration().common().messageLevel(0);
    ObjectContainer db = Db4oEmbedded.openFile(
            Db4oEmbedded.newConfiguration(), DB4OFILENAME);

    try {
        // Native Query (NQ)
        ObjectSet<Pessoa> tudo = db.query(new Predicate<Pessoa>() {
            public boolean match(Pessoa pessoa) {
                return true;
            }
        });
        // shows NQ result
        for (Pessoa pessoa : tudo) {
            System.out
                    .println("\n----------------------------------------");
            System.out.println(pessoa);
        }

    } finally {
        db.close();
    }
}

EDIT 1: As I understand db4o doesn't have the notion of tables, so I found a workaround to fill a table from the ObjectSet. It's working, but the database file should have thousands (or maybe millions) of rows, which will definitely cause a (very) big performance issue upon populating the table row by row, which is not an option for me. The performance was the main factor that led me to drop HSQL and have db4o a try.

Any ideas for my next move?

code to populate table follows...

public void populateTableFromObjectSet(ObjectSet<Pessoa> os) {
        DefaultTableModel table = (DefaultTableModel) tblDB.getModel();

        int nPessoas = os.size();
        int nPes = 0;
        for (int i = 0; i < nPessoas; i++) {
            table.setValueAt(os.get(i).getIdPessoa(), nPes, 0);
            table.setValueAt(os.get(i).getNome(), nPes, 1);
            table.setValueAt(os.get(i).getApelidos(), nPes, 2);
            table.setValueAt(os.get(i).getDataM(), nPes, 3);
            table.setValueAt(os.get(i).getECivil(), nPes, 4);
            table.setValueAt(os.get(i).getIdade(), nPes, 5);
            table.setValueAt(os.get(i).getPai(), nPes, 6);
            table.setValueAt(os.get(i).getMae(), nPes, 7);
            table.setValueAt(os.get(i).getNaturalidade(), nPes, 8);
            table.setValueAt(os.get(i).getMorada(), nPes, 9);
            table.setValueAt(os.get(i).getSexo(), nPes, 10);
            table.setValueAt(os.get(i).getIDSepultura(), nPes, 11);
            nPes++;
            table.addRow(new Object[] { null, null, null, null, null, null,
                    null, null, null, null, null, null });
        }
    }

Solution

  • With SQL code you use a setModelFromResultSet method to update data in table,

    The setModelFromResultSet(), is just a method that somebody has written to read the data from the ResultSet.

    but what about db4o?...

    You need to write your own method. I have never used that class so I have no idea what methods are available but I'm sure the methods are similar to when using a real database.

    You already have a for loop so I'm guess that is for each row of data that is returned. So now you need to figure out how to access the data in each column for the given row. Then you can create the TableModel to be added to the table.

    Maybe the TableFromDatabaseExample.java from Table From Database will help you out. It shows you how to structure the code with two loops to get the row and column data by creating Vectors. You should be able to modify the code for your purposes.