Insert Date data into the SQL Server database

Source: Internet
Author: User
The field type in the database is datetime.

1. Use the java. util. Date class

  1. Import java. SQL. connection;
  2. Import java. util. date;
  3. Import java. SQL. resultset;
  4. Import java. SQL. sqlexception;
  5. Import java. SQL. statement;
  6. Import java. Text. simpledateformat;
  7. /**
  8. * @ Author: jcuckoo
  9. * @ Date: 2008-12-1
  10. * @ Version: V 1.0
  11. */
  12. Public class maintest {
  13. Public static void main (string [] ARGs ){
  14. Connection conn;
  15. Statement st;
  16. Resultset RS;
  17. Simpledateformat SDF = new simpledateformat ("yyyy/mm/DD ");
  18. Date = new date ();
  19. System. Out. println (date );
  20. String d = SDF. Format (date );
  21. System. Out. println (d );
  22. String SQL = "insert into chepiao (ID, starttime) values (117, '" + D + "')";
  23. Conn = dbpool. getconnection ();
  24. Try {
  25. St = conn. createstatement ();
  26. St.exe cute (SQL );
  27. } Catch (sqlexception e ){
  28. E. printstacktrace ();
  29. }
  30. }
  31. }

2. Use the java. SQL. Date class

Java. Lang. Object
Java. util. Date
Java. SQL. Date

Public classDateExtendsDate

PublicDate(INT year, int month, int Day)

year-Year minus 1900, which must be the number between 0 and 8099. (Note that 8099 is obtained from 9999 minus 1900 .)

month-Number between 0 and 11

day-Number between 1 and 31

A thin wrapper (thin wrapper) that encapsulates millisecond values. It allows JDBC to mark millisecond values as SQL DATEValue. The millisecond value indicates the number of milliseconds since January 1, January 1, 1970 00:00:00 GMT.

To work with SQLDATEConsistent definition, fromjava.sql.DateThe instance package's millisecond value must be "normalized" by setting the hour, minute, second, and millisecond to zero in a specific time zone related to the instance ".

If you want to write data to the database from 2001 to 12, you need date = new date (-1900,11, 12 );

  1. Import java. SQL. connection;
  2. Import java. SQL. date;
  3. Import java. SQL. sqlexception;
  4. Import java. SQL. statement;
  5. Import java. Text. simpledateformat;
  6. /**
  7. * @ Author: jcuckoo
  8. * @ Date: 2008-12-1
  9. * @ Version: V 1.0
  10. */
  11. Public class maintestsqldate {
  12. Public static void main (string [] ARGs ){
  13. Connection conn;
  14. Statement st;
  15. Date = new date (2001-, 11, 12 );
  16. String SQL = "insert into chepiao (ID, starttime) values (122, '" + date + "')";
  17. Conn = dbpool. getconnection ();
  18. Try {
  19. St = conn. createstatement ();
  20. St.exe cute (SQL );
  21. } Catch (sqlexception e ){
  22. E. printstacktrace ();
  23. }
  24. }
  25. }

 

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.