简体   繁体   中英

JDBC connectivity persistence issue

I understand that jdbc involved these steps

  • Import the packages,
  • Register the JDBC driver,
  • Open a connection,
  • Execute a query,
  • Extract data from result set,
  • Clean up the environment.

I have 2 java files one is for GUI (Swing) and another is normal Java file. I have many input fields in GUI and send them to backend file for processing using JDBC. I have many methods like this in my Java file. The problem is in each method I have to the entire steps (mentioned above) in each of these methods. As a result of this it has become slow. I wanted to optimize it making first 3 steps and final steps mentioned above only once.

How to achieve this?

Sounds like you need a Connection Pool . There are lots of questions related to connection pooling on Stack Overflow - there's even a tag for it!

https://stackoverflow.com/questions/tagged/connection-pooling

In broad outline, here are several approaches, in (roughly) increasing order of complexity:

  • Stick with what you're doing, abstracting useful utilities as you learn. It's laborious, but it allows maximal cutomization.

  • Adopt a lightweight JDBC helper library; several are cited here .

  • Dive in and learn the Java Persistence API .

It's entirely possible to do the first while exploring the second or third.

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