0

I understand that Connection is an interface and what are all the basic ways to get the Connection using DriverManager ? But what it does, it creates and opens a connection and returns it. So, is there a way to create a Connection or any similar object in Java which can give the metadata, dbName etc properties from a connection String?

Adonis
  • 4,670
  • 3
  • 37
  • 57
Madhura
  • 551
  • 5
  • 18
  • 1
    Why don't you parse directly the connection string? Take a look at: https://stackoverflow.com/a/11733697/2508770 – Emax Aug 22 '17 at 12:13
  • @Emax He is talking about database connections; not URLs?! – GhostCat Aug 22 '17 at 12:21
  • Use [mockito](https://examples.javacodegeeks.com/core-java/mockito/mockito-mock-database-connection-example) to mock DB connection – a_a Aug 22 '17 at 12:23
  • @GhostCat "or any similar object in java which can give me the metadata, dbName etc properties from a connection String", the connection string is an url so what's the problem of parsing it? – Emax Aug 22 '17 at 12:24
  • Plese refer to get the db details using connection object. https://stackoverflow.com/questions/7116517/how-to-get-sqlserver-database-name-from-datasource-name-in-java – Suresh Kb Aug 22 '17 at 12:26
  • @Emax a JDBC URL is an opaque object, the JDBC specification requires nothing more than `jdbc::`, everything after `jdbc::` is free to a specific driver implementation, and although it may look like a lot of drivers follow the same conventions, they do not all do that, and there are always subtle differences. – Mark Rotteveel Aug 22 '17 at 12:32
  • To get the connection details from a connection object, i will have to establish a connection first. Right now I am parsing the connection string itself using substring etc but if in future there is a change in connection string, i will have to change my code as well. Hence I was looking for something like MSSqlConnectionString object – Madhura Aug 22 '17 at 12:32
  • @MarkRotteveel Thank you, I wasn't understanding the question – Emax Aug 22 '17 at 12:34
  • Why are you parsing a connection string at all, it is - IMHO - the wrong way to go about things. If you need to manage this in some way, it would be better to use a driver-specific `DataSource` instead as it usually exposes all available properties as a collection of getters and setters. – Mark Rotteveel Aug 22 '17 at 12:35
  • @MarkRotteveel Even to get the Data Source I will have to connect to the database first. I have a connection String But, there are certain places where I need the dbName. Its a legacy code hence we wanna implement new changes with minimum interference in the existing code – Madhura Aug 22 '17 at 12:39

1 Answers1

-1

No, it's not possible to obtain a Connection without connecting (unless you stubbed the interface yourself).

It sounds like your database name is hardcoded somewhere in the existing code. If so, perhaps you could extract it to a constant that could be referenced by your new code too. Then you'll have the database name directly as a String instead of indirectly via the Connection object. If the database name is embedded inside a connection URL, you might have to separate it out and construct the URL. E.g.

public static final String DATABASE_NAME = "MyDatabase";
private static final String CONNECTION_URL = "jdbc:sqlserver://localhost;databaseName=" + DATABASE_NAME;

Even better would be to store the connection details separately from the code—e.g. in a properties file that is read when you program starts up—though that would require slightly more change to the existing code.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66