I am currently doing on a project based on ordering system.
Here's the entity class for "orders" :
package fourFinger.entity;
import java.util.Date;
import javax.swing.JComboBox;
public class Orders {
private int orderId;
private int tableNo ;
private int numPax;
private Date orderDate;
private double totalAmount;
public Orders( int tableNo, int numPax, Date orderDate, double totalAmount) {
super();
//this.orderId = order Id;
this.tableNo = tableNo;
this.numPax = numPax;
this.orderDate = orderDate;
this.totalAmount = totalAmount;
}
public Orders( int orderId, int tableNo, int numPax, Date orderDate, double totalAmount) {
this(tableNo, numPax, orderDate, totalAmount );
this.orderId = orderId;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getTableNo() {
return tableNo;
}
public void setTableNo(int tableNo) {
this.tableNo = tableNo;
}
public int getNumPax() {
return numPax;
}
public void setNumPax(int numPax) {
this.numPax = numPax;
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public double getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(double totalAmount) {
this.totalAmount = totalAmount;
}
My OrdersDA that connects to database :
public static int createOrders(Orders orders) {
// declare local variables
int orderID ;
DBController db = new DBController();
String dbQuery;
PreparedStatement pstmt;
// step 1 - establish connection to database
db.getConnection();
// step 2 - declare the SQL statement
dbQuery = "INSERT INTO orders (orderId, tableNo, numPax, orderDate , totalAmount) VALUES(?, ?, ?, ? , ?)";
pstmt = (PreparedStatement) db.getPreparedStatement(dbQuery);
orderID = getNextOrderId();
// step 3 - to insert record using executeUpdate method
try {
pstmt.setInt(1,orderID );
pstmt.setInt(2, orders.getTableNo());
pstmt.setInt(3, orders.getNumPax());
pstmt.setDate(4, (Date)orders.getOrderDate());
pstmt.setDouble(5, orders.getTotalAmount());
if (pstmt.executeUpdate() == 1)
return orderID;
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
// step 4 - close connection
db.terminate();
return -1;
}
There will be an error on :
pstmt.setDate(4, (Date)orders.getOrderDate()); and states that java.util.Date cannot be cast to java.sql.Date
My main method when creating orders :
private void actionPerformedOrder() {
//retrieve user input
String numPax = (String) cbNoPax.getSelectedItem();
String tableNo= (String)cb_tableno.getSelectedItem();
Date orderDate = new Date();
orders=newOrders(Integer.parseInt(tableNo),Integer.parseInt(numPax)
,orderDate, totalAmount);
int orderID = OrdersDA.createOrders(orders);
}
Do you have any ideas where have i went wrong? Your help will be appreciated!