Results 1 to 3 of 3
  1. #1
    lo2
    lo2 is offline Member
    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Default Help with this class implementing JDBC

    Hi there I have this class:

    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    public class ConnTest {
    	public static void main(String args[]){
    
    		String dbuser = "db_abc";
    		String passwd = "yourpassword";
    		
    		Connection conn;
    		int port = 1521;
    		try {
    			Class.forName("oracle.jdbc.driver.OracleDriver");
    			conn = DriverManager.getConnection(
    					"jdbc:oracle:thin:@localhost:"+port+":dbwc", dbuser, passwd);
    			conn.setAutoCommit(false);
    			
    		PreparedStatement s = conn.prepareStatement("Select * from Ships");
    		
    		ResultSet rs = s.executeQuery();
    		
    		while (rs.next()){
    			String sname = rs.getString(1);
    			String sclass = rs.getString(2);
    			String slaunched = rs.getString(3);
    			
    			System.out.println(sname + ":" + sclass + ":" + slaunched);
    		}
    		
    		} catch(SQLException e) {
    			System.out.println(e);
    		} catch(ClassNotFoundException e) {
    			System.out.println(e);
    		}
    
    	}
    }
    I would really like to get explained what is happening in these lines:

    Java Code:
    Class.forName("oracle.jdbc.driver.OracleDriver");
    			conn = DriverManager.getConnection(
    					"jdbc:oracle:thin:@localhost:"+port+":dbwc", dbuser, passwd);
    What does this Class.forName do? And where is the DriverManager object created?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Help with this class implementing JDBC

    Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.

    When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.
    You can read more on the following link: DriverManager (Java Platform SE 7 )

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    444
    Rep Power
    6

    Default Re: Help with this class implementing JDBC

    Class.forName("X") loads the class if it not already loaded on JVM. Typically its called dynamic class loading.
    Mak
    (Living @ Virtual World)

Similar Threads

  1. implementing class and object
    By mheyai09 in forum New To Java
    Replies: 2
    Last Post: 07-19-2012, 01:18 PM
  2. Implementing Class Files Into Driver
    By mike_ in forum New To Java
    Replies: 5
    Last Post: 12-07-2011, 12:00 AM
  3. Need Urgent Help in Implementing Interface Class
    By yel_hiei in forum New To Java
    Replies: 4
    Last Post: 07-29-2010, 01:42 PM
  4. need help with implementing class
    By ALH813 in forum New To Java
    Replies: 3
    Last Post: 10-01-2009, 11:04 PM
  5. Help implementing JDBC
    By mooey1232003 in forum New To Java
    Replies: 6
    Last Post: 07-11-2007, 10:15 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
  •