简体   繁体   中英

conversion from util.Date to sql.Date not working

Why does the below query return this error message: 'data exception: invalid datetime format'? I have researched this and there does not seem to be anything wrong with it?

java.util.Date today = new java.util.Date(); 
releaseDate = new java.sql.Date(today.getTime());
qtd.updateTakenOutTable("insert into takenOutTbl (MovieID, CellPhoneNum, DateTakenOut) VALUES ( ?,      ?, ?)", movieID, num, releaseDate); 

and here is the method:

public void updateTakenOutTable(String update, String movieID, String num, java.sql.Date releaseDate) throws SQLException {
    try {
        Connection connection = dc.DatabaseConnection();
        PreparedStatement statement = connection.prepareStatement(update);
        statement.setString(1, movieID);
        statement.setDate(2,releaseDate);
        statement.setString(3, num);
        statement.executeUpdate();
        statement.close();

    } catch (SQLException ex) {
        System.err.println(ex.getMessage());
    }
}

Seems you are trying to assign releaseDate into CellPhoneNum and num to DateTakenOut .

try

statement.setString(2, num);    
statement.setDate(3,releaseDate);

You are mixing how your date and time information is formatted, hence invalid formats... one is "long" and one is not. Can't see the declared types if any.

today is a util.Date see http://docs.oracle.com/javase/7/docs/api/java/util/Date.html

long getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT >represented by this Date object.

releaseDate is a sql.Date http://docs.oracle.com/javase/7/docs/api/java/sql/class-use/Date.html#java.sql

Reading this documentation carefully, you will see that the type is NEVER represented as "long."

If you want a sql oriented time, perhaps you would rather use ... sql.time? This has its own page on docs.oracle.

The sql.date and sql.time are intended for communicating with SQL databases, while the utility. Date is not. Time is not easily handled across different database systems. The long type you referenced is to count milliseconds like a timer...this timer started in 1970 for the Y2K problem at the turn of the century. It allows comparison on this scale. Note the documents regarding how many milliseconds are in various lengths of time ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM