【问题标题】:How to add data from a DB to jTable in Java Swing when tables contain null values using joins?java - 当表使用连接包含空值时,如何在Java Swing中将数据从数据库添加到jTable?
【发布时间】:2016-09-06 19:29:56
【问题描述】:

好的,所以我有一个方法可以从数据库中获取数据并将其放入这样的列表中:

public List<Sale> getSales() throws SQLException{
    List<Sale> res = new ArrayList<Sale>();
    Statement st = conn.createStatement(); 
    ResultSet rs = st.executeQuery("select sale.id,product.name,sale.price,sale.quantity,sale.total,employee.name,buyer_seller.name from sale "
            + "                     join product on sale.product = product.id "
            + "                     join employee on sale.salesman = employee.id "
            + "                     join buyer_seller on sale.buyer = buyer_seller.id;");
    while(rs.next()){
        res.add(new Sale (rs.getInt(1),new Product(-1,rs.getString(2),new Category(-1,"",""),new Manufacturer(-1,"",""),0.0,-1),rs.getDouble(3),rs.getInt(4),rs.getDouble(5),new Employee(-1,rs.getString(6),"","",new JobCategory(-1,"",""),"",""),new BuyerSeller(-1,rs.getString(7),"","","")));
    }  
    return res;
}

然后我有一个用该数据填充 jTable 的方法:

public void fillSales(){
    jLabel1.setText("Podaci se ucitavaju iz baze..."); 
    SwingWorker sw = new SwingWorker() { 
        @Override
        protected Object doInBackground() throws Exception {
            MyShopRepository repo = MyShopRepository.getInstance();
            List<Sale> allSale = repo.getSales();
           return allSale;
        } 
        @Override
        protected void done() {
            super.done();
            try {
                List<Sale> res = (List<Sale>)get();
                DefaultTableModel dtm = (DefaultTableModel)jTable1.getModel(); 
                int redova = dtm.getRowCount();
                for(int i=0;i<redova;i++){
                    dtm.removeRow(0);
                } 
                for(Sale s : res){
                    dtm.addRow(new Object[]{
                        s.id,s.product.name,s.price,s.quantity,s.total,s.salesman.name,s.buyer.name
                    });
                } 
                jLabel1.setVisible(false);
            } catch (InterruptedException | ExecutionException ex) {  }
        } 
    };
    sw.execute();
}

这工作得很好,但是当我从表中删除一些行时,它们被设置为 null 并且我没有让它们显示在 jTable 中。如果它的值为 null ,我怎样才能将它们也添加到只有一个空单元格的 jTable 中?

【问题讨论】:

  • 当我从具有 FK 的 DB 表中删除一行到另一个表时,我看不到 jTable 中的行,该行对于具有 fk 的列的值为 null,我使用 ON DELETE SET空。

标签: java mysql swing


【解决方案1】:

在数据库方面,如果您因为尝试跨表连接空值而没有看到行,则使用LEFT JOIN 而不是JOIN

在 UI 方面,DefaultTableCellRenderer 将为任何 null 对象返回一个空字符串。如果您仍然在JTable 中看到null,那么这很可能是您的数据库以字符串值“null”的形式返回的。要绕过空字符串值,您可以扩展 DefaultTableCellRenderer 并覆盖 setValue 方法以考虑“空”字符串值。例如:

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){
    @Override
    protected void setValue(Object value) {
        setText((value == null || value.toString().equalsIgnoreCase("null")) ? "" : value.toString());
    } 
};
table.setDefaultRenderer(Object.class, renderer);

【讨论】:

  • 如果该行的某些列在数据库中具有空值,我在 jTable 中看不到任何内容,并且我确实希望它们的行可见并且它们的列只显示为空..
  • 我有这个:postimg.org/image/rj1z6atib,列“Radno mesto”也是另一个用 FK 连接到这个表的表:postimg.org/image/73as5etgt,当我从表“Radno mesto”中删除“sef”时“(第二个链接)我在第一个表中看不到任何东西(第一个链接)
  • i do want to have them rows visible and theirs columns just displayed empty 使用left join
猜你喜欢
  • 2023-03-30
  • 1970-01-01
  • 2013-01-19
  • 2017-11-08
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
相关资源
最近更新 更多