Skip to main content

Posts

Showing posts from January, 2011

class.forName(String className) used in JDBC unfolded

During many interviews I have taken , I have observed even seasoned programmers find it difficult to explain the use of class.forname() method while making a JDBC connection object. In this post, I am just trying to explain this, Here is a simple JDBC code, import java.sql.*; public class JdbcConCode { public static void main(String args[]) { Connection con = null; String url = "jdbc:mysql://localhost:3306/nilesh"; String driver = "com.mysql.jdbc.Driver"; String user = "root"; String pass = "nilesh"; try { Class.forName(driver); con = DriverManager.getConnection(url, user, pass); System.out.println("Connection is created...."); //TODO //rest of the code goes here } catch (Exception e) { System.out.println(e); } } } Let's go through the code above, the url indicates the location of the database schema, here the database is nilesh . Then there is Driver which is the fully qualified JDBC driver class name.