Establish a connection

Source: Internet
Author: User

Establish a connection with the database
To establish a link with the DBMS, you must do the following: (1) load the driver (2) create a link
Load driver
Loading the driver is very simple and requires only one sentence of code. For example you, you want to drive with JDBC-ODBC bridge, then you write:
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
Your driver document will tell you the class name. If your class name is jdbc. DriverXYZ, replace the Code with the following:
Class. forName ("jdbc. DriverXYZ ");
You do not need to create a driver instance and register it with DriverManager, because it is automatically loaded when you call class. forName. If you create an instance of your own, it will not be used or redundant, but it will not cause any harm to the program.
After you load the driver, you can establish a connection with the DBMS.
Create Link
The second step is to establish a connection with the DBMS. See the following code:
Connection con = DriverManager. getConection (Url, "myLogin", "myPassword ");
The second step is equally simple, and there is a little bit of difficulty about 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. Therefore, if you use ODBC to access the ODBC data source named Fred, your jdbc url should be written as jdbc: odbc: Fred. In "myLogin", enter the user name for logging on to the DBMS. "myPassword" is a password. Look at the Code:
String url = "jdbc: odbc: Fred ";
Connection con = DriverManager. getConnection (url, "Fernanda", "J8 ");
If you want to use other drivers, the instructions will show you how to write a URL, carefully view the instructions, and some specific examples will show you how to do it.
Once the DriverManager. getConnection method finds the driver and source of the connection, it starts to establish a connection with the DBMS based on the user name and password provided later.


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.