How to store date in Java into datetime in MySQL and convert string to date type

Source: Internet
Author: User

 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 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.executeUpdate();   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 Rule java. util. Date = new java. util. Date ();
Timestamp timestamp = new timestamp (date. gettime (); music. settime (timestamp); hour private date time = NULL; public date gettime (){
Return time;
}
Public void settime (date time ){
This. Time = time;
} How to convert string type to date type

  1. Public static void main (string [] ARGs ){
  2. String d = "15:20:25 ";
  3. Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
  4. Try {
  5. System. Out. println (SDF. parse (d ));
  6. } Catch (exception e ){
  7. }
  8. }

 

Appendix

Timestamp T = new timestamp (calendar. getinstance (). gettime (). gettime ());

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

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.