0

I need your help writing my java program, I want a way to connect to a server without specifying the database. Then, execute a query on the server that lists the databases, the user chooses one, so the program connects to the chosen database. The server is a Microsoft SQL server. For now, I am using jpa persistence.xml to connect to the server and the database, but this way is static, I have to specify the database within persistence.xml to connect to it. The way I want it is to not specify the database, just specify the server in the persistence file. Then, execute a query on the server like:

SELECT name FROM master.sys.databases

get the names, then the user choose one of the databases, and the program would connect to the database specified.

  • 1
    have a look at here, it is not Microsoft SQL though: https://www.tutorialkart.com/java/list-databases-in-mysql-server-using-java/ – eugen Feb 25 '20 at 04:55
  • Thanks Eugene, but this is not the main problem, the main problem is to put the name of the specified database into something that would make the connection to the specified database. connecting to the server and executing a query is not the main problem. – Ismael Alsabea Feb 25 '20 at 05:33
  • Do you have any questions about your code? – NomadMaker Feb 25 '20 at 05:42
  • NomadMaker, No, its not a code question, I don't have a way to connect to a database dynamically, and I'm here looking for that way. – Ismael Alsabea Feb 25 '20 at 06:28
  • Java has an entire api devoted to connecting to sql databases. Just Google java sql. – NomadMaker Feb 25 '20 at 07:00
  • NomadMaker, yes, there are two, JDBC and JPA, I am fully aware of them amd know how to implement them, but there is no one tutorial or set of instructions for dynamic database connection. For both, I have to specify the database. The thing that I don't want to. I want to just connect to the server, and then based on user input connect to the database, the mechanism of connecting to the database, after you established server connection, is the thing that I am looking for. – Ismael Alsabea Feb 25 '20 at 17:20

0 Answers0