You will need a transaction manager that will manage the two-phase commit between the two databases.
Java EE application servers have such transaction managers built it, so if you are doing this within a web application (or actually, within an
EJB)
you should be OK - but check the app server's documentation for transaction handling
If you are write a Java SE app, then I suggest you find a
java transaction manager and use it within your app.