Getting started with JDBC (2)

Time:2022-6-17

The first thing you need to do is to establish a connection with the DBMS you want to use. This consists of two steps: loading the driver and establishing a connection.

Load driver
Loading the driver requires only a very simple line of code. For example, if 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 documentation will tell you the class name you should use. For example, if the class name is jdbc Driverxyz, you will load the driver with the following code:

    Class.forName(“jdbc.DriverXYZ”);

You do not need to create an instance of the driver class and register it with the drivermanager because class Forname will automatically load the driver class. If you have ever created an instance yourself, you will create an unnecessary copy, but it will not cause any harm.

Once the driver classes are loaded, they can be used to establish a connection to the database.

Establish connection
The second step is to establish a connection with the DBMS with the appropriate driver classes. The following codes are general practice:

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

This step is also very simple. The most difficult thing is how to provide the URL. If you are using the jdbc-odbc bridge, the JDBC URL will start with jdbc:odbc: the remaining URLs are usually your data source name or database system. Therefore, suppose you are using ODBC to access an ODBC data source called “Fred”, and your JDBC URL is jdbc:odbc:fred. Replace “mylogin” and “mypassword” with the user name and password of your DBMS login. If you log in to the database system with a username of “Fernanda” and a password of “J8”, you can establish a connection with the following two lines of code:

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 document will tell you what subprotocol to use, that is, the part behind JDBC in the JDBC URL. For example, if the driver developer registers acme as a subprotocol, the first and second parts of the JDBC URL will be jdbc:acme. The driver documentation will also tell you the format of the remaining JDBC URLs. The last part of the JDBC URL provides information to locate the database.

If the driver you loaded identifies the driver provided to the drivermanager The JDBC URL of getconnection. The driver will establish a connection to the specified DBMS according to the JDBC URL. As the name suggests, the drivermanager class manages all the details of establishing a connection for you behind the scenes. 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。

  DriverManager. The 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.