aspose file tools*
The moose likes JDBC and the fly likes Regarding JDBC.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Regarding JDBC...." Watch "Regarding JDBC...." New topic
Author

Regarding JDBC....

Nittin singla
Greenhorn

Joined: Jul 02, 2011
Posts: 24
I need to know what actually does following statements do in JDBC?

Class.forName(“driver class”);
Connection conn = DriverManager.getConnection(conn url, uname, pswd);


As per my knowledge, Class.forName() does dynamic loading of class mentioned in parameters but I am confused how the loaded class gets linked to this statement “DriverManager.getConnection(conn url, uname, pswd)” .


Also I want to know whether Class.forName can be used for any own created class because as per my understanding, “Class.forName(my own class)” and the object created using the new operator like Class a = new Class() differ only in the loading. One is static loading and the other one being lazy loading(dynamic)..

Does loading a class in JVM takes memory or not?

Can anyone please clarify my doubt or correct me if I am wrong…
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

Class.forName(...) indeed makes Java load the class that you specify dynamically. When the driver class is loaded, its static initializer blocks are executed. The driver class has a static initializer to register the driver with JDBC, so that when you call DriverManager.getConnection, JDBC knows where to find the driver for the URL that you specify.

You can use Class.forName(...) also for your own classes, and your understanding is correct. The advantage of Class.forName(...) is that the class that you are loading does not need to be known at compile time (it doesn't have to be in the classpath at compile time). This can be very useful for software that works with plug-ins, because you don't know in advance what plug-ins are going to be used.

Loading a class does cost memory, because the JVM has to store the content of the class (the bytecode of the methods etc.) in memory. But it doesn't matter if you load the class statically or dynamically, both will cost (approximately) the same amount of memory.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding JDBC....