Mapping data from a database to JTable

I am writing a program using jdbc that will be the interface to the database (smth, like CRUD). I assume that I need to write a class (for example, DBCLass) that will perform all operations with the database ( select, update, insert, deleteand, possibly, other logic that will be reduced to these operations). The user interface consists of a set of tables and several buttons. To use Jtable, I need to implement a class (e.g.Model), which is a subclass of AbstractTableModel. Thus, this class will display my data to the user. I need to implement such a model for all tables in my database schema. I do not want to write logic in those classes that display data to the user, and I think it is not very good to write logical code in such classes. But it is also incorrect to load all the data from the db table into memory (e.g. ArrayList), and then display them in Model. So, I want advice, which is the best way to solve such a problem.

edit A small example:

Statement stmt = ....;
ResaultSet rs = stmt.executeQuery("SELECT * FROM table1");

javadoc says the executeQuery method returns a ResultSet object containing the data received by this query. So, if we have a lot of data (the size of which is larger than the allowed size for our virtual machine), our program will fail. So my question is still relevant

+3
source share
6 answers

Download the SQuirreL SQl source and look at the table implementation.

Some notes:

Java JTables. ( , ) , ( "", JColumn, , ).

. . , : union, difference, sub set.

, , UI.

, "" "".

  • N .

  • ,

  • . , .

+ :

1 , , . , , ? , 15 . ... ... ... ! ! ! , , ... ... ... aaargh.

. .

2 , ResultSet . 100% . , . ", , " , , .

, . , , . : , , . , ( , , 15 0,0000000001).

3 , # 2. , : , . . .

, :

  • 1000 , . , 100 ( ), 1 . , , .

  • "", SQL. , , , , ( , ).

.

+4

.

+3

, : JTable

0

DBTable QuickTable.

SO .

0

, .

See table = new JTable (rows (), columns ());

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.print.PrinterException;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import com.swtdesigner.SwingResourceManager;
import java.util.*;
import java.sql.*;
import javax.swing.*;
public class listing extends JDialog {


private JTable table;
public static Vector rows() {

    Vector data = new Vector();
    String sql= null;
    Connection C;


        try {

            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            C = (Connection) DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE",
                    "system", "manager");

            Statement stmt = C.createStatement();
            ResultSet rset = stmt.executeQuery("SELECT * FROM site ");
            ResultSetMetaData md = rset.getMetaData();
            int columns = md.getColumnCount();



            while (rset.next()) {
                Vector row = new Vector(columns);

                for (int i = 1; i <= columns; i++) {
                    row.addElement(rset.getObject(i));
                }

                data.addElement(row);

            }

            rset.close();
            stmt.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getStackTrace());
        }

        //System.out.println("lignes :  "+data);
        return data;
}


   public static Vector columns()
    {   Connection c ;
        Vector cols = new Vector ();
        String sql2=null;
        try { 
            DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
            c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE",
                    "system", "manager");

            sql2 = "select * from SITE";

            Statement stmt = c.createStatement();
            ResultSet rs = stmt.executeQuery(sql2);
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();

              //récupération des noms des colonnes dans la table site

            for (int i = 1; i <= columns; i++) {
            cols.addElement(md.getColumnName(i));
            }
           } catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println(e.getStackTrace());
        }
        //System.out.println("colonnes ::: "+cols);
        return cols;
    }

public static void main(String args[]) {
    try {
        listing dialog = new listing();
        dialog.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        dialog.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public listing() {
    super();
    getContentPane().setLayout(null);
    setTitle("Listing de la table \"SITE\"");
    setBounds(100, 100, 500, 363);
    setResizable(false);

    final JLabel laTablesiteLabel = new JLabel();
    laTablesiteLabel.setText("La table \"SITE\" contient . . . ");
    laTablesiteLabel.setBounds(10, 34, 274, 16);
    getContentPane().add(laTablesiteLabel);

    final JLabel label = new JLabel();
    label.setIcon(SwingResourceManager.getIcon(listing.class, "/pictures/130.png"));
    label.setBounds(402, 18, 49, 48);
    getContentPane().add(label);

    final JButton okButton = new JButton();
    okButton.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent arg0) {
            dispose();
            setVisible(false);
        }


    });
    okButton.setIcon(SwingResourceManager.getIcon(listing.class, "/pictures/33-32x32.png"));
    okButton.setText("Ok");
    okButton.setBounds(10, 291, 148, 32);
    getContentPane().add(okButton);

    final JButton refeshButton_1 = new JButton();
    refeshButton_1.setIcon(SwingResourceManager.getIcon(listing.class, "/pictures/48-32x32.png"));
    refeshButton_1.setText("Actualiser");
    refeshButton_1.setBounds(171, 291, 148, 32);
    getContentPane().add(refeshButton_1);

    final JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(10, 85, 474, 187);
    getContentPane().add(scrollPane);

    table = new JTable(rows(), columns()); // chargement de JTable 
    scrollPane.setViewportView(table);

    final JButton printButton_1_1 = new JButton();
    printButton_1_1.addActionListener(new ActionListener() {
        public void actionPerformed(final ActionEvent arg0) {
            try {
                table.print();
            } catch (PrinterException e) {

                e.printStackTrace();
            }
        }
    });
    printButton_1_1.setIcon(SwingResourceManager.getIcon(listing.class, "/pictures/Printer_h.png"));
    printButton_1_1.setText("Imprimer");
    printButton_1_1.setBounds(336, 291, 148, 32);
    getContentPane().add(printButton_1_1);




    }

}
-1
source

All Articles