Use hibernate to easily solve the problem of incompatibility between date and datetime types in Java

Source: Internet
Author: User

 

Today it's okay. I followed Shang xuexiang to write a forum program. A problem was found during the write time. The datetime type used for posting time when the MySQL table is created. And use the system function now. Java. util. date type used in Java object classes. I found this to be a headache. The time type in Java has always been a headache.

With a hard head, we finally found that hibernate had helped us solve the problem.

The simple MySQL table statement is as follows:

Create Database BBS;

Use BBS;

Create Table article
(
Id int primary key auto_increment,
PID int,
Rootid int,
Title varchar (255 ),
Cont text,
Pdate datetime,
Isleaf int #1-not leaf 0-leaf
);

Insert into article values (null, 0, 1, 'Ant vs. elephant ', 'Ant vs. elephant', now (), 1 );
Insert into article values (null, 1, 1, 'elephant knocked down ', 'elephant knocked down', now (), 1 );
Insert into article values (null, 2, 1, 'Ant is not too bad ', 'Ant is not too bad', now (), 0 );
Insert into article values (null, 2, 1, 'taobao', 'taobao', now (), 1 );
Insert into article values (null, 4, 1, 'no taobao', 'no taobao', now (), 0 );
Insert into article values (null, 1, 1, 'How possible ', 'How possible', now (), 1 );
Insert into article values (null, 6, 1, 'How is it impossible? ', 'How is it impossible?', now (), 0 );
Insert into article values (null, 6, 1, 'likelihood is great ', 'likelihood is great', now (), 0 );
Insert into article values (null, 2, 1, 'elephant in hospital ', 'elephant in hospital', now (), 1 );
Insert into article values (null, 9, 1, 'nurse is ant', 'nurse is ant', now (), 0 );

The object class is as follows:

Package database;

Import java. util. date;

/**
* Article entity.
*
* @ Author smartcat86
*/

Public class article implements java. Io. serializable {

// Fields

Private integer ID;
Private integer PID;
Private integer rootid;
Private String title;
Private string cont;
Private date pdate;
Private integer isleaf;

// Constructors

/** Default constructor */
Public article (){
}

/** Full constructor */
Public article (integer PID, integer rootid, String title, string cont,
Date pdate, integer isleaf ){
This. PID = PID;
This. rootid = rootid;
This. Title = title;
This. cont = cont;
This. pdate = pdate;
This. isleaf = isleaf;
}

// Property accessors

Public integer GETID (){
Return this. ID;
}

Public void setid (integer ID ){
This. ID = ID;
}

Public integer getpid (){
Return this. PID;
}

Public void setpid (integer PID ){
This. PID = PID;
}

Public integer getrootid (){
Return this. rootid;
}

Public void setrootid (integer rootid ){
This. rootid = rootid;
}

Public String gettitle (){
Return this. title;
}

Public void settitle (String title ){
This. Title = title;
}

Public String getcont (){
Return this. cont;
}

Public void setcont (string cont ){
This. cont = cont;
}

Public date getpdate (){
Return this. pdate;
}

Public void setpdate (date pdate ){
This. pdate = pdate;
}

Public integer getisleaf (){
Return this. isleaf;
}

Public void setisleaf (integer isleaf ){
This. isleaf = isleaf;
}

}

Test class

Package database;

Public class test {

Public static void main (string [] ARGs ){
Dboperate DBO = new dboperate (hibernatesessionfactory. getsession (); // database operation class
Article A = new article ();
A. setid (null); // Enable Native in hibernate to automatically increase
A. setpid (PID );
A. setrootid (rootid );
A. settitle (title );
A. setcont (cont );
A. setpdate (new date ());
A. setisleaf (0 );

DBO. insert ();
}
}

After testing, the insertion time is:2008-01-029 04:04:36. 0,The same result is obtained using new java. Text. simpledateformat ("YY-mm-dd hh: mm: SS"). Format (New java. util. Date.

Of course, the simpledateformat class can be used for implementation from the database.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.