mysql-数据类型与java数据类型转化⼯具类
mysql和java对照表
类型名称显⽰长度数据库类型JAVA类型JDBC类型索引(int)描述VARCHAR L+N VARCHAR java.lang.String12
CHAR N CHAR java.lang.String1
BLOB L+N BLOB java.lang.byte[]-4
TEXT65535VARCHAR java.lang.String-1
INTEGER4INTEGER UNSIGNED java.lang.Long4
TINYINT3TINYINT UNSIGNED java.lang.Integer-6
SMALLINT5SMALLINT UNSIGNED java.lang.Integer5
MEDIUMINT8MEDIUMINT UNSIGNED java.lang.Integer4
bigdecimal转换为integerBIT1BIT java.lang.Boolean-7
BIGINT20BIGINT UNSIGNED java.math.BigInteger-5
FLOAT4+8FLOAT java.lang.Float7
DOUBLE22DOUBLE java.lang.Double8
DECIMAL11DECIMAL java.math.BigDecimal3
BOOLEAN1同TINYINT
ID11PK (INTEGER UNSIGNED)java.lang.Long4
DATE10DATE java.sql.Date91
TIME8TIME java.sql.Time92
DATETIME19DATETIME java.sql.Timestamp93
TIMESTAMP19TIMESTAMP java.sql.Timestamp93
91
YEAR4YEAR java.sql.Date
对于bolb,⼀般⽤于对图⽚的数据库存储,原理是把图⽚打成⼆进制,然后进⾏的⼀种存储⽅式,在java中对应byte[]数组。
对于boolen类型,在mysql数据库中,个⼈认为⽤int类型代替较好,对bit操作不是很⽅便,尤其是在具有web页⾯开发的项⽬中,表⽰0/1,对应java类型的Integer较好。
/**
* 数据类型转化JAVA
* @param sqlType:类型名称
* @return
*/
public static String toSqlToJava(String sqlType) {
if( sqlType == null || im().length() == 0 ) return sqlType;
sqlType = LowerCase();
switch(sqlType){
case "nvarchar":return "String";
case "char":return "String";
case "varchar":return "String";
case "text":return "String";
case "nchar":return "String";
case "blob":return "byte[]";
case "integer":return "Long";
case "tinyint":return "Integer";
case "smallint":return "Integer";
case "mediumint":return "Integer";
case "bit":return "Boolean";
case "bigint":return "java.math.BigInteger";
case "float":return "Fload";
case "double":return "Double";
case "decimal":return "java.math.BigDecimal";
case "boolean":return "Boolean";
case "id":return "Long";
case "date":return "java.util.Date";
case "datetime":return "java.util.Date";
case "year":return "java.util.Date";
case "time":return "java.sql.Time";
case "timestamp":return "java.sql.Timestamp";
case "numeric":return "java.math.BigDecimal";
case "real":return "java.math.BigDecimal";
case "money":return "Double";
case "smallmoney":return "Double";
case "image":return "byte[]";
default:
System.out.println("-----------------》转化失败:未发现的类型"+sqlType);
break;
}
return sqlType;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。