JDBC entry (2)-establish a connection

Source: Internet
Author: User

Tutorial: getting started with JDBC by Maydene Fisher
The first thing you need to do is to establish a connection with the DBMS you want to use. This involves two steps: loading the driver and establishing a connection.

Load driver
Loading the driver requires only a very simple line of code. For example, you want to use the JDBC-ODBC bridge driver, you can load it with the following code:

Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");

Your driver document will tell you the class name you should use. For example, if the class name is jdbc. DriverXYZ, you will use the following code to load the driver:

Class. forName ("jdbc. DriverXYZ ");

You do not need to create an instance of the driver Class and register it with DriverManager, because calling Class. forName will automatically load the driver Class. If you have created your own instance, you will create an unnecessary copy, but it will not bring any harm.

After the Driver class is loaded, they can be used to establish a connection with the database.

Establish a connection
The second step is to use the appropriate driver class to establish a connection with the DBMS. The following code is a common practice:

Connection con = DriverManager. getConnection (url, "myLogin", "myPassword ");

This step is also very simple, and the most difficult is how to provide a url. If you are using a JDBC-ODBC bridge, the jdbc url will start with jdbc: odbc: The remaining URL is typically your data source name or database system. Therefore, if you are using ODBC to access an ODBC data source named "Fred", your jdbc url is jdbc: odbc: Fred. Replace "myLogin" and "myPassword" with your username and password for logging on to the DBMS. If the username you log on to the database system is "Fernanda" and the password is "J8", you only need the following two lines of code to establish a connection:

String url = "jdbc: odbc: Fred ";
Connection con = DriverManager. getConnection (url, "Fernanda", "J8 ");
If you are using a third-party JDBC driver, the document will tell you what subprotocol to use, that is, the part that is placed behind JDBC in the jdbc URL. For example, if the driver developer registers acme as subprotocol, the first and second parts of jdbc url are jdbc: acme. The driver documentation also tells you the format of the remaining jdbc url. The last part of the jdbc url provides information for locating the database.

If the driver you load recognizes the jdbc url provided to DriverManager. getConnection, the driver will establish a connection to the specified DBMS based on the jdbc url. As shown in the name, the DriverManager class is behind the scenes to manage all the details of establishing a connection for you. Unless you are writing a driver, you may not need to use any other method of this class. The only method that programmers need to use directly in this class is DriverManager. getConnection.

The DriverManager. getConnection method returns an open connection. You can use this connection to create JDBC statements and send SQL statements to the database. In the previous example, the con object is an open connection, and we will use it in future examples.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.