如何在JTable中显示来自MySQL的数据

时间:2017-08-26 14:26:38

标签: java mysql swing jtable

我想在JTable中显示来自MySQL的数据,但是显示了表中的最后一行,而没有更多。请帮帮我。我知道我有一个问题,因为jt = new JTable(数据,列)每次为每一行创建一个新表(删除前一个),但我找不到合适的选项。

public class Test2  extends JPanel {

    static final String USERNAME = "root";
    static final String PASSWORD = "root";
    static final String CONN_STRING = "jdbc:mysql://localhost:3306/mydbtest?useSSL=false";

    JTable jt;
    public Test2 () {

        try {
            Connection conn;
            conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
            Statement stmt = (Statement) conn.createStatement();
            String query = "Select title, season, episode from movie";
            ResultSet rs = stmt.executeQuery(query);

            rs.beforeFirst();

            while (rs.next()) {
                String title = rs.getString("Title");
                String season = rs.getString("Season");
                String episode = rs.getString("Episode");

            String[] columns = {"Title", "S", "E"};
            String[][] data = {{title, season, episode}};


            jt = new JTable(data, columns);
            };

            jt.setPreferredScrollableViewportSize(new Dimension(450, 63));
            jt.setFillsViewportHeight(true);

            JScrollPane jps = new JScrollPane(jt);
            add(jps);
        }
        catch (Exception er) {System.err.println(er);}
    }

    public static void main(String[] args) {
        JFrame jf = new JFrame();
        Test2 t = new Test2();
        jf.setTitle("Test");
        jf.setSize(500,500);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(t);
    }
}

2 个答案:

答案 0 :(得分:2)

你的问题在这里:

while (rs.next()) {
    String title = rs.getString("Title");
    String season = rs.getString("Season");
    String episode = rs.getString("Episode");

    String[] columns = { "Title", "S", "E" };
    String[][] data = { { title, season, episode } };

    jt = new JTable(data, columns); // *** you're making many JTables here!! ***
}

每次while循环循环时,您都会创建 discard 一个新的JTable对象。例外情况是最后一次循环,结果集最后一行的数据不会被丢弃,然后显示在最终创建的JTable中。要解决这个问题,要在JTable中显示所有数据,您需要整理while循环中的所有结果集数据,然后将其添加到JTable中,这是最简单的方法这是创建一个表模型,这里是一个简单的DefaultTableModel,之前的 while循环,并在while循环中的结果集数据的每一行中填充它:

// create a table model with the appropriate column headers
// and with 0 rows (to start with)
String[] columnNames = {"Title", "Season", "Episode"};
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);

while (rs.next()) {
    String title = rs.getString("Title");
    String season = rs.getString("Season");
    String episode = rs.getString("Episode");

    // create a single array of one row's worth of data
    String[] data = { title, season, episode } ;

    // and add this row of data into the table model
    tableModel.addRow(data);
}

jt.setModel(tableModel); // place model into JTable

或者更好,将最后一行更改为:

jt = new JTable(tableModel); // to create a new JTable

答案 1 :(得分:0)

尝试一下...您可以轻松实现。...

Text(
   "Forgot Password?",
   maxLines: 1,
),