如何將java中Date存入mysql中的datetime中,字串怎麼轉換為日期類型

來源:互聯網
上載者: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(); }}

  


數值型
整型 JDBC
tinyint         java.lang.Integer
smallint
mediumint       java.lang.Long
int          
bigint         java.math.BigInteger

單精確度浮點型 JDBC
float         java.lang.Float

雙精確度浮點型 JDBC
double         java.lang.Double

其他 JDBC
decimal         java.math.BigDecimal

字元型 JDBC
char           java.lang.String
varchar
tinytext
text
mediumtext
longtext

日期型 JDBC
date           java.sql.Date
datetime         java.sql.Timestamp
timestamp       java.sql.Timestamp
time           java.sql.Time
year           java.sql.Date

其他
tinyblob         待定
blob
mediumblob
longblob

enum
set  -------------------------------------------------------------------------------------------------------java.util.Date date = new java.util.Date();    
Timestamp timeStamp = new Timestamp(date.getTime()); music.setTime(timeStamp);            -------------------------------------------------------------------------------------------------------private Date time = null;public Date getTime() {
  return time;
 }
 public void setTime(Date time) {
  this.time = time;
 }  字串類型如何轉換為日期類型

  1. public static void main(String[] args) {
      
  2.         String d = "2008-10-13 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.     }  

 

附一句

Timestamp t=new Timestamp(Calendar.getInstance().getTime().getTime());

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.