8

Is there any way to change postgreSQL current database from java application? or is there any equivalent for \connect report ? I know i can do this by changing JDBC connection string, but is there any better alternative?

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
MoienGK
  • 4,544
  • 11
  • 58
  • 92
  • It is always better to have different datasources configured for different databases running in the same server. A connection at any point will point to only one database and you cannot change it. You can only open a new connection – Abhijith Nagarajan Oct 28 '13 at 14:23
  • maybe i should have described my solution, i need to list all databases to user and connect to one of them on his/her demand. – MoienGK Oct 28 '13 at 14:27
  • 2
    You can't CHANGE a connection. You can either OPEN or CLOSE it. If you want to work with a different databases, simple create a new connection to it. If you want don't want the previous connection, simply close it. Changing a data-source simply doesn't make sense. – user2339071 Oct 28 '13 at 14:27
  • 1
    @dave, So `let the user make the selection, and append it to the JDBC connection string`. If the user wants to change the connection, they will select which new database to connect to, right. So, close the previous connection, and create a new connection to the newer database. – user2339071 Oct 28 '13 at 14:30
  • @user2339071 thats what i am doing now, question raised when i tried to find simpler or maybe more elegant way. from what i learned from comments current way is the elegant one . – MoienGK Oct 28 '13 at 14:33
  • @dave LOL. Optimizations are hard. But, I would like to tell you that, this isn't an optimization problem. The language simply doesn't support it as of yet. Maybe Java 9. ;) – user2339071 Oct 28 '13 at 14:38

0 Answers0