A table contains a field of the datetime type, and you want to insert a time to it in the format of "yyyy-mm-dd hh: mm: SS ";
First, execute ps. setdate (2, date) in SQL )... After successful insertion, the time and seconds of the background database are always 00:00:00.
I have already formatted the statement before executing the SQL statement.
Simpledateformat SFD = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); string r1_tdate = SFD. format (date); Java. SQL. date date2 = Java. SQL. date. valueof (cfinishdate); PS. setdate (7, date2 );
The above method directly generates a bug:
Java. Lang. numberformatexception: for input string: "18 02:18:15"
Don't understand, just ask du Niang, and find that the vast majority of us are called first format first, then insert.
In addition, I inserted the current time into MySQL and tried it once as someone else said:
PS. setdate (2, new java. SQL. Date (long) New java. util. Date (). gettime ()));
As a result, there is no record in MySQL data, that is, time, minute, and second. 00:00:00 is displayed.
In fact, I always thought that in MySQL, data in the format of date, datetime, and other time types should be used to execute SQL insert in Java, and the PS. setdate method should be used,
You can use setstring to insert data directly. The format must be in advance. After experiment, it is really convenient to insert with setstring.
Besides, if it is still a field of the datetime type, it should be read from the database in the format of "yyyy-mm-dd hh: mm: SS. You can only read "yyyy-mm-dd.
Therefore, the best way is to change the field type to varchar to store the time. This greatly saves the trouble of format conversion.