Best Way to Transfer Data Between Two JDBC Connections?

July 10, 2025 5 answers
Asked by _chapter break
Hey everyone! I often find myself needing to transfer data between two different databases, each accessed through its own JDBC connection. I usually just maintain two connections, retrieve a ResultSet from the first, and manually write the data into the second database. But I'm wonderingโ€”are there more efficient or principled methods to handle this? Are there any frameworks, like JOOQ, that simplify this process? I'd love to hear your recommendations! Thanks!

Answers

MIDNIGHT_dispatch
For the best performance, utilize the fastest methods available for each database, like using COPY for PostgreSQL or BULK INSERT for SQL Server. You can also execute these processes in parallel. If you're dealing with PostgreSQL, a good strategy is to create an UNLOGGED temporary table, load the data quickly with COPY, set up any necessary indexes afterward, and then swap it with the main table or perform an upsert.
Reply to this answer
~pause.at.step
Whatโ€™s your end goal here? Are you looking to sync data between these databases? Is the plan for one database to eventually take over the other? Understanding your use case would really help in offering specific advice.

_chapter break: From my initial post, it seems like I'll be transferring data from table T1 in DB1 to table T2 in DB2 once a day with some minor changes. I'm looking for a way to do this directly from DB1 to DB2 without needing to load the data onto an ETL machine.

Reply to this answer
board.game.night
You could consider using two data sources, where you run the query on the first data source, process the results, and then save them using the second data source.
Reply to this answer
purlwise_4
You might want to set up a remote connection between the two databases. This way, you could run a command like INSERT INTO LOCALTABLE FROM REMOTEVIEW to transfer the data directly.
Reply to this answer
maple.68
One option is to look into whether the databases you're using can connect to each other directly, allowing you to run SQL queries on remote tables. This could streamline the process if it's a recurring task.

cursive learner: That's not feasible in my case since the databases are from different vendors.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions