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;
}