【问题标题】:JAVA Date Exception?JAVA日期异常?
【发布时间】:2014-01-12 20:08:56
【问题描述】:

好的,所以我有一个名为 DepositsBean 的类

package Beans;

import java.util.Date;

public class DepositsBean {
private long deposit_id;
private long client_id;
private double balance;
public enum Type {SHORT,LONG}
private long estimated_balance;
private java.sql.Date opening_date;
private java.sql.Date closing_date;
public DepositsBean() {
    super();
}
public Type type;


public long getDeposit_id() {
    return deposit_id;
}
public Type getType() {
    return type;
}
public void setType(Type type) {
    this.type = type;
}
public void setDeposit_id(long deposit_id) {
    this.deposit_id = deposit_id;
}
public long getClient_id() {
    return client_id;
}
public void setClient_id(long client_id) {
    this.client_id = client_id;
}
public double getBalance() {
    return balance;
}
public void setBalance(double balance) {
    this.balance = balance;
}
public long getEstimated_balance() {
    return estimated_balance;
}
public void setEstimated_balance(long estimated_balance) {
    this.estimated_balance = estimated_balance;
}
public java.sql.Date getOpening_date() {
    return opening_date;
}
public void setOpening_date(Date opening_date) {
    this.opening_date = (java.sql.Date) opening_date;
}
public java.sql.Date getClosing_date() {
    return closing_date;
}
public void setClosing_date(Date closing_date) {
    this.closing_date = (java.sql.Date) closing_date;
}

}

找到他的经理:

package Managers;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;

import Beans.DepositsBean;
import Beans.DepositsBean.Type;

public class DepositsManager implements IDepositsManager {
private ConnectionPool connect;

public DepositsManager(ConnectionPool connect) {
    this.connect = connect;
}

@Override
public void createNewDeposit(DepositsBean deposit,long clientId,double balance) {

    try {
        PreparedStatement ps = connect.connectionCheck().prepareStatement(
                "INSERT INTO mbank.deposits VALUES"+
                "(?,?,?,?,?,?,?)");
        ps.setLong(1, deposit.getDeposit_id());
        ps.setLong(2,clientId);
        ps.setDouble(3, balance);
        String type= "";
        if (deposit.getType()==Type.SHORT) {
            type = Type.SHORT.name();
        } else {
            type = Type.LONG.name();
        }
        ps.setString(4, type);
        ps.setLong(5, deposit.getEstimated_balance());
        ps.setDate(6,deposit.getOpening_date());
        ps.setDate(7, (Date) deposit.getClosing_date());
        ps.execute();   


    } catch (SQLException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null,"Problem occurs during creation
                    new deposit");
    }   
}


@Override
public void closeDeposit(long depositId , long accountId ) {

    try{
        PreparedStatement ps = 
                    connect.connectionCheck().prepareStatement("SELECT * FROM 
                    mbank.deposits  WHERE deposit_id = ?");
        ps.setLong(1, depositId);  
        ResultSet rs = ps.executeQuery();
        double depositBalance = rs.getDouble("balance");
        Date closingDate = rs.getDate("closing_date");
        java.util.Date currentDate = new java.util.Date();      
        if(currentDate.after(closingDate)){

            PreparedStatement ps1 = 
                           connect.connectionCheck().prepareStatement("SELECT * FROM 
                            mbank.accounts WHERE account_id = ?");
            ps1.setLong(1, accountId);  
            ResultSet rs2 = ps1.executeQuery();
            double AccountBalance = rs2.getDouble("balance");

            PreparedStatement ps2 =
                            connect.connectionCheck().prepareStatement(
                    "UPDATE mbank.accounts SET balance = ?" +
                    "WHERE client_id=?");
            ps2.setDouble(1,AccountBalance+depositBalance);
            ps2.executeUpdate();


            PreparedStatement ps3 =  
                            connect.connectionCheck().prepareStatement(
                    "DELETE FROM mbank.deposits WHERE"+
                    "(?)");
            ps3.setLong(1,depositId );
            ps3.execute();
        }


    }catch (SQLException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null,
                "Problem occurs during the closing the deposit");
    }       


}

@Override
public void preOpenDeposit(long accountId,long depositId) {

    try{
    PreparedStatement ps = connect.connectionCheck().prepareStatement("SELECT *
            FROM mbank.deposits WHERE deposit_id = ?");
    ps.setLong(1, depositId);  
    ResultSet rs = ps.executeQuery();
    double depositBalance = rs.getDouble("balance");
    depositBalance=depositBalance*(PropertiesManager.PRE_OPEN_FEE);


    PreparedStatement ps1 = connect.connectionCheck().prepareStatement("SELECT *    
            FROM mbank.accounts WHERE account_id = ?");
    ps1.setLong(1, accountId);  
    ResultSet rs2 = ps1.executeQuery();
    double AccountBalance = rs2.getDouble("balance");

    PreparedStatement ps2 = connect.connectionCheck().prepareStatement(
            "UPDATE mbank.accounts SET balance = ?" +
            "WHERE client_id=?");
    ps2.setDouble(1,AccountBalance+depositBalance);
    ps2.executeUpdate();


    PreparedStatement ps3 = connect.connectionCheck().prepareStatement(
            "DELETE FROM mbank.deposits WHERE"+
            "(?,?)");
    ps3.setLong(1,depositId );
    ps3.execute();

    }catch (SQLException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null,
                "Problem occurs during the closing the deposit");
    }       


}


@Override
public void viewAllDeposits() {
    try {
        PreparedStatement ps =    
   connect.connectionCheck().prepareStatement("SELECT * FROM mbank.deposits");  
        ResultSet rs = ps.executeQuery();
        String result=null;
        while (rs.next()) {
long deposit_id = rs.getLong("deposit_id");
long client_id = rs.getLong("client_id");
double balance = rs.getDouble("balance");
String type = rs.getString("deposit_type");
long estimated_balance = rs.getLong("estimated_balance");
Date opening_date = rs.getDate("opening_date");
Date closing_date = rs.getDate("closing_date");           
    result = "Client id : " +client_id +"\n" + "deposit id : "  +type +"\n" +"balance:"            
    + balance +"\n" + "deposit type : " +deposit_id +"\n" + "estimated balance : "
    +estimated_balance +"\n" + "closing date : " +closing_date +"\n" + "opening date : "
    +opening_date +"\n"; 
System.out.println(result);
        }

    } catch (SQLException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(null,
        "Problem occurs while trying to retrive all clients");
    }

}



}

还有他的界面:

package Managers;

import Beans.DepositsBean;

public interface IDepositsManager {

public void createNewDeposit(DepositsBean deposit,long clientId,double 
    balance);//Creating a deposit for X amount of time , taking the money for it from 
    the account
public void closeDeposit(long depositId , long accountId); // Closing the deposit 
    for its time is due 
public void preOpenDeposit(long accountId,long depositId); // Pre opening the 
    deposit - transferring the amount to the account after taking the pre-open fee 
public void viewAllDeposits();
}

以及我正在使用的主要产品:

import java.sql.Date;

import Action.Action;
import Action.AdminAction;
import Action.ClientAction;
import Beans.AccountsBean;
import Beans.ClientsBean;
import Beans.DepositsBean;
import Beans.DepositsBean.Type;

public class Main {

public static void main(String[] args) {
    ClientsBean client = new ClientsBean();
    client.setClient_id(227715);

    double balance=2981000;

    java.util.Date currentDate = new java.util.Date();  
    DepositsBean deposit=new DepositsBean();
    deposit.setDeposit_id(1);
    deposit.setClient_id(client.getClient_id());
    deposit.setBalance(100);
    double amount = deposit.getBalance();
    deposit.setType(Type.SHORT);
    deposit.setEstimated_balance(150); 
    deposit.setOpening_date(currentDate);
    deposit.setClosing_date(currentDate);

    ClientAction C = new ClientAction();
    C.createNewDeposit(deposit, client.getClient_id(), amount);
    C.viewClientDeposit(client.getClient_id());
    }
}

当我运行它时,我得到:

线程“主”java.lang.ClassCastException 中的异常: java.util.Date 不能转换为 java.sql.Date at Beans.DepositsBean.setOpening_date(DepositsBean.java:53) 在 Main.main(Main.java:46)

我想要做的是查看今天是什么日期,以及存款应该在什么日期打开。所以我会知道是否该打开存款。 看来我这里的日期有问题。

【问题讨论】:

  • java.sql.Date 扩展了 java.util.Date,因此您可以在任何需要 java.util.Date 的地方使用 java.sql.Date。反之不行!
  • 将日期应用于 SQL 语句时,将其包装在 java.sql.Date 实例中...

标签: java mysql date


【解决方案1】:

只是不要在你的 Bean 中使用 java.sql.Date,只使用 java.util.Date,这是那个的超类型。

当您需要向 JDBC 查询/过程调用等通知某些参数时,即 (AFAIK) 唯一需要java.sql.Date 的地方,最好的办法就是在此处构建它。例如:

java.util.Date d = new java.util.Date();

Statement stmt = ....;

...

stmt.setDate(1, new java.sql.Date(d.getTime());

然后,忘记java.sql.Date,除非你非常确定自己在做什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2011-12-08
    • 1970-01-01
    • 2019-12-09
    • 2011-03-28
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多