我似乎无法将数据加载到表中,尽管能够动态加载列名。当我尝试加载数据时,尽管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并关闭连接和语句,但是,同样的错误仍然存在
答案 0 :(得分:0)
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方法解决了这个问题。