当前位置: 首页 > 知识库问答 >
问题:

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

段干茂实
2023-03-14

我想在JTable中显示MySQL中的数据,但只显示了表中的最后一行。请帮帮我。我知道我有一个问题,因为jt=newjtable(数据,列)每次都为每行创建一个新表(删除之前的),但我找不到正确的选项。

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个答案

祁宝
2023-03-14

试试这个……你可以很容易做到。。。。

public final void EmployeeGridView(){

    try{

        Connection conn = DBConn.connect();
        PreparedStatement ps = conn.prepareStatement("Select * from empdetails");
        ResultSet rs=ps.executeQuery();
        DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
        tm.setRowCount(0);

        while(rs.next()){

            Object o[] = {rs.getInt("EMPID"),rs.getString("FirstName"),rs.getString("LastName"),rs.getString("Designation"),rs.getString("NIC"),rs.getString("PhoneNO"),rs.getString("DOB"),rs.getString("Address"),rs.getString("Gender")};
            tm.addRow(o);



        }


    }
    catch(Exception e){

        JOptionPane.showMessageDialog(null,"Error in Employee Grid View..... "+e);
    }



}
郑功
2023-03-14

您的问题就在这里:

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!! ***
}

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

// 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
 类似资料: