Results 1 to 6 of 6
- 09-15-2011, 08:33 AM #1
Member
- Join Date
- May 2011
- Posts
- 22
- Rep Power
- 0
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
My question is when you sayJava 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(); } } }
---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
- 09-15-2011, 08:37 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. :)
- 09-15-2011, 09:00 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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,
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.Java Code:conn = DriverManager.getConnection(url+dbName,userName,password);
- 09-15-2011, 09:58 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,463
- Rep Power
- 16
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.
- 09-15-2011, 10:17 AM #5
Member
- Join Date
- May 2011
- Posts
- 22
- Rep Power
- 0
Re: Help me in understanding Mysql database connectivity in java.
Thank you ..... Great help from you guys...:) I owe a lot to this forum...
- 09-16-2011, 06:58 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Help me in understanding Mysql database connectivity in java.
Hang on! You could learn a lot in near future.
Similar Threads
-
Java Database Connectivity
By mac0sX in forum New To JavaReplies: 2Last Post: 06-13-2011, 10:51 AM -
question about MySQL and java connectivity
By mr_anderson in forum JDBCReplies: 12Last Post: 07-21-2010, 04:30 AM -
Connectivity issue-MySQL and Java
By Pragya in forum JDBCReplies: 9Last Post: 01-27-2010, 05:28 PM -
Java Database Connectivity
By Java Tutorial in forum Java TutorialReplies: 2Last Post: 02-08-2009, 02:15 AM -
java with database connectivity
By thamizhisai in forum New To JavaReplies: 2Last Post: 04-26-2008, 10:53 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks