Can you specify the database in a query in Java?

0

I need to connect to a different database at runtime and I do not have the slightest idea how to do it in JPA. I have a database that tells me which one I should connect to according to some parameters to follow. I make my query in the first database and then I want to connect to the second one and that's where I'm standing.

How can I do it? Can I place the database to which I wish to obtain the data?

Example:

Select 
    * 
from 
    BD1.Empleados;
    
asked by Lenin 30.05.2017 в 17:43
source

1 answer

0

The answer is no, in the query you can not change the connection data of the database, but there is a way to do what you want.

You must change the JPA configuration data that is hosted in Persistence.xml. The instance can be accessed with the following elements:

EntityManagerFactory managerFactory = null;
Map<String, String> persistenceMap = new HashMap<String, String>();

persistenceMap.put("javax.persistence.jdbc.url", "<url>");
persistenceMap.put("javax.persistence.jdbc.user", "<username>");
persistenceMap.put("javax.persistence.jdbc.password", "<password>");
persistenceMap.put("javax.persistence.jdbc.driver", "<driver>");

managerFactory = Persistence.createEntityManagerFactory("<current persistence unit>", persistenceMap);
manager = managerFactory.createEntityManager();

I leave the URL where the operation is explained ( link )

    
answered by 30.05.2017 в 18:22