zoukankan      html  css  js  c++  java
  • 在JDBC中使用Java8的日期LocalDate、LocalDateTime

      在实体Entity里面,可以使用java.sql.Date、java.sql.Timestamp、java.util.Date来映射到数据库的date、timestamp、datetime等字段

    但是,java.sql.Date、java.sql.Timestamp、java.util.Date这些类都不好用,很多方法都过时了。

    Java8里面新出来了一些API,LocalDate、LocalTime、LocalDateTime 非常好用

    如果想要在JDBC中,使用Java8的日期LocalDate、LocalDateTime,则必须要求数据库驱动的版本不能低于4.2

    下面将分别演示如何在JDBC中使用Java8的日期LocalDate、LocalDateTime来操作mysql,postgresql

    一:MySQL

    首先创建表:

    create table tab_java8date (id int not null primary key auto_increment,t_date date, t_time time, t_datetime datetime);

    然后,加入mysql的驱动 

     
    1. <dependency>  
    2.     <groupId>mysql</groupId>  
    3.     <artifactId>mysql-connector-java</artifactId>  
    4.     <version>5.1.37</version>  
    5. </dependency>  

    上面说了,数据库驱动的版本不能低于4.2,如何判断呢?

    直接打开数据库驱动jar,里面有个META-INF/MANIFEST.MF文件

    注意这里,必须要至少是4.2

    JDBC代码如下: 

     
    1. import java.sql.Connection;  
    2. import java.sql.DriverManager;  
    3. import java.sql.PreparedStatement;  
    4. import java.time.LocalDate;  
    5. import java.time.LocalDateTime;  
    6. import java.time.LocalTime;  
    7.   
    8. public class App {  
    9.     public static void main(String[] args) throws Exception {  
    10.         Class.forName("com.mysql.jdbc.Driver");  
    11.         Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.1.100:3306/db_java8","root","root123");  
    12.         PreparedStatement st = conn.prepareStatement("insert into tab_java8date (t_date,t_time,t_datetime)values(?,?,?)");  
    13.         st.setObject(1, LocalDate.now());  
    14.         st.setObject(2, LocalTime.now());  
    15.         st.setObject(3, LocalDateTime.now());  
    16.         st.execute();  
    17.         st.close();  
    18.         conn.close();  
    19.     }  
    20. }  

    运行,查询数据库

    mysql> select * from tab_java8date;
    +----+------------+----------+---------------------+
    | id | t_date     | t_time   | t_datetime          |
    +----+------------+----------+---------------------+
    |  1 | 2016-11-13 | 11:34:31 | 2016-11-13 11:34:31 |
    +----+------------+----------+---------------------+
    1 row in set (0.00 sec)

    看到已经成功插入到数据库中去了

    如果你使用的mysql-connector-java版本低于5.1.37,则数据库的驱动版本低于4.2,运行会报如下错误: 

     
    1. Exception in thread "main" com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: 'xACxEDx00x05srx00x0Djava.time.Serx95]x84xBAx1B"HxB2x0Cx00x00xpwx07x03x00x00x07xE0x0Bx0Dx' for column 't_date' at row 1  
    2.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3845)  
    3.     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)  
    4.     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)  
    5.     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)  
    6.     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)  
    7.     at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)  
    8.     at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1193)  
    9.     at com.pp.App.main(App.java:18)  

    对于mysql驱动器,可以通过查看release note查看支持的版本,https://dev.mysql.com/doc/relnotes/connector-j/5.1/en/news-5-1-45.html。

  • 相关阅读:
    linux定时器
    TIMESTAMP和DATETIME的区别
    Linux进程或线程绑定到CPU
    C++学习笔记
    磁盘扇区校验和
    docker安装mysql,并配置部分表同步
    docker 安装tomcat
    多tomcat 同一个浏览器 多个项目 会导致session覆盖
    Ubuntu 安装 NodeJS
    Ubuntu 下安装 Arduino IDE
  • 原文地址:https://www.cnblogs.com/zhjh256/p/8372299.html
Copyright © 2011-2022 走看看