Im trying to passing values to database using ArrayList. then I got a exception java.lang.ClassCastException: java.lang.String cannot be cast to model.OrderModel
Here's How i get values by user Can someOne please check this and help me to do that
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();
}
}
and this is my method in
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();
}
}
And i have set all da getters&Setteres to OrderModel class. here's that code
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)
is the problem
ValueAt(l, j)
return a String (table column data types) .you cannot cast String
to a OrderModel
object they are completely different objects. you should create a OrderModel
object using row column values.you can either use setters or directly pass in to the constructor. then you can add it to your 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.