繁体   English   中英

JTable无法从数据库加载数据

[英]Jtable cant load data from database

尽管能够动态加载列名,但我似乎无法将数据加载到表中。 当我尝试加载数据时,尽管Java Icon仍显示在任务栏上,却什么也没出现。 看起来GUI本身没有任何问题,所以我想知道从构造函数打开连接是否会导致逻辑错误。我在populateTable方法的第二个for循环中打印了locals变量,以检查是否将数据传递到本地变量,但确实如此,所以我不知道是什么导致了此错误。

这是GUI的代码:

public class WeatherFrame extends JFrame {

    private JPanel contentPane;
    private JTable table;
    HealthData health = new HealthData();
    private DefaultTableModel model;
    String[] columnNames = {"zipcode", "county", "city", "state", "year", "month","ageGroup",
                            "numOfVisits", "MonthlyMax", "MonthlyMin", "MonthlyNor"};

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    WeatherFrame frame = new WeatherFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public WeatherFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 300);
        contentPane = new JPanel();
        contentPane.setBounds(100, 100,750, 200);
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(6, 25, 788, 180);
        contentPane.add(scrollPane);

        model = new DefaultTableModel();
        populateTable();
        table = new JTable(model);

        scrollPane.setViewportView(table);

        JButton btnInsert = new JButton("insert");
        btnInsert.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnInsert.setBounds(315, 217, 117, 29);
        contentPane.add(btnInsert);

        JButton btnDelete = new JButton("delete");
        btnDelete.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnDelete.setBounds(198, 243, 117, 29);
        contentPane.add(btnDelete);

        JButton btnSearch = new JButton("search");
        btnSearch.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnSearch.setBounds(81, 243, 117, 29);
        contentPane.add(btnSearch);

        JLabel lblWeatherTable = new JLabel("Weather Table");
        lblWeatherTable.setBounds(149, 6, 107, 16);
        contentPane.add(lblWeatherTable);

        JButton btnNext = new JButton("update");
        btnNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnNext.setBounds(198, 217, 117, 29);
        contentPane.add(btnNext);

        JButton btnRefresh = new JButton("refresh");
        btnRefresh.setBounds(81, 217, 117, 29);
        contentPane.add(btnRefresh);
    }

    public void populateTable() {

        for(String name: columnNames)
            model.addColumn(name);

        ArrayList<Health> healthdata = new ArrayList<Health>();
        healthdata = health.showAllData();                                  
        model.addRow(healthdata.toArray()); 
    }
}

这是HealthData类的构造函数

public HealthData(){
        try {
            /* Connect to database */
            connection = DriverManager.getConnection(jdbcURL, MySQLConfig.user, MySQLConfig.password);
            statement = connection.createStatement();
           data = new ArrayList<Health>();
        } catch (Exception e) 
        {
            e.printStackTrace();
        }

    }

这是showAlldata方法的代码

public ArrayList<Health> showAllData(){
        ArrayList<Health> list = new ArrayList<Health>();
        try {
            connection = DriverManager.getConnection(jdbcURL, MySQLConfig.user, MySQLConfig.password);
            Statement stmt = connection.createStatement();
            ResultSet result = stmt.executeQuery(SELECT_ALL_QUERY);


            /* Get and print out data from health table : zipcode, county, city, state, year, month, ageGroup, numberOfVisits, MMax, MMin, MNor */
            while(result.next()){
                Health data = new Health();
                int zipcode = result.getInt(1);
                data.setZipCode(zipcode);
                String county = result.getString(2);
                data.setCounty(county);
                String city = result.getString(3);
                data.setCity(city);
                String state = result.getString(4);
                data.setState(state);
                int year = result.getInt(5);
                data.setYear(year);
                int month = result.getInt(6);
                data.setMonth(month);
                String ageGroup = result.getString(7);
                data.setAgeGroup(ageGroup);
                int numOfVisits = result.getInt(8);
                data.setNumOfVisits(numOfVisits);
                float MMax = result.getFloat(9);
                data.setMMax(MMax);
                float MMin = result.getFloat(10);
                data.setMMin(MMin);
                float MNor = result.getFloat(11);
                data.setMNor(MNor);
                list.add(data);     
                connection.close()
                stmt.close();   
            }   

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }

有人可以提出解决此问题的解决方案。 预先感谢您的帮助。 我已经发布了一个与此有关的问题,但是这个问题是最新的必需代码。

我编辑代码以仅对showAlldata进行一次调用,并关闭连接和语句,但是,相同的错误仍然存​​在

public void populateTable() {

        model = new DefaultTableModel(){

            @Override
            public boolean isCellEditable(int row, int column) {
               //all cells false
               return false;
            }
        };

        for(String name: columnNames)
            model.addColumn(name);
        ArrayList<Health> temp = new ArrayList<Health>();
        temp = health.showAllData();
        for(int i = 0; i< temp.size(); i++) {
            Object[] data = {temp.get(i).getZipCode(), temp.get(i).getCounty(), temp.get(i).getCounty(), temp.get(i).getState(),temp.get(i).getYear(),
                             temp.get(i).getMonth(), temp.get(i).getAgeGroup(), temp.get(i).getNumOfVisits(), temp.get(i).getMMax(), temp.get(i).getMMin(), temp.get(i).getMNor()};
            model.addRow(data);
        }
    }

非常感谢您的帮助,我通过如上所述修改populateTable方法解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM