I am not clear with what does Class.forName().
I read that Class.forName is used to dynamically load a class.
For e.g. the
jdbc driver that we use is loaded when u
write
Class.forName("com.mysql.jdbc.Driver");
but one can also do it with extracting the jar file and importing the package
Using the code below i have connected to the database so what is the benefit of using
Class.forName().
package foo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import com.mysql.jdbc.Driver;
public class l
{
public static void main(
String args[])
{
try
{
Driver.class.newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/effectiveinfo","root","rkn09876");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select * From EmpMst Limit 10");
while(rs.next())
{
System.out.println(rs.getString(1));
}
rs.close();
System.out.println(con.getCatalog());
}catch(Exception e)
{
System.out.println("Exception" + e.toString());
}
}
}