1 16 package com.ibatis.sqlmap.engine.type; 17 18 import com.ibatis.sqlmap.client.SqlMapException; 19 20 import java.sql.*; 21 import java.text.DateFormat ; 22 import java.text.ParseException ; 23 import java.text.SimpleDateFormat ; 24 25 28 public class SqlTimestampTypeHandler extends BaseTypeHandler implements TypeHandler { 29 30 private static final String DATE_FORMAT = "yyyy/MM/dd hh:mm:ss"; 31 private static final DateFormat format = new SimpleDateFormat (DATE_FORMAT); 32 33 public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType) 34 throws SQLException { 35 ps.setTimestamp(i, (java.sql.Timestamp ) parameter); 36 } 37 38 public Object getResult(ResultSet rs, String columnName) 39 throws SQLException { 40 Object sqlTimestamp = rs.getTimestamp(columnName); 41 if (rs.wasNull()) { 42 return null; 43 } else { 44 return sqlTimestamp; 45 } 46 } 47 48 public Object getResult(ResultSet rs, int columnIndex) 49 throws SQLException { 50 Object sqlTimestamp = rs.getTimestamp(columnIndex); 51 if (rs.wasNull()) { 52 return null; 53 } else { 54 return sqlTimestamp; 55 } 56 } 57 58 public Object getResult(CallableStatement cs, int columnIndex) 59 throws SQLException { 60 Object sqlTimestamp = cs.getTimestamp(columnIndex); 61 if (cs.wasNull()) { 62 return null; 63 } else { 64 return sqlTimestamp; 65 } 66 } 67 68 public Object valueOf(String s) { 69 try { 70 java.util.Date date = format.parse(s); 71 return new Timestamp(date.getTime()); 72 } catch (ParseException e) { 73 throw new SqlMapException("Error parsing default null value date. Format must be '" + DATE_FORMAT + "'. Cause: " + e); 74 } 75 } 76 77 } 78 | Popular Tags |