如何使用oop概念传递具有不同数据类型的ArrayList对象?

时间:2015-09-14 06:57:09

标签: java oop arraylist

我试图使用ArrayList将值传递给数据库。 然后我得到了例外java.lang.ClassCastException: java.lang.String cannot be cast to model.OrderModel
这是我如何获得用户的价值观 可以someOne请检查这个并帮助我做到这一点

DefaultTableModel dtm = (DefaultTableModel) tblOrder.getModel();
    int numRow = dtm.getRowCount();
    int numCol = dtm.getColumnCount();
  try {

    ArrayList<OrderModel> list = new ArrayList<OrderModel>();    
    for(int l = 0;l<numRow;l++){
        for(int j=0;j<numCol;j++){
          list.add((OrderModel) tblOrder.getValueAt(l, j));//this line may be the problem as i think
        }
    } 

    Order or = new Order();
    or.passingValuesToDB(list);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我在

中的方法
public class Order {
Connection con;
    public void passingValuesToDB(ArrayList<OrderModel> list) {
     try {
        con = new DBconnector().connect();
        String sql = "INSERT INTO order (orderid, customername, item, qty, amount, total) VALUES (?,?,?,?,?,?)";

        PreparedStatement ps = con.prepareStatement(sql);
        for(int i=0;i<list.size();i++){
            ps.setInt(1, list.get(i).getOrderid());
            ps.setString(2, list.get(i).getCustomername());
            ps.setString(3, list.get(i).getItem());
            ps.setDouble(4, list.get(i).getQty());
            ps.setDouble(5, list.get(i).getAmount());
            ps.addBatch();
        }
        ps.executeQuery();
    } catch (SQLException ex) {
        ex.printStackTrace();
    }

    }

我已将所有da getter&amp; Setteres设置为OrderModel类。这是代码

public class OrderModel {
     private int orderid;
     private String customername;
     private String item;
     private double qty;
     private double amount;
     private double total;

      public OrderModel(int orderid,String customername,String item,Double qty,Double amount,Double total){
        this.orderid = orderid;
        this.customername = customername;
        this.item = item;
        this.qty = qty;
        this.amount=amount;
        this.total=total;
    }

    public OrderModel() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    public int getOrderid() {
        return orderid;
    }
    public void setOrderid(int orderid) {
        this.orderid = orderid;
    }
    public String getCustomername() {
        return customername;
    }
    public void setCustomername(String customername) {
        this.customername = customername;
    }
    public String getItem() {
        return item;
    }
    public void setItem(String item) {
        this.item = item;
    }
    public double getQty() {
        return qty;
    }
    public void setQty(double qty) {
        this.qty = qty;
    }
    public double getAmount() {
        return amount;
    }
    public void setAmount(double amount) {
        this.amount = amount;
    }
    public double getTotal() {
        return total;
    }
    public void setTotal(double total) {
        this.total = total;
    }


}

0 个答案:

没有答案