将数据从数据库传输到JTable

时间:2013-09-20 16:08:19

标签: java database swing jtable

我已经完成了搜索,但我无法得出如何做到这一点的好结论。 我在数据库中有一个表,我想将它传输到JTable(稍后我想编辑一行或多行并将其发送回数据库)。 根据我的理解,我可以通过将数据转移为: ,多维数组 ,向量 或ResultSetTableModel。 我应该使用其中之一吗?如果以后我想编辑JTable并将其发送回数据库,哪一个最有效?

- 谢谢你,我。

1 个答案:

答案 0 :(得分:1)

这是一个简单的示例,您可以根据自己的需要进行修改。它使用vector来存储从数据库中检索的数据,然后在JTable中显示它。

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

public class DisplayResult extends JPanel {
    private boolean DEBUG = false;
        // JDBC driver name and database URL
    static final String JDBC_DRIVER = "org.postgresql.Driver"; // use specific driver for your database
    static final String DB_URL = "jdbc:postgresql://127.0.0.1:8000/testdb";
    // Database credentials
    static final String USER = "username";
    static final String PASS = "password";
    Connection conn = null;
    Statement stmt = null;
    static Vector<Vector<String>> data = new Vector<Vector<String>>();

    public DisplayResult() {
        super(new GridLayout(1,0));


        Vector<String> columnNames = new Vector<String>();
         columnNames.add("column1");
        columnNames.add("column2");
        columnNames.add("column3");


      String query =
                "select column1, column2, column3 from MYTABLE";


        try {
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {

                Vector<String> vstring = new Vector<String>();


                 vstring.add(rs.getString("column1"));
                vstring.add(rs.getString("column2"));
                vstring.add(rs.getString("column3"));
                 vstring.add("\n\n\n\n\n\n\n");

                data.add(vstring);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException ex) {
                    Logger.getLogger(FlashBuilderGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

      //  final JTable table = new JTable(data, columnNames);
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        final JTable table = new JTable(model);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        if (DEBUG) {
            table.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    printDebugData(table);
                }
            });
        }

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }

    private void printDebugData(JTable table) {
        int numRows = table.getRowCount();
        int numCols = table.getColumnCount();
        javax.swing.table.TableModel model = table.getModel();

        System.out.println("Value of data: ");
        for (int i=0; i < numRows; i++) {
            System.out.print("    row " + i + ":");
            for (int j=0; j < numCols; j++) {
                System.out.print("  " + model.getValueAt(i, j));
            }
            System.out.println();
        }
        System.out.println("--------------------------");
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    public static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Database Data");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        DisplayResult newContentPane = new DisplayResult();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

   public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
       //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               createAndShowGUI();
            }
       });
    }
}