The date localdate and localdatetime of Java 8 are used in JDBC to operate MySQL and PostgreSQL

preface

I believe everyone should know that Java can be used in entity sql. Date、java. sql. Timestamp、java. util. Date to map to the date, timestamp, datetime and other fields of the database

However, Java sql. Date、java. sql. Timestamp、java. util. Date classes are not easy to use, and many methods are outdated.

There are some new APIs in Java 8. Localdate, Localtime and localdatetime are very easy to use

If you want to use the date localdate and localdatetime of Java 8 in JDBC, you must require that the version of database driver must not be lower than 4.2

The following will demonstrate how to use the date localdate and localdatetime of Java 8 in JDBC to operate MySQL and PostgreSQL. I won't say much. Let's take a look at the detailed introduction.

1: Mysql

First create the table:

Then, add the driver of MySQL

As mentioned above, the version of database driver cannot be lower than 4.2. How to judge?

Directly open the database driver jar, where there is a meta-inf / manifest MF file

Note here that it must be at least 4.2

The JDBC code is as follows:

Run, query database

See that it has been successfully inserted into the database

If you use MySQL connector java version lower than 5.1 37, the driver version of the database is lower than 4.2, and the following error will be reported when running:

2: PostgreSQL

First create the table:

Then, add the database driver of PostgreSQL

Note that the minimum version of the database driver added here is 4.2, and the inspection method is similar to the above

The JDBC code is as follows:

Run, and then query the database table

Found that has been successfully executed

If you add a dependency and the driver version of the database is lower than 4.2, the following error will be reported:

The above only demonstrates two databases, MySQL and PostgreSQL. Please test the other databases by yourself. I won't demonstrate here. The methods are similar.

summary

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>