gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:16:03 24 4
gpt4 key购买 nike

我正在尝试使用 ArrayList 将值传递到数据库。然后我得到一个异常java.lang.ClassCastException:java.lang.String无法转换为model.OrderModel
这是我如何获取用户的值有人可以检查一下并帮助我做到这一点

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();
}

}

我已将所有 getters&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;
}


}

最佳答案

(OrderModel) tblOrder.getValueAt(l, j)是问题所在

ValueAt(l, j)返回一个字符串(表列数据类型)。您无法强制转换 StringOrderModel对象它们是完全不同的对象。你应该创建一个 OrderModel使用行列值的对象。您可以使用 setter 或直接传递给构造函数。然后您可以将其添加到您的 ArrayList<OrderModel> list

ArrayList<OrderModel> list = new ArrayList<OrderModel>();

for (int l = 0; l < numRow; l++) {

int orderid = Integer.parseInt(tblOrder.getValueAt(l, 0).toString());
String customername = tblOrder.getValueAt(l, 1).toString();
String item = tblOrder.getValueAt(l, 2).toString();
double qty = Double.parseDouble(tblOrder.getValueAt(l, 3).toString());
double amount = Double.parseDouble(tblOrder.getValueAt(l, 4).toString());
double total = Double.parseDouble(tblOrder.getValueAt(l, 5).toString());
// OrderModel object
OrderModel ormod1 = new (orderid, customername, item, bla..bla..);
//then add OrderModel to the list
list.add(ormod1);

}

关于java - 如何使用oop概念传递不同数据类型的ArrayList对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32558819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com