This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi This is a very common scenerio that we use in any jdbc application ...
Class.forName(//some jdbc.odbc class name should be declared here)
now when we read the documentation of forName method it's saying that it is used to load the particular class. And the same thing we can do by import statement in the starting of the program. Now this is leading a confusion that why we use Class.forName method when we can import that Package and if we cann't import those classes then what is the exactly usage of Class.forName functionalty. Please reply asap with appropriate functional difference.
The Best way to predict your future is to create it - Every great individual common man
If you import the class, you would need to change the import and recompile the class to use a different driver. JDBC is designed to create a lose coupling between the driver and JDBC code, and you lose many of the benefits of JDBC by hard-coding the driver. Admittedly you may not always all these features from JDBC, but why rule them out if you may need them later?
Consider these two pieces of code, which do the same thing:
In both cases the class would have to be changed if the driver changed, but consider this variation on the first example:
Now we haven't defined how this code gets the driver name from the preoprties, but you can do it any of a number of ways. The point is that the driver type is now a matter of configuration, and can be changed without requiring a recompilation of the code. Whenever you aim to distribute code or support multiple databases easily you need to do this.
There is a little bit more to it, such as using standard SQL and not using the additional methods on the DRiver classes, but this should give you an idea of what I mean.