1. 数据库类型
oracle 基本数据类型
数据类型对应关系 ,详情参看 : oracle docs
mysql 基本数据类型
数据类型对应关系 : 详情参看:mysql docs
MySQL Types to Java Types for ResultSet.getObject()
MySQL Type Name | Return value ofGetColumnClassName | Returned as Java Class |
---|---|---|
BIT(1) (new in MySQL-5.0) | BIT | java.lang.Boolean |
BIT( > 1) (new in MySQL-5.0) | BIT | byte[] |
TINYINT | TINYINT | java.lang.Boolean if the configuration property tinyInt1isBit is set to true (the default) and the storage size is 1, or java.lang.Integer if not. |
BOOL , BOOLEAN | TINYINT | See TINYINT , above as these are aliases for TINYINT(1) , currently. |
SMALLINT[(M)] [UNSIGNED] | SMALLINT [UNSIGNED] | java.lang.Integer (regardless of whether it is UNSIGNED or not) |
MEDIUMINT[(M)] [UNSIGNED] | MEDIUMINT [UNSIGNED] | java.lang.Integer (regardless of whether it is UNSIGNED or not) |
INT,INTEGER[(M)] [UNSIGNED] | INTEGER [UNSIGNED] | java.lang.Integer , if UNSIGNED java.lang.Long |
BIGINT[(M)] [UNSIGNED] | BIGINT [UNSIGNED] | java.lang.Long , if UNSIGNED java.math.BigInteger |
FLOAT[(M,D)] | FLOAT | java.lang.Float |
DOUBLE[(M,B)] | DOUBLE | java.lang.Double |
DECIMAL[(M[,D])] | DECIMAL | java.math.BigDecimal |
DATE | DATE | java.sql.Date |
DATETIME | DATETIME | java.sql.Timestamp |
TIMESTAMP[(M)] | TIMESTAMP | java.sql.Timestamp |
TIME | TIME | java.sql.Time |
YEAR[(2|4)] | YEAR | If yearIsDateType configuration property is set to false , then the returned object type is java.sql.Short . If set to true (the default), then the returned object is of type java.sql.Date with the date set to January 1st, at midnight. |
CHAR(M) | CHAR | java.lang.String (unless the character set for the column is BINARY , then byte[] is returned. |
VARCHAR(M) [BINARY] | VARCHAR | java.lang.String (unless the character set for the column is BINARY , then byte[] is returned. |
BINARY(M) | BINARY | byte[] |
VARBINARY(M) | VARBINARY | byte[] |
TINYBLOB | TINYBLOB | byte[] |
TINYTEXT | VARCHAR | java.lang.String |
BLOB | BLOB | byte[] |
TEXT | VARCHAR | java.lang.String |
MEDIUMBLOB | MEDIUMBLOB | byte[] |
MEDIUMTEXT | VARCHAR | java.lang.String |
LONGBLOB | LONGBLOB | byte[] |
LONGTEXT | VARCHAR | java.lang.String |
ENUM('value1','value2',...) | CHAR | java.lang.String |
SET('value1','value2',...) | CHAR | java.lang.String |
2. 默认当前时间
oracle 中 默认当前时间
使用 sysdate
栗子:
create table user (
id varchar2(100) not null primary key,
name varchar2(50) not null,
create_time default sysdate
)
tablespace xlch;
mysql 中 默认当前时间
使用 DEFAULT CURRENT_TIMESTAMP | ON UPDATE CURRENT_TIMESTAMP ,同时使用时无先后顺序 ,任何 和 current_timestamp 同义词的 函数都可以使用,具有同义词的如下: current_timestamp() | now() | localtime | localtime() | localtimestamp | localtimestamp()
mysql 中 TIMESTAMP 类型的列 和 DATETIME 类型的列 可以自动初始化 和 自动更新当前时间 日期。
——自动初始化 :当插入记录没有指定该列的值时,自动初始化列 将被设置为 当前的 timestamp . (类似与 oracle 中的 default sysdate)
——自动更新:当 一条记录 中的任意某列值发生改变,自动更新的列 将被自动更新到当前的 timestamp ,如果 一条记录的 其他列的值 被设置为当前值 , 自动更新的列不更新
另外 ,也可以 指定其为 null 值(默认是不允许为 null)
栗子:
mysql> create table user(
-> id int not null auto_increment,
-> name varchar(50) not null,
-> create_time datetime default current_timestamp,
-> update_time timestamp default current_timestamp on update current_timestamp,
-> primary key(id)
-> );
Query OK, 0 rows affected (1.53 sec)
验证操作:
mysql> insert into user (name) values ('xlch');
Query OK, 1 row affected (0.22 sec)
mysql> select * from user;
+----+------+---------------------+---------------------+
| id | name | create_time | update_time |
+----+------+---------------------+---------------------+
| 1 | xlch | 2016-12-12 13:19:28 | 2016-12-12 13:19:28 |
+----+------+---------------------+---------------------+
1 row in set (0.18 sec)
mysql> update user set name = 'ssx' where id = 1;
Query OK, 1 row affected (0.41 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from user;
+----+------+---------------------+---------------------+
| id | name | create_time | update_time |
+----+------+---------------------+---------------------+
| 1 | ssx | 2016-12-12 13:19:28 | 2016-12-12 13:21:25 |
+----+------+---------------------+---------------------+
1 row in set (0.00 sec)
3. java 中时间类型区别
java.util.Date | java.sql.Date | java.sql.Time | java.sql.Timestamp 区别
java.util.Date 是 以下三个类的 父类 。
java.sql.Date : 对应 sql 中的 date 类型,即只保留 年,月,日,而时,分,秒,毫秒都会被忽略,而且 没有绑定 时区(timezone);
java.sql.Time : 对应 sql 中的 time 类型,,只保留 时,分,秒,毫秒,而其他的 年,月,日会被忽略;
java.sql.Timestamp : 对应 sql 中的 timestamp 类型 ,能够自定义精度,并且能精确到纳秒级(java.util.Date 只能支持到毫秒级)。
4.类型转换
oracle日期类型转换 :
★ date→ string : to_char(date,format)
select to_char(sysdate,'yyyy-mm-dd HH24:MI:SS') from dual;
★ String → date : to_date(value,format)
★ timestamp → date
java 中的 timestap 类型 转为 oracle数据库的 date类型
思路一: 先转为字符串 (to_char()),然后转为 date(to_date());
思路二: 使用内置函数 cast();函数详情介绍参看:CAST
cast 用于 将内置的数据类型 或者 集合类型的值 转换为 另一种内置数据类型 或者 集合类型的值。
栗子:
select cast(systimestamp as date) from dual;
思路三:使用内置函数 trunc(date [,format]) 返回 被截取的 date 类型,详情参看: oracle docs 或者 techonthenet
select trunc(systimestamp,'DDD') from dual;;--截取到 日
mysql 日期类型转换
详情参看 :Date 和 String 相互转换
★ date → string:date_format(date,format)
★ string→ date:str_to_date(str,format)