源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

JDBC中使用Java8的日期LocalDate和LocalDateTime操作mysql、postgresql

  • 时间:2021-08-12 07:28 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:JDBC中使用Java8的日期LocalDate和LocalDateTime操作mysql、postgresql
[b]前言[/b] 相信大家应该都知道,在实体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,话不多说了,来一看看详细的介绍吧。 [b]一:MySQL[/b] [b]首先创建表:[/b]
create table tb_java8date (id int not null primary key auto_increment,t_date date, t_time time, t_datetime datetime);
[b]然后,加入mysql的驱动[/b]
<dependency> 
 <groupId>mysql</groupId> 
 <artifactId>mysql-connector-java</artifactId> 
 <version>5.1.37</version> 
</dependency> 
[b]上面说了,数据库驱动的版本不能低于4.2,如何判断呢?[/b] 直接打开数据库驱动jar,里面有个META-INF/MANIFEST.MF文件 [img]http://files.jb51.net/file_images/article/201709/201796110502671.png?20178611513[/img] [b]注意这里,必须要至少是4.2[/b] [b]JDBC代码如下:[/b]
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.time.LocalDate; 
import java.time.LocalDateTime; 
import java.time.LocalTime; 
 
public class App { 
 public static void main(String[] args) throws Exception { 
  Class.forName("com.mysql.jdbc.Driver"); 
  Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.1.100:3306/db_java8","root","root123"); 
  PreparedStatement st = conn.prepareStatement("insert into tb_java8date (t_date,t_time,t_datetime)values(?,?,?)"); 
  st.setObject(1, LocalDate.now()); 
  st.setObject(2, LocalTime.now()); 
  st.setObject(3, LocalDateTime.now()); 
  st.execute(); 
  st.close(); 
  conn.close(); 
 } 
} 
[b]运行,查询数据库[/b]
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)
看到已经成功插入到数据库中去了 [b]如果你使用的mysql-connector-java版本低于5.1.37,则数据库的驱动版本低于4.2,运行会报如下错误:[/b]
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 
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3845) 
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783) 
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447) 
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594) 
 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545) 
 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901) 
 at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1193) 
 at com.pp.App.main(App.java:18) 
[b]二:PostgreSQL[/b] [b]首先创建表:[/b]
create table tb_java8date (id SERIAL not null primary key,t_date date, t_time time, t_datetime timestamp);
[b]然后,加入PostgreSQL的数据库驱动[/b]
<dependency> 
 <groupId>org.postgresql</groupId> 
 <artifactId>postgresql</artifactId> 
 <version>9.4.1212</version> 
</dependency> 
[b]注意这里添加的数据库驱动版本最低要是4.2,检验方法和上面类似[/b] [img]http://files.jb51.net/file_images/article/201709/201796110632783.png?20178611641[/img] [b]JDBC代码如下:[/b]
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.time.LocalDate; 
import java.time.LocalDateTime; 
import java.time.LocalTime; 
 
public class App { 
 public static void main( String[] args ) throws Exception { 
  Class.forName("org.postgresql.Driver"); 
  Connection conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/pg_java8","admin","123456"); 
  PreparedStatement st = conn.prepareStatement("insert into tb_java8date (t_date,t_time,t_datetime)values(?,?,?)"); 
  System.out.println(st.getClass()); 
  st.setObject(1, LocalDate.now()); 
  st.setObject(2, LocalTime.now()); 
  st.setObject(3, LocalDateTime.now()); 
  st.execute(); 
  st.close(); 
  conn.close(); 
 } 
} 
[b]运行,然后查询数据库表[/b] [img]http://files.jb51.net/file_images/article/201709/201796110710519.png?20178611718[/img] 发现,已经成功执行 [b]如果你加入的依赖,数据库的驱动版本低于4.2,运行会报如下错误:[/b]
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. 
 at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:1051) 
 at com.pp.App.main(App2.java:16) 
以上只是演示了mysql,postgresql两个数据库,其他的数据库,请自行测试。我这里就不演示了,方法都类似。 [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部