A simple example of how Java inserts the datetime type of MySQL

Source: Internet
Author: User

Go to the instance first, so that you do not get depressed!

 

Instance:

How to insert the datetime type of MySQL in Java

Date = new date ();

Timestamp timestamp = new timestamp (date. gettime ());

Clientinfo. setclientinfoaddtime (timestamp );

The data in the format of "yyyy-mm-dd hh: mm: SS" is put into the database.

 

In the past, I have always treated the period as a character type. Khan, recently the project needs to be relatively time-consuming, so I checked the information to understand that Java can directly insert data of the date type, ignorant!
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. preparedstatement;
Import java. SQL. timestamp;

Public class databasetest {
Private connection conn;
Preparedstatement pstmt = NULL;
Private void test (){
Try {
Class. forname ("com. MySQL. JDBC. Driver"). newinstance ();
String url = "JDBC: mysql: // localhost: 3306/Rhino? User = root & Password = & useunicode = true & characterencoding = UTF-8 ";
Connection conn = drivermanager. getconnection (URL );
System. Out. println ("connect to database successfully! ");

Pstmt = conn. preparestatement ("insert into rh_entry" +
"(ID, allow_comments, category_id, comment_count, content, create_on, name, status)" +
"Values (?,?,?,?,?,?,?,?) ");
Conn. setautocommit (false );
Java. util. Date = new java. util. Date ();
Timestamp TT = new timestamp (date. gettime ());
Pstmt. setint (1, 1 );
Pstmt. setint (2, 1 );
Pstmt. setint (3, 1 );
Pstmt. setint (4, 5 );
Pstmt. setstring (5, "Shit !!!!! ");
Pstmt. settimestamp (6, TT );
Pstmt. setstring (7, "Jordan ");
Pstmt. setint (8, 1 );
Int j=pstmt.exe cuteupdate ();
Conn. Commit ();
If (J! = 0)
System. Out. println ("OK ");
} Catch (exception e ){
Try {
Conn. rollback ();
} Catch (exception E1 ){
E1.printstacktrace (system. Out );
}
E. printstacktrace ();
} Finally {
Try {
Pstmt. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
}

Public static void main (string ARGs []) {
Databasetest dt = new databasetest ();
DT. Test ();
}
}

Numeric type
Integer JDBC
Tinyint java. Lang. Integer
Smallint
Mediumint java. Lang. Long
Int
Bigint java. Math. biginteger

Single-precision floating-point JDBC
Float java. Lang. Float

Double-precision floating-point JDBC
Double java. Lang. Double

Other JDBC
Decimal java. Math. bigdecimal

Balanced JDBC
Char java. Lang. String
Varchar
Tinytext
Text
Mediumtext
Longtext

Date JDBC
Date java. SQL. Date
Datetime java. SQL. Timestamp
Timestamp java. SQL. Timestamp
Time Java. SQL. Time
Year java. SQL. Date

Others
Tinyblob To be determined
Blob
Mediumblob
Longblob

Enum
Set
Bytes -------------------------------------------------------------------------------------------------------
Java. util. Date = new java. util. Date ();
Timestamp timestamp = new timestamp (date. gettime ());
Music. settime (timestamp );
Bytes -------------------------------------------------------------------------------------------------------
Private date time = NULL;
Public date gettime (){
Return time;
}
Public void settime (date time ){
This. Time = time;
}

Related Article

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.