JAVA - 将mysql结果放在表中

时间:2013-06-18 19:05:48

标签: java mysql

我是Java的新手,我正在尝试创建一个带有表视图的窗口。在这个窗口中,我想删除,更新和插入数据等。

最后它看起来像这样:

enter image description here

我从来没有在任何人提示或提供良好教程之前做过这个。我不是要求你为我编码,而是要学习如何创建一个动态表,我可以在一个动作执行后重新加载。我找不到任何关于此的好教程。我的Java书也没有给出好的例子。

在PHP中,我可以在5分钟内完成,但在JAVA中,我不知道!无论如何,我会试着用我的知识弄清楚如何做到这一点。

我现在有了这个Java代码:

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.ResultSet;
import java.sql.SQLException;

public class test extends JPanel {
    private boolean DEBUG = false;

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

        Database db = new Database();

        String tabel = "testtabel";

        ResultSet res = db.query("SELECT COUNT( * ) FROM `"+tabel+"`");
        ResultSet res2 = db.query("SELECT COUNT( * ) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '"+tabel+"'");

        int rows = 0;
        int collums = 0;

        try {
            res.next();
            res2.next();

            rows = res.getInt(1);
            collums = res2.getInt(1);

        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        ResultSet clom = db.query("DESCRIBE  `"+tabel+"`");

        String[] columnNames = new String[collums];

        int s = 0;

        try {
            while(clom.next()){
                columnNames[s] = clom.getString(1);
                s++;
            }
        } catch (SQLException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        Object[][] data = new Object[rows][collums];

        ResultSet result = db.query("SELECT * FROM `"+tabel+"`");

        int q = 0;
        try {
            while(result.next()){
                for(int a=0; a<= (collums - 1); a++){
                    data[q][a] = result.getString(a + 1);
                    System.out.println(q + " - " + a);
                }
                q++;
            }
        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        final JTable table = new JTable(data, columnNames);
        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.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("SimpleTableDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        test newContentPane = new test();
        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();
            }
        });
    }
}

如何使这更简单?

1 个答案:

答案 0 :(得分:1)

很高兴你想学习一门新语言。 Oracle文档页面非常有用。这个链接真的有助于你想要做的事情,祝你好运! JTable Documentation

This Image is included with the documentation