Date/time handling techniques in JDBC

Source: Internet
Author: User
Tags stmt
1. For different types of time of the database, the corresponding Java wrapper classes should be used to access them respectively;
Date type with java.sql.Date, Time type with java.sql.Time, date/time type with Java.sql.Timestamp;
Here's an example: assuming that you want to get the system time from Oracle, you need to perform sql:select sysdate from dual,
Then, when you get the query results through resultset, be sure to call the method: Gettimestamp (), so that you can take the month and a minute to get out,
The call to GETDATE () can only be taken out of the month-day, call GetTime () can only be retrieved seconds.



2. Try not to call GetString () to obtain date/time type data;
Calling GetString () can also fetch data of the time type, and JDBC converts the date/time to a string internally;
But the format of this string depends on the database, Oracle is a kind of, with Sybase is a, MySQL is a kind of,
If you want to be compatible with a variety of databases, or do not use GetString () good.


3. How to convert the date/time type of JDBC to a string;
Have ever seen someone write this:
Java.sql.Timestamp Timestamp =//Get this data through database access
Java.util.Date utildate = new Java.util.Date (Timestamp.gettime ());
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String str = Sdf.format (utildate);

This can also achieve the ultimate goal, but there is a problem, Java.sql.Timestamp is inherited Java.util.Date,
Therefore, it is not necessary to temporarily create a utildate when format is passed directly into the timestamp. Just like the following:
Java.sql.Timestamp Timestamp =//Get this data through database access
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
String str = Sdf.format (timeStamp);



4. When making a SELECT, update, or INSERT,
If a field is a date/time type, it is best to use PreparedStatement instead of the statement
You will often see a program like this:
Connection conn;//the preceding steps slightly
Statement stmt = Conn.createstatement ();
Stmt.execute ("INSERT into tab (begin_date) VALUES (to_date (' 06/27/2005 12:59:52 ', ' mm/dd/yyyy HH24:MI:SS ')");

People familiar with Oracle know that To_date is a function in Oracle, and that other databases are not, if any, likely to be different. So this code can only be used on Oracle,
If you change to another database, the program cannot be executed.

If you use PreparedStatement, the code is like this:
Connection conn;//the preceding steps slightly
PreparedStatement pstmt = conn.preparestatement ("INSERT into tab (Begin_date) VALUES (?)");
Pstmt.settimestamp (1, New Timestamp (System.currenttimemillis ()));
Pstmt.execute ();
As you can see, this code has nothing to do with the database, as long as it is a database that supports standard SQL.


5.java.sql.timestamp how to convert to Java.util.Date
Java.sql.Timestamp is a subclass of Java.util.Date that does not require any conversion to be directly assigned:
Java.sql.Timestamp ts;
Java.util.Date utildate;
utildate = ts;



6.java.util.date how to convert to Java.sql.Timestamp
Java.util.Date is Java.sql.Timestamp's parent class and cannot be done like trick 5. To do this:
Java.sql.Timestamp ts;
Java.util.Date utildate;
Ts.settime (Utildate.gettime ());

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.