简体   繁体   中英

java.sql vs mysql.jdbc

While developing java apps that connects to database, it seems like we have two options:

  • Use the standard java.sql classes
  • Use the vendor specific classes (like we have com.mysql.jdbc.Connection and we also have java.sql.Connection ).

Which one is the recommended to use? Say I am currently using mysql. But in the future I might switch to oracle. Is there any performance bonus for using the vendor specific implementation?

Never go for vendor specific code, always use standard api(JDBC) and later on it will be easy to maintain and also easily portable across databases. Performance advantage of vendor specific code is negligible, you can better do with optimized queries, indexes, cacheing etc.

In most cases, you won't notice a performance difference.

The java.sql package represents a most common denominator between different databases, while the vendor-specific packages also expose some features which are exclusive to the database backend. But when you want to keep the option open to switch the database backend later, you should avoid these anyway, because it is possible that the new database got no equivalent functionality and you need to solve the problem in a completely different way, which could in the end require some large-scale refactoring of your entire application.

tl;dr: When you want to keep the option open to switch to Oracle or any other SQL database, use java.sql.

Or even better: use an object-relational mapping framework like Hibernate to do the database abstraction for you. They need a while to get used to, but afterwards you will be able to implement database functionality with a lot less man-hours and switching the database backend will be a one-line change in a configuration file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM