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
- Public static void main (string [] ARGs ){
- String d = "15:20:25 ";
- Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
- Try {
- System. Out. println (SDF. parse (d ));
- } Catch (exception e ){
- }
- }
Appendix Timestamp T = new timestamp (calendar. getinstance (). gettime (). gettime ()); |