Hello, young man!
The recommended is that you create a Factory of connections to the bank, a DAO (Data Access Object) to manipulate the access and acquisition of the information to the bank and, finally, one that allows you to validate whether the information is valid or not.
This way you will be able to maintain a high cohesion a low coupling.
See, in the highest layer, which I will call Validator, you receive your object with the user properties, such as login and password.
In this Validator you request the information to the bank through the DAO - which I would recommend to be injected - and then homologate if the values are consistent or not.
At the DAO you check which bank you should access, and then call your Factory. This can be by reading some system property, reading some external file, or even just asking another class.
Following this idea, if you have any problem in the validation, you only change in Validator; if you have problems in the query, change in DAO; finally, if it is a problem in the creation of the connection, change in Factory.
Any questions, just say the word!
Greetings
Edited
Here it is possible to find an example:
Example
Hello, Iago. Is this a simple academic project or a real application? In professional applications, connection management is usually delegated to a web container or framework. If it is a program made to learn, a simple way is to have a class that returns new connections to others. If you can give more details, it will be easier to help. Hug!
– utluiz
Hello. It’s both for learning and for a real @utluiz application. The application in the case is for android, cellular communication to sql server and vice versa.
– Iago Coutinho Campos
At least you should comment on why my topic is closed, no return can do anything.
– Iago Coutinho Campos
I did not vote to close, but I believe that the reason is that the details of the question are scattered by the comments and the question itself is wide, the tag about being for Android is missing, as it gives the impression that it can be for any Java program. If you want the question to be reopened, edit and add there the information that has been completed in the comments. In the OS, the question must contain all the necessary details.
– utluiz
I didn’t answer your question because I don’t work with Android, but reread now, I wanted to add one thing: keeping a connection open is not recommended simply because you will be consuming resources without need most of the time. In addition, the app can exit and return to memory at any time, so depending on where to store the object it can become "Stale" and at some point become unusable.
– utluiz