Results 1 to 7 of 7

Thread: Class.forName()

  1. #1
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default Class.forName()

    hi friends,
    i am confused in using Class.forName() method.
    it simply load the specified class to a class object. and i got that.

    Class <?> c = Class.forName("Class4Name");
    Object o = c.newInstance();

    here class os loaded in to c and we can use 'c' to make object of that class.

    but in database driver loading code i found

    Class.forName("oracle.jdbc.driver.OracleDriver"); con=DriverManager.etConnection( "jdbc:oracle:thin:@machine_name:1521:database_name ",
    "scott",
    "tiger");

    my doubt is where it stores the class object returned by Class.forName() method? there is nothing to receive it. in first code we have a Class object 'c' to receive.

    please clear my doubt...
    thanks in advance.......
    Last edited by jomypgeorge; 12-22-2010 at 10:14 AM.

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    When you invoke Class.forName(), the system class loader will load the class from the specified string. The loaded classes are stored in a special area of the JVM memory called the 'permanent generation', or PermGen. This holds all of the class metadata. Once a class is loaded it stays in this spot for the lifetime of the Java VM. We do not need to return the class reference and assign it to our local Class<?> c variable for it to remain loaded.

    In the case of JDBC drivers, the common practice of calling Class.forName() is to have the vendor-specific class perform its internal registration with the JDBC driver manager.

    Basically inside a JDBC implementation is likely a static { } block of code that obtains a handle to the DriverManager singleton, and registers itself as the handler for the JDBC url prefix, for example, a JDBC URL starting with "jdbc:oracle:...", when a request for a connection to a URL of this format is seen, the Driver Manager will then be able to find the previously mapped handler, which is the Oracle driver implementation. It's odd how they did that, but perhaps back in Java 1.? early days before they had the modern facilities such as Springframework, activation framework, annotations, JNDI contexts, and other inversion of control or configuration and initialization mechanism, the best and simplest way to have the JDBC class register itself with the DriverManager was thru this static { } piece of code in the implementation class, which happens on the loading of the class. And since we wouldn't normally ever need to create an instance of the class, or invoke the class directly in an import, the Class.forName() is good.

    It is also convenient perhaps to wrap in a try..catch, so if the class loading fails, the application can assert that the specific database driver is not found and perhaps to try to look for a different database driver before throwing an error.

  3. #3
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks travishein.......

    but how it find the loaded handler?......

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    DriverManager is built into the Java runtime. Loading the class with Class.forName() keeps the loaded class in the class loader memory. Later on when a connection is required, DriverManager will create an instance of that class and return it.

  5. #5
    jomypgeorge is offline Member
    Join Date
    May 2010
    Posts
    90
    Rep Power
    0

    Default

    thanks for your reply

  6. #6
    aparnaverma's Avatar
    aparnaverma is offline Member
    Join Date
    Apr 2011
    Location
    Meerut
    Posts
    3
    Rep Power
    0

    Question error in loading problem

    hey guyz,,I am facing the same problem.Can you plz help me to find what should I do ???:confused:
    This is the code:

    import java.sql.*;
    import java.io.*;
    public class blob {

    public static void main(String[] args)throws Exception {
    Connection con=null;
    Statement st=null;
    ResultSet rs=null;

    try{
    Class.forName("oracle.jdbc.OracleDriver");
    System.out.println("Oracle JDBC driver loaded ok.");
    }
    catch (ClassNotFoundException c){
    System.out.println("Error in Loading Driver");
    }
    try{
    String connectURL="jdbc:oracle:thin:@localhost:1521:XE";
    con=DriverManager.getConnection(connectURL,"system ","password");
    st=con.createStatement();
    //String createTable = "create table blobTable( columnOne blob)";
    // st.execute("drop table blobTable");
    //st.execute(createTable);
    InputStream tempStream =new FileInputStream(new File("H:\\captcha pix:\\9.gif"));
    System.out.println(tempStream.available());
    PreparedStatement ps = con.prepareStatement("insert into blobTable values(?)");
    ps.setBlob(1, tempStream);
    ps.executeUpdate();


    } catch(Exception e){
    e.printStackTrace();
    //StringBuffer sb=new StringBuffer();
    //SQLWarning s=ps.getWarnings();
    //if(s!=null){
    //while(s!=null){
    //sb.append(s.getErrorCode());
    //sb.append(s.getMessage());
    //s=s.getNextWarning();
    //}
    //}

    }
    //finally{
    //st.close();
    //con.close();
    //}
    }
    }




    The error is
    No suitable driver found for "jdbc:oracle:thin:@localhost:1521:XE"

    plz guyz help me..what is the problem??:(

  7. #7
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    the parameter inside the Class.forname is the type of driver you want to use for the connection between java and your database

Similar Threads

  1. Class.forName and casting
    By martinmarinov in forum Advanced Java
    Replies: 16
    Last Post: 06-09-2010, 02:42 PM
  2. Class.forName(ObjectName).newInstance()
    By nurkhasanah in forum AWT / Swing
    Replies: 2
    Last Post: 05-07-2010, 04:51 AM
  3. Class.forName Exception
    By Moncleared in forum Advanced Java
    Replies: 5
    Last Post: 02-21-2009, 06:08 AM
  4. about Class.forName
    By angus203 in forum New To Java
    Replies: 0
    Last Post: 11-25-2007, 04:47 AM
  5. question about Class.forName()
    By oregon in forum JDBC
    Replies: 4
    Last Post: 08-01-2007, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •