mysql、sqlserver数据库常见数据类型对应java中的的类型探究

来源:互联网 发布:商业模式画布 知乎 编辑:程序博客网 时间:2024/05/17 13:10

由于本次测试表的结构不涉及到主键的自增长,所以mysql、sqlserver建表语句相同:

CREATE TABLE testType (id INT NOT NULL DEFAULT 0,gender TINYINT NOT NULL DEFAULT 0,married SMALLINT NOT NULL DEFAULT 0,bigPro BIGINT NOT NULL DEFAULT 0,birthday datetime NOT NULL DEFAULT '1970-01-01');
 手动在数据库插入一条数据后查询:

public static void main(String[] args) throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource();dataSource.setDriverClass("com.mysql.jdbc.Driver");dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");// 指定连接sqlserver数据库// dataSource.setDriverClass("com.microsoft.sqlserver.jdbc.SQLServerDriver");// 指定sqlserver数据库的连接地址// dataSource.setJdbcUrl("jdbc/:sqlserver/://192.169.2.64/:1433;database/=jkpt_kf");dataSource.setUser("root");dataSource.setPassword("123");Connection con = dataSource.getConnection();String sql = "select * from testType";PreparedStatement ps = con.prepareStatement(sql);ResultSet rs = ps.executeQuery();List<Map<String, Object>> resultList = ResultSetUtils.convertRS2List(rs);System.out.println(resultList);Map<String, Object> map = resultList.get(0);for (String str : map.keySet()) {System.out.println(str + " , " + map.get(str).getClass().getName());}}
查询mysql数据库,打印结果如下:

id , java.lang.Integer
gender , java.lang.Integer
married , java.lang.Integer
bigPro , java.lang.Long
birthday , java.sql.Timestamp

由以上打印结果可知,在mysql数据库,int类型、tinyint类型、smallint类型对应的java类型都是Integer,而bigint类型对应的是Long型,datetime类型对应的是Timestamp,是java.util.Date的子类。mysql数据库中,tinyint类型可存储的最大整数是127,smallint类型可存储的最大整数是32767。

查询sqlserver数据库,打印结果如下:

id , java.lang.Integer
gender , java.lang.Short
married , java.lang.Short
bigPro , java.lang.Long
birthday , java.sql.Timestamp

由以上打印结果可知,在sqlserver数据库,同mysql数据库相同的是,int类型对应的java类型是Integer,bigint类型对应的java类型是Long型,datetime类型对应的是Timestamp,不同的是,tinyint类型、smallint对应的java类型是Short而不再是Integer。sqlserver数据库中,tinyint类型可存储的最大整数是255

0 0
原创粉丝点击