LocalDate、LocalTime、LocalDateTime 和mysql交互


大家知道,在實體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 tb_java8date (id int not null primary key auto_increment,t_date date, t_time time, t_datetime datetime);

然后,加入mysql的驅動

[html]  view plain  copy
  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代碼如下:

[java]  view plain  copy
  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 tb_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 tb_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,運行會報如下錯誤:

[plain]  view plain  copy
  1. Exception in thread "main" com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x07\x03\x00\x00\x07\xE0\x0B\x0Dx' 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)  


二:PostgreSQL

首先創建表:

create table tb_java8date (id SERIAL not null primary key,t_date date, t_time time, t_datetime timestamp);

然后,加入PostgreSQL的數據庫驅動

[html]  view plain  copy
  1. <dependency>  
  2.     <groupId>org.postgresql</groupId>  
  3.     <artifactId>postgresql</artifactId>  
  4.     <version>9.4.1212</version>  
  5. </dependency>  
注意這里添加的數據庫驅動版本最低要是4.2,檢驗方法和上面類似


JDBC代碼如下:

[java]  view plain  copy
  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("org.postgresql.Driver");  
  11.         Connection conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/pg_java8","admin","123456");  
  12.         PreparedStatement st = conn.prepareStatement("insert into tb_java8date (t_date,t_time,t_datetime)values(?,?,?)");  
  13.         System.out.println(st.getClass());  
  14.         st.setObject(1, LocalDate.now());  
  15.         st.setObject(2, LocalTime.now());  
  16.         st.setObject(3, LocalDateTime.now());  
  17.         st.execute();  
  18.         st.close();  
  19.         conn.close();  
  20.     }  
  21. }  
運行,然后查詢數據庫表

發現,已經成功執行

如果你加入的依賴,數據庫的驅動版本低於4.2,運行會報如下錯誤:

[java]  view plain  copy
  1. Exception in thread "main" org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.time.LocalDate. Use setObject() with an explicit Types value to specify the type to use.  
  2.     at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:1051)  
  3.     at com.pp.App.main(App2.java:16)  

以上只是演示了mysql,postgresql兩個數據庫,其他的數據庫,請自行測試。我這里就不演示了,方法都類似。

原文地址:https://blog.csdn.net/qq_40006446/article/details/80535359


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM