[sql]
view plain
copy
- createtableTEST(
- IDINTEGER,
- BIRTHDAYTIMESTAMP
- );
使用JDBC将日期插入到TIMESTAMP类型字段
[java]
view plain
copy
- 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 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|