Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Eranga

Thread: Help me in understanding Mysql database connectivity in java.

  1. #1
    Anveshan is offline Member
    Join Date
    May 2011
    Posts
    24
    Rep Power
    0

    Unhappy Help me in understanding Mysql database connectivity in java.

    Hi all,

    I am new to Java. So please help in understanding basic program which connects java to Mysql database. Below is program

    Java Code:
    import java.sql.*;
    
    public class MysqlConnect{
        public static void main(String[] args) {
            System.out.println("MySQL Connect Example.");
            Connection conn = null;
            String url = "jdbc:mysql://localhost:3306/";
            String dbName = "jdbctutorial";
            String driver = "com.mysql.jdbc.Driver";
            String userName = "root"; 
            String password = "root";
            
            try {
                  Class.forName(driver).newInstance();
                  conn = DriverManager.getConnection(url+dbName,userName,password);
                  System.out.println("Connected to the database");
                  conn.close();
                  System.out.println("Disconnected from database");
            }
            catch (Exception e) {
                  e.printStackTrace();
            }
      }
    }
    My question is when you say
    ---Class.forName(driver).newInstance();
    A new "driver instance" is created but then where is it stored ?.. in the subsequent code there is no reference to the "driver instance" we have created. we are only calling DriverManager.

    to sum up :

    Class.forName(driver).newInstance(); creates a "Class" of type "com.mysql.jdbc.Driver" but then the created instance, where is it stored or loaded ? and in
    -- conn = DriverManager.getConnection(url+dbName,userName,pa ssword);
    how do we know Driver Manager is the one we have created ?...

    If I am totally ignorant of what I am asking then apologies..
    Last edited by Eranga; 09-15-2011 at 08:39 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Help me in understanding Mysql database connectivity in java.

    First of all, please use the code tags when you are posting again. Unformated codes are really hard to read. :)

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Help me in understanding Mysql database connectivity in java.

    Regarding your question.

    Have you ever heard about bootstrap class loader in JVM? For a moment think about the following line of code,

    Java Code:
    conn = DriverManager.getConnection(url+dbName,userName,password);
    Class.forName. That method what simply doing is load the Class using about class loader I have mentioned. When DriverManager.getConnection is called the driver class with the relevant jdbc connection URL which you have already defined.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default Re: Help me in understanding Mysql database connectivity in java.

    The DriverManager loads a driver based on the supplied url. Each Driver has a different url signature.
    The Class.forName() ensures that the class mapped to that URL has been loaded into the system to be found...there is no need to do newInstance().
    Indeed, I'm not sure the newer JDBC stuff even needs the Class.forname() these days, but it's still used out of habit I suppose.

  5. #5
    Anveshan is offline Member
    Join Date
    May 2011
    Posts
    24
    Rep Power
    0

    Default Re: Help me in understanding Mysql database connectivity in java.

    Thank you ..... Great help from you guys...:) I owe a lot to this forum...

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Java Database Connectivity
    By mac0sX in forum New To Java
    Replies: 2
    Last Post: 06-13-2011, 10:51 AM
  2. question about MySQL and java connectivity
    By mr_anderson in forum JDBC
    Replies: 12
    Last Post: 07-21-2010, 04:30 AM
  3. Connectivity issue-MySQL and Java
    By Pragya in forum JDBC
    Replies: 9
    Last Post: 01-27-2010, 05:28 PM
  4. Java Database Connectivity
    By Java Tutorial in forum Java Tutorial
    Replies: 2
    Last Post: 02-08-2009, 02:15 AM
  5. java with database connectivity
    By thamizhisai in forum New To Java
    Replies: 2
    Last Post: 04-26-2008, 10:53 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
  •