Updates from April, 2017 Toggle Comment Threads | Keyboard Shortcuts

  • shashankgee 9:43 am on 11 Apr 2017 Permalink | Reply  

    Exact meaning of Class.forName(); in JDBC 

    Before going to discuss, need to discuss about Drivers.

    Inorder to connect a  java Application  with database ,  we need to follow the below important steps

    1. Use any JDBC  Driver class.
    2. This JDBC Driver class has to be instantiated.
    3. And Instantiated Driver class should be Registered with DriverManager class

    For Example:

    Let’s take Type1-Driver(sun.jdbc.odbc.JdbcOdbcDriver) which comes with JDK by Default

    So Inorder to connect  a java application with  database with this driver we have to write following statements.

    sun.jdbc.odbc.JdbcOdbcDriver driver=new sun.jdbc.odbc.JdbcOdbcDriver();
    
    DriverManager.registerDriver(driver);
    
    

    Ok.Then what is Class.forName(“com.Anyclass”) ?

    Basically Class.forName("com.Anyclass");   used to load a class.

    while loading a class, if that class contains static blocks, and any code which is in that static blocks,  will be executed.

    So if write a statement  for Ex:      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

    code in static block of this driver will be executed while class is loading.

    All most all JDBC drivers contains static blocks and these static block contains the code to

    1) Instantiate the driver class

    2) Register the driver with DriverManager.

    Following  is the  static block in JdbcOdbcDriver

    
    static{
    
    JdbcOdbcTracer jdbcOdbcTracer= new JdbcOdbcTracer();
    
    if(jdbcOdbcTracer.isTracing()){
    
    </pre>
    <pre>jdbcOdbcTracer.trace("JdbcOdbcDriver class loaded");</pre>
    <pre>
    }
    
    sun.jdbc.odbc.JdbcOdbcDriver driver=new sun.jdbc.odbc.JdbcOdbcDriver();
    DriverManager.registerDriver(driver);
    
    }
    
    
    Advertisements
     
  • shashankgee 6:26 pm on 14 May 2014 Permalink | Reply  

    Exact Meaning of Class.forName(); in JDBC 

    Before going to discuss, need to discuss about Drivers. Inorder to connect a  java Application  with database ,  we need to follow the below important steps

    1. Use any JDBC  Driver class.
    2. This JDBC Driver class has to be instantiated.
    3. And Instantiated Driver class should be Registered with DriverManager class

    For Example:

    Let’s take Type1-Driver(sun.jdbc.odbc.JdbcOdbcDriver) which comes with JDK by Default So Inorder to connect  a java application with  database with this driver we have to write following statements.

    sun.jdbc.odbc.JdbcOdbcDriver driver=new sun.jdbc.odbc.JdbcOdbcDriver();
    
    DriverManager.registerDriver(driver);
    
    

    Ok.Then what is Class.forName(“com.Anyclass”) ? Basically Class.forName("com.Anyclass");   used to load a class. while loading a class, if that class contains static blocks, and any code which is in that static blocks,  will be executed. So if write a statement  for Ex:      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  code in static block of this driver will be executed while class is loading. All most all JDBC drivers contains static blocks and these static block contains the code to 1) Instantiate the driver class 2) Register the driver with DriverManager.

    Following  is the  static block in JdbcOdbcDriver

    
    static{
    
    JdbcOdbcTracer jdbcOdbcTracer= new JdbcOdbcTracer();
    
    if(jdbcOdbcTracer.isTracing()){
    jdbcOdbcTracer.trace("JdbcOdbcDriver class loaded");
    }
    JdbcOdbcDriver driver=new JdbcOdbcDriver();
    try{
    DriverManager.registerDriver(driver);
    }
    catch(SQLException sqlException){
        if(jdbcOdbcTracer.isTracing()){
               jdbcOdbcTracer.trace("Unable to register Driver");
        }
    }
     }
     

    summary: 

    Class.forName(“com.any.Driver”);    Internally  loads the Driver class, while loading the class

    1. It instantiates the Driver class
    2. Register the Driver class with DriverManager.
     
  • shashankgee 10:29 am on 8 May 2014 Permalink | Reply  

    Difference Between Statement and Prepared Statement 

    Advantages of PreparedStatment:

     

    1.  If you want to execute a query in a loop (more than 1 time), prepared statement can be faster.
    2. It improves the Performance of the application by Avoiding   compilation of multiple queries and preparing them each and every time .
    3. Parameterized query is a good way to avoid SQL Injection, that is only available in PreparedStatement.
    4. PrepareStatement  interface accepts Input Parameters at Runtime where as Statement interface cannot accept.
    5. It is easier to insert or update the SQL 99 data type columns, such as BLOB,CLOB  or OBJECT with the help of setXXX method.

    Disadvantages of PreparedStatment:

     

    1.    It can represent only one SQL statement. Threfore, we cannot execute more than one  SQL statement by a single                            PreparedStatement.
    2.  It Supports to add only a single SQL statement to be executed for multiple times with different set of values it a batch.
     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel