Oracle inserts timestamp data

Source: Internet
Author: User

Oracle inserts timestamp data

Oracle table structure:

Create table TEST (
Id integer,
BIRTHDAY TIMESTAMP
);

Insert a date to a TIMESTAMP field using JDBC

Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. SQLException;
Import java. text. SimpleDateFormat;
Import java. util. Date;

Public class Test {
 
Static {
Try {
Class. forName ("oracle. jdbc. driver. OracleDriver ");
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
}

/**
* Obtain the Connection
*
* @ Return
*/
Public static Connection getConnection (){
Connection conn = null;
Try {
Conn = DriverManager. getConnection ("jdbc: oracle: thin: @ localhost: 1521/orcl", "root", "root ");
} Catch (SQLException e ){
E. printStackTrace ();
}
Return conn;
}

Public static void main (String [] args) throws SQLException {
// TODO Auto-generated method stub
PreparedStatement pst = null;
Connection conn = getConnection ();
SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
String myDate = df. format (new Date (); // current time
String SQL = "insert into test (id, birthday) values ('1', to_timestamp ('" + myDate + "', 'yyyy-mm-dd hh24: mi: ss '))";
Pst = conn. prepareStatement (SQL );

Pst.exe cuteUpdate ();
// Omitted IO stream close
}

}

Use to_timestamp to convert struct type to timestamp

Install Oracle 11gR2 (x64) in CentOS 6.4)

Steps for installing Oracle 11gR2 in vmwarevm

Install Oracle 11g XE R2 In Debian

Install Oracle 11.2.0.4 x64 in Oracle Linux 6.5

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.