> establishing a connection to sql server ~ Online tutorial

establishing a connection to sql server

Establishing a Connection
The first thing you need to do is establish a connection with the DBMS you want to use.
This involves two steps:

(1) loading the driver and
(2) making the connection.

Loading Drivers
Loading the driver or drivers you want to use is very simple and involves just one line of
code. If, for example, you want to use the JDBC-ODBC Bridge driver, the following
code will load it:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Your driver documentation will give you the class name to use.
For instance, if the class name is jdbc.DriverXYZ ,
you would load the driver with the following line of code:
Class.forName("jdbc.DriverXYZ");
You do not need to create an instance of a driver and register it with the DriverManager
because calling Class.forName will do that for you automatically. If you were to create
your own instance, you would be creating an unnecessary duplicate, but it would do no
harm.
When you have loaded a driver, it is available for making a connection with a DBMS.
Making the Connection
The second step in establishing a connection is to have the appropriate driver connect to
the DBMS. The following line of code illustrates the general idea:
Connection con = DriverManager.getConnection(url,
"myLogin", "myPassword");


This step is also simple, with the hardest thing being what to supply for url .
If you are using the JDBC-ODBC Bridge driver, the JDBC URL will start with jdbc:odbc: .
 The rest of the URL is generally your data source name or database system. So, if you are using
ODBC to access an ODBC data source called " Fred, " for example, your JDBC URL
could be jdbc:odbc:Fred . In place of " myLogin " you put the name you use to log in to
the DBMS; in place of " myPassword " you put your password for the DBMS. So if you
log in to your DBMS with a login name of " Fernanda " and a password of " J8, " just
these two lines of code will establish a connection:

String url = "jdbc:odbc:Fred";
Connection con = DriverManager.getConnection(url, "Fernanda", "J8");



If you are using a JDBC driver developed by a third party, the documentation will tell
you what subprotocol to use, that is, what to put after jdbc: in the JDBC URL.


Buy It Now





Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

2 comments:

Sql Server JDBC Driver Download said...

Wow!!! very informative step by step post for establishing a connection to sql server.

Thanks for sharing helpful post.
Keep it up :)

Thanks,
Mike

Unknown said...

thanks sir for sharing this sir