java Eclipse平台java如何将db中的数据显示到jtable中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16798789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:01:33  来源:igfitidea点击:

Eclipse platform java how to display data from db into jtable

javamysql

提问by Musa Dlamini

How can I display data from the database into the JTable ? I managed to display the data in the console but IN JTABLE it does not display even the Jtable does not appear on the Jframe yet Isee no error . please any help will be appreciated .

如何将数据库中的数据显示到 JTable 中?我设法在控制台中显示数据,但在 JTABLE 中,即使 Jtable 没有出现在 Jframe 上,它也不显示,但我没有看到错误。请任何帮助将不胜感激。

import java.awt.Dimension;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.*;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class ju {
    private static Object request;
    static JTable mysTable;
    //constructor method

    public static void main (String args []){

        mysTable = new JTable(3,2);
        JScrollPane myPanel = new JScrollPane(mysTable,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                              JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        mysTable.setPreferredScrollableViewportSize(new Dimension(500,70));

        JFrame frame = new JFrame("King Musa Saloon Software");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setSize(500,500);
        frame.setVisible(true);
        frame.add(mysTable);
        //frame.add(mysTable);

        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Driver loading success!");
            String url = "jdbc:mysql://localhost:3306/saloon";
            String name = "root";
            String password = "";

            try {
                java.sql.Connection con = DriverManager.getConnection(url, name, password);
                System.out.println("Connected.");
                // pull data from the database 
                java.sql.Statement stmts = null;
                String query = "select  userid, username, name from saloonuser ";
                stmts = con.createStatement();

                ResultSet rs = stmts.executeQuery(query);
                int li_row = 0;
                while(rs.next()){
                    mysTable.setValueAt(rs.getString("username"),li_row,0);
                    mysTable.setValueAt(rs.getString("name"),li_row,0);
                    int userid = rs.getInt("userid");
                    String username = rs.getString("username");
                    String name1     = rs.getString("name");
                    System.out.println(name1);
                    li_row++;
                }  
            } catch (SQLException e) {
                e.printStackTrace();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }   
    }

}

回答by duffymo

It's so done. Have a look at this:

就这么搞定了。看看这个:

http://www.java2s.com/Code/Java/Swing-JFC/DisplayResultSetinTableJTable.htm

http://www.java2s.com/Code/Java/Swing-JFC/DisplayResultSetinTableJTable.htm

The code you posted is awful. I'd recommend throwing it away and starting from scratch.

您发布的代码很糟糕。我建议扔掉它并从头开始。

Break up the problem. Forget the UI for now. Write a DAO that does nothing but access the database and bring back the data you need. Test it and put it aside. Then start on the UI. Give it an instance of the DAO class and only worry about displaying the data it brings back.

分解问题。暂时忘记 UI。编写一个 DAO,它只访问数据库并带回您需要的数据。测试它并把它放在一边。然后从 UI 开始。给它一个 DAO 类的实例,只关心显示它带回来的数据。