-2

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!

Jens
  • 67,715
  • 15
  • 98
  • 113
Ng Wei Ze
  • 1
  • 4

3 Answers3

3

The message is pretty clear.

pstmt.setDate(4, (Date)orders.getOrderDate()); and states that java.util.Date cannot be cast to java.sql.Date

You have an Object java.util.Date but preparedStatement Needs a java.sql.Date. So you have to convert it.

See this question how to do it.

Community
  • 1
  • 1
Jens
  • 67,715
  • 15
  • 98
  • 113
1

It seems your Date class which you are importing are different in both the classes. it is java.util.Date in the first one but java.sql.Date in OrdersDA class

sumitsabhnani
  • 320
  • 1
  • 6
0

You need to do

java.util.Date yourDate;
java.sql.Date sqlDate = new java.sql.Date(yourDate.getTime());

Then you will be able to save it into the SQL database.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428