2015-02-03 2 views
-2

현재 주문 시스템을 기반으로 한 프로젝트를하고 있습니다.자바에서 날짜가 잘못되었습니다

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

내 주요 :

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

가에 오류가있을 것입니다 :

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 데이터베이스에 연결 : 여기에

는 "주문"에 대한 엔티티 클래스의 주문 생성시 사용 방법 :

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

    } 

내가 잘못 갔다는 아이디어가 있습니까? 귀하의 도움을 부탁드립니다!

+1

OT : '주문'클래스를 '주문'이라고해서는 안됩니까? – Biffen

답변

3

메시지가 분명합니다.

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을 참조하십시오.

1

가져올 Date 클래스가 두 클래스에서 모두 다르게 보입니다. 그것은 당신이

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

는 그런 다음 SQL 데이터베이스에 저장 할 수 있습니다 할 필요가 java.util.Date 첫 번째뿐만 java.sql.Date

관련 문제