如何使用4层将数据从数据库插入JTable

时间:2014-11-09 08:41:00

标签: java swing jtable derby

这是我遇到的事情

UI:

       jlbl.setText("Purchase ID: ");
   String counterV = String.valueOf(purc.getCounter());

   jTF3.setText("P000"+counterV);
   JPanel p5 = new JPanel();
    p5.add(jlbl);
   p5.add(jTF3);

  else if(e.getSource() == jbtRetrieve)
        {

            String pID = jTF3.getText();
        Purchase purchase = purcControl.selectRecord(pID);
        if (purchase != null) {
            String pdCountStr = purchase.getPurchaseDetailsID();
            String pdCountStr2 = pdCountStr.substring(2,6);
            int pdCount = Integer.parseInt(pdCountStr2);
            for(int g = 0; g<pdCount;g++)
            tableModel.addRow(new Object[]{ purchase.getPurchaseDetailsID(),purchase.getStockID(),purchase.getPrice(),purchase.getQuantity()});

        }
        else {
           JOptionPane.showMessageDialog(null, "No such purchase ID.", "RECORD NOT FOUND", JOptionPane.ERROR_MESSAGE);


        }
        }

控制:

    public Purchase selectRecord(String pID){
    return purcDA.getRecord(pID);
}

DA:

 public Purchase getRecord(String pID){
    String queryStr = "SELECT * FROM " + tableName + " WHERE PurchaseID = ?";
    Purchase purchase = null;
    //MainPurchasingFrame purc = new MainPurchasingFrame();

    try{
        stmt = conn.prepareStatement(queryStr);
        stmt.setString(1,pID);
        ResultSet rs = stmt.executeQuery();



     if(rs.next()){
        purchase = new Purchase(rs.getString("purchaseDetailsID"),pID, ,rs.getString("stockID"),rs.getDouble("price"),rs.getInt("quantity"));
     }
    }   catch (SQLException ex){
        JOptionPane.showMessageDialog(null,ex.getMessage(),"ERROR",JOptionPane.ERROR_MESSAGE);
    }
    return purchase;
}

域:

 public String getPurchaseDetailsID(){

    return purchaseDetailsID ;

}
public double getPrice(){
    return price;
}
public String getStockID(){
    return stockID;
}
public int getQuantity(){
    return quantity;
}
public void setPurchaseDetailsID(String r){
    this.purchaseDetailsID = r ;
}
public void setPrice(double p){
    this.price = p;
}
public void setStockID(String s){
    this.stockID = s;
}
public void setQuantity(int q){
    this.quantity = q;
}

如何通过4层这样将数据输入JTable?当一切都设置在一个但我面对Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException时,我知道怎么做 我每次尝试检索数据。我使用的是netbean。

1 个答案:

答案 0 :(得分:0)

purcControl = new MaintainPurchasingControl();

初始化purcControl,因此不会发生NPE。