This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I am using JDBC Type 3 driver for Oracle database. I am using classes12.jar. In this we have a class called oracle.jdbc.driver.OracleDriver. When we are creating a connection with a database first statement we write is
Class.forName("oracle.jdbc.driver.OracleDriver");
Can anybody tell me exactly how this functions? I mean when we say Class.forName("ABC"), this causes class named ABC to be initilized or it creates a copy of the class ABC.
What happens to copy of class ABC if it is already there?
~ Amit [ July 27, 2005: Message edited by: Bear Bibeault ]
You'll find better answers if you search the forum, but the short answer is that 'Class.forName() causes the class to be loaded, and when Drivers are loaded they register themselves with the DriverManager. If you call it multiple times the class will only load itself once and register iteself once.
Makarand Parab
Ranch Hand
Joined: Dec 10, 2004
Posts: 121
posted
0
Hi Okay here is the answer. When u say Class.forName("ABC"); Class.forName gets executed. it loads the class "ABC". In class ABC, there is a static block which gets executed as stated below
public class ABC { ABC abc = null ;
static { if(abc == null) { //loads and creates a new object if it does not exist ABC abc = new ABC(); //then it registers with the Driver Manager using DriverManager.registerDriver(abc); // this is a static method check the DriverManager API } } }
Hope the above code resolves ur issue. Let me know if u want any more help or check out for JDBC specification pdf on sun site.
Regards Makarand Parab
sinasi susam
Ranch Hand
Joined: Jul 15, 2005
Posts: 67
posted
0
What do you mean by "class loading?" where are the classes loaded when its passed?
If you create your own Driver, your code becomes 'coupled' with that Driver. You won't be able to use a different database without recompiling your code.
If you use Class.forName(string) and DriverManager.getConnection(string), then you can pass the string values in at run time and change it each time the application is run, or even distribute your application and allow users to choose their own dtabaase rather than being forced to use just one.