我想从JTable中的MySQL数据库中显示客户详细信息,但是当我单击“显示”按钮时,我在面板中看不到任何结果 .
这是将表添加到JScrollPane的方法:
void addTable()
{
for(int i=0 ; i<myTableModel.getColumnCount();i++)
{
myTableModel.getColumnName(i);
}
showCustomers();
table.setModel(myTableModel);//mytablemodel is a object from MyTableModel Class
scrollPane.add(table); //was on another part of program but i edit it for helping to answers
panel_show.add(scrollPane);
}
这里是 MyTableClass
,使用新方法 addCustomer
实现TableModel . addCustomer
将客户添加到CustomerList . 我第一次尝试使用ArrayList来显示客户数据,但它不起作用 .
此外, columnName
将使用数据库中的列名创建表头 .
public class MyTableModel implements TableModel
{
private ArrayList<Customer> customerList;
private String[] columnName =
{"id", "name", "family", "idc", "age", "sex", "balance", "tel", "haveFamily", "population"};
@Override
public int getRowCount()
{
//return customerList.size();
return 1;
}
@Override
public int getColumnCount()
{
return 10;
}
@Override
public String getColumnName(int columnIndex)
{
return columnName[columnIndex];
}
@Override
public Class<?> getColumnClass(int columnIndex)
{
if(columnIndex == 0)
return Integer.class;
return String.class;
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return false;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex)
{
if(columnIndex == 0)
return customerList.get(rowIndex).getId();
else if(columnIndex == 1)
return customerList.get(rowIndex).getName();
else if(columnIndex == 2)
return customerList.get(rowIndex).getFamily();
else if(columnIndex == 3)
return customerList.get(rowIndex).getIdc();
else if(columnIndex == 4)
return customerList.get(rowIndex).getDate();
else if(columnIndex == 5)
return customerList.get(rowIndex).getSex();
else if(columnIndex == 6)
return customerList.get(rowIndex).getBalance();
else if(columnIndex == 7)
return customerList.get(rowIndex).getTel();
else if(columnIndex == 8)
return customerList.get(rowIndex).isHaveFamily();
else if(columnIndex == 9)
return customerList.get(rowIndex).getPopulation();
else
return null;
}
public void addCustomer(Customer customer)
{
CustomerManager customerManager = new CustomerManager();
customerManager.addCustomer(customer);
customerList.add(customer);
}
showCustomer
方法将打开一个数据库连接,并与客户一起创建一个TableModel,然后将其用于我面板上的表 .
public void showCustomers()
{
Connection conn = null;
try
{
//String user = "root";
// String pass = null;
//String url = "jdbc:mysql://localhost/estate";
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost/estate", "root", null);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * from customer");
//int columnCount = rsmd.getColumnCount();
while (rs.next())
{
myTableModel.addCustomer(new Customer(1,"david","schmidt","0025674433","31","Male","200000","4545552132","true","2");
}//Original Form --> Customer({"id","name","family","idc","age",
// "sex","balance","tel","haveFamily","population"});
}
在 MyTableModel
中,我创建了 addCustomer
方法并使用ResultSet填充客户数据 . 需要它吗?
我的问题:如何使用JTable向我的客户展示?
经过一番摆弄后我现在也遇到了 myTableModel.addCustomer(rs.getInt(), ...)
的问题 . 我得到的错误是这样的:
addCustomer中的错误,KEY PRIMARY的重复项“0”
但是,我没有ID为0的客户 .
1 回答
显示JTable时,首先将表添加到滚动窗格,然后将滚动窗格添加到面板 .
将组件添加到可见GUI时,您需要确保已调用布局管理器,以便组件具有大小和位置 .
所以基本代码是:
另一种方法是创建一个空表,并在首次创建GUI时将滚动窗格添加到GUI . 然后你需要的所有代码是:
编辑:
当您单击按钮时,您的问题是关于在面板中显示表格 . 这就是SSCCE应该做的所有事情 . 数据来源无关紧要,因此发布处理数据库的代码是完全没必要的,因为我们无法执行代码 . 自定义TableModel与真实问题无关 .
这是一个简单的SSCCE,它使用我的第二个建议,用新模型更新现有表 . 每次单击该按钮时,列数都会发生变化 . 这个
simulates
从某个地方获取新数据 .看看代码是多么容易理解?代码完整且在一个类中 . 它可以轻松复制和粘贴,以便其他人可以测试代码 .
当您简化问题时,解决方案通常会更容易 . 这就是为什么你花时间创建一个
SSCCE
. 即使SSCCE没有按照您想要的方式工作,我们也只需要几行代码来了解您尝试做什么 . 没有必要使用SQL代码使问题复杂化 .一旦您获得简单的代码,然后修改
refreshData
方法以获取实际数据 . 这就是硬编码数据的意思 . 无需动态查询数据库即可演示在面板中显示表的问题 .