The field type in the database is datetime.
1. Use the java. util. Date class
- Import java. SQL. connection;
- Import java. util. date;
- Import java. SQL. resultset;
- Import java. SQL. sqlexception;
- Import java. SQL. statement;
- Import java. Text. simpledateformat;
- /**
- * @ Author: jcuckoo
- * @ Date: 2008-12-1
- * @ Version: V 1.0
- */
- Public class maintest {
- Public static void main (string [] ARGs ){
- Connection conn;
- Statement st;
- Resultset RS;
- Simpledateformat SDF = new simpledateformat ("yyyy/mm/DD ");
- Date = new date ();
- System. Out. println (date );
- String d = SDF. Format (date );
- System. Out. println (d );
- String SQL = "insert into chepiao (ID, starttime) values (117, '" + D + "')";
- Conn = dbpool. getconnection ();
- Try {
- St = conn. createstatement ();
- St.exe cute (SQL );
- } Catch (sqlexception e ){
- E. printstacktrace ();
- }
- }
- }
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
DATE
Value. The millisecond value indicates the number of milliseconds since January 1, January 1, 1970 00:00:00 GMT.
To work with SQLDATE
Consistent definition, fromjava.sql.Date
The 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 );
- Import java. SQL. connection;
- Import java. SQL. date;
- Import java. SQL. sqlexception;
- Import java. SQL. statement;
- Import java. Text. simpledateformat;
- /**
- * @ Author: jcuckoo
- * @ Date: 2008-12-1
- * @ Version: V 1.0
- */
- Public class maintestsqldate {
- Public static void main (string [] ARGs ){
- Connection conn;
- Statement st;
- Date = new date (2001-, 11, 12 );
- String SQL = "insert into chepiao (ID, starttime) values (122, '" + date + "')";
- Conn = dbpool. getconnection ();
- Try {
- St = conn. createstatement ();
- St.exe cute (SQL );
- } Catch (sqlexception e ){
- E. printstacktrace ();
- }
- }
- }