Unable to display minutes and seconds when jdbc oracle database storage and access time field

About database access time field only display the date (date) can not get the time (minutes and seconds) problem is a relatively common problem, here to do an analysis for this problem and find a solution.

This problem occurs reasons:

       Since java.util.Date type, though they have minutes and seconds, but this is just the type of java in the class, not the time the database type, not directly into the database; it can only be used java.sql.Date stored in the database, but it only the date and no time.

problem solved:

      Method a: A java.util.Date into java.sql.Timestamp

          public java.sql.Timestamp getTimestamp(Date date) {
                return new java.sql.Timestamp(date.getTime());
          }

     Method 2: In the write sql statement date parameters TO_DATE () function was transformed into      

String sql =" UPDATE  tablename  SET state=?,deal_date=to_date(?,'yyyy-mm-dd hh24:mi:ss')  where id=?

              Connection conn = null;
              PreparedStatement ps = null;
   try {
              Date date = new java.util.Date();
              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              String formatdate = sdf.format(date);//获取格式化日期,带有时分秒

              conn = getConnection();
	          ps = conn.prepareStatement(sql);
              ps.setString(1, state);
              ps.setString(2,formatdate);
              ps.setLong(3,Long.valueOf(id));
              ps.executeUpdate();
        } catch(Exception e) {
	        	e.printStackTrace();
	    }finally {
	            cleanUp(conn, null, ps, null);
	        }

 

 

Published 118 original articles · won praise 59 · views 490 000 +

Guess you like

Origin blog.csdn.net/u012255097/article/details/104211884