I was just wondering that why would the driver manager class provides many overloaded
methods to get connection to the database. Some of them are
getConnection(String url, String user, String password)
getConnection(String url, Properties info)
The others can be found at http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/DriverManager.html
You need to have the same set of information before hand you get a connection to database
in each case but each of the getConnection() methods has a different way to access that information. I have seen people using all of them without any preference for a particular(as far as I feel).
My question is that is there any preference in terms of coding style because of which use of one may be preferred over other. If not isn’t this overloading redundant?
Technically the
getConnection(String url, Properties info)one is much cleaner if you programmatically collect the driver properties. The original thought was that people would configure a driver using a GUI, using the getPropertyInfo method of theDriverinterface to collect all possible properties for selection and configuration in the GUI.You then simply needed to store the (base) JDBC url and a
.propertiesfile with all driver properties (or a (serialized) copy of the properties object) to make connections. I am not sure if anybody ever used it in that way.So the original idea never really caught on (AFAIK), and people preferred storing JDBC urls with all properties as part of the URL and having username and password seperately, thus using
getConnection(String url, String user, String password)or maybe evengetConnection(String url)by including the username and password in the URL.With the introduction of
DataSourcein JDBC 2 (standard extension) I believe there was even talk about deprecating and ultimately removing DriverManager in favor of always using DataSources (as they used the JavaBeans style of configuration); although that actually never happened.