What is the purpose Class.forName method?

Solution:

This method is used to load the driver that will establish a connection to the database.

Sample Class ClassLoader is shown below to demonstrate the usage of Class.forName() method.

Class.forName

public class ClassLoader 
{
 
   public static void main(String[] args) {
 
      try
      {
         Class cls = Class.forName("BasicClass");
        
         .....
        
         System.out.println("Class = " + cls.getName());
        
      }
        
     catch(ClassNotFoundException exception) 
      {
         System.out.println(exception.toString());
      }
     
}
0 answers