Establish a connection

Source: Internet
Author: User
Tags documentation odbc
Establish a link with the database
To build a link with the DBMS you must do the following two jobs: (1) load driver (2) Link set up

Load driver \ r

Loading the driver is very simple and requires only a single line of code. Like you, you want to drive with the Jdbc-odbc bridge, then you write:
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");
Your driver documentation will tell you the class name. If your class name is JDBC. DRIVERXYZ, then you change the code like this:
Class.forName ("jdbc.") Driverxyz ");
You do not need to create a driver instance and register with DriverManager because it is automatically loaded when you call Class.forName. If you do not create your own instance, it will not be used, it is superfluous, but it will not do any harm to the program.
When you load the driver, you can establish a connection with the DBMS.

Create a link

The second step is to establish a link with the DBMS. Look at the following code:

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

The second step is as simple as it is, and a little bit difficult is how that URL is provided, if you use the Jdbc-odbc Bridge driver, then the JDBC URL should be jdbc:odbc: Next is the name of your data source or database. So, if you use ODBC to access ODBC data sources, named Fred, then your JDBC URL should be written as jdbc:odbc:Fred. In "MyLogin" should fill in your login DBMS username, the following "MyPassword" Nature is the password. Look at the code:

String url = "Jdbc:odbc:Fred";
Connection con = drivermanager.getconnection (URL, "Fernanda", "J8");

How to use other drivers, then the documentation will tell you how to write the URL, carefully look at the corresponding documentation, there will be specific examples to teach you how to do.

Once the Drivermanager.getconnection method has found the driver and source to establish the link, start a connection with the DBMS based on the user name and password provided later, if the link is established.



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.