【问题标题】:Having error with date in java [duplicate]java中的日期有错误[重复]
【发布时间】:2015-04-02 20:45:20
【问题描述】:

我目前正在做一个基于订购系统的项目。

这是“订单”的实体类:

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

我的 OrdersDA 连接到数据库:

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

会有一个错误:

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

我创建订单时的主要方法:

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

    }

你有什么想法我哪里出错了吗?您的帮助将不胜感激!

【问题讨论】:

  • OT:不应该将Orders 类称为Order吗?

标签: java date


【解决方案1】:

信息很清楚。

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

您有一个对象java.util.Date,但preparedStatement 需要一个java.sql.Date。所以你必须转换它。

this question怎么做。

【讨论】:

    【解决方案2】:

    看来您要导入的 Date 类在两个类中都不同。第一个是 java.util.Date 而 OrdersDA 类是 java.sql.Date

    【讨论】:

      【解决方案3】:

      你需要做的

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

      然后您就可以将其保存到 SQL 数据库中了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多