前端之家收集整理的这篇文章主要介绍了
Oracle插入timestamp类型数据,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- createtableTEST(
- IDINTEGER,
- BIRTHDAYTIMESTAMP
- );
使用JDBC将日期插入到TIMESTAMP类型字段
- importjava.sql.Connection;
- importjava.sql.DriverManager;
- importjava.sql.PreparedStatement;
- importjava.sql.sqlException;
- importjava.text.SimpleDateFormat;
- importjava.util.Date;
- publicclassTest{
- static{
- try{
- Class.forName("oracle.jdbc.driver.OracleDriver");
- }catch(ClassNotFoundExceptione){
- e.printStackTrace();
- }
- }
- publicstaticConnectiongetConnection(){
- Connectionconn=null;
- try{
- conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/orcl","root","root");
- }catch(sqlExceptione){
- e.printStackTrace();
- }
- returnconn;
- }
- publicstaticvoidmain(String[]args)throwssqlException{
- PreparedStatementpst=null;
- Connectionconn=getConnection();
- SimpleDateFormatdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
- StringmyDate=df.format(newDate());
- Stringsql="insertintotest(id,birthday)values('1',to_timestamp('"+myDate+"','yyyy-mm-ddhh24:mi:ss'))";
- pst=conn.prepareStatement(sql);
- pst.executeUpdate();
- }
- }
使用to_timestamp将字符型转成timestamp 原文链接:https://www.f2er.com/oracle/208587.html