Getting Started with JDBC (ii) _JSP programming

Source: Internet
Author: User
Tags documentation odbc
The first thing you need to do is to establish a connection with the DBMS you want to use. This includes 2 steps: Loading the driver and establishing a connection.

Load Driver
Loading a 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 code below:

Class.forName ("jdbc.") Driverxyz ");

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

After loading the Driver classes, they can be used to establish a connection to the database.

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

Connection con = drivermanager.getconnection (URL, "MyLogin", "MyPassword");

This step is also very simple, the most difficult 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. So, suppose you are using ODBC to access an ODBC data source called "Fred", your JDBC URL is jdbc:odbc:Fred. Replace "MyLogin" and "MyPassword" with the username and password of your login DBMS. If you log on to the database system with the username "Fernanda" password "J8", you can establish a connection with just the following 2 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, or the part of the JDBC URL that follows JDBC. For example, if the driver developer registers Acme as Subprotocol, the first and second parts of the JDBC URL will be jdbc:acme. The driver documentation also tells you the format of the remaining JDBC URLs. The last part of the JDBC URL provides information about locating the database.

If you load a driver that identifies the JDBC URL provided to drivermanager.getconnection, the driver will establish a connection to the specified DBMS based on the JDBC URL. As the name shows, 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 of these other methods, and the only way a programmer needs to use it directly in this class is drivermanager.getconnection.

The Drivermanager.getconnection method returns an open connection that you can use to create JDBC statements and send SQL statements to the database. In the previous example, the Con object is an open connection, and we want to use it in a future example.

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.