Procedure for connecting to a database using JDBC

Source: Internet
Author: User
Welcome to the Java Community Forum, and interact with 2 million technical staff to access the JAVA database. There are many ways to connect to the database. There are four types of drivers based on different database drivers: 1: 1. This is the way JDBC-ODBC bridges are built. However, this method is not suitable for program reuse and maintenance and is not recommended. The ODBC driver of the database is required. 2: Class 2 driver.

Welcome to the Java Community Forum, and interact with 2 million technical staff> there are many ways to connect to the database in JAVA. There are four types of database drivers. This is the way JDBC-ODBC bridges are built. However, this method is not suitable for program reuse and maintenance and is not recommended. The ODBC driver of the database is required. 2: Class 2 driver.

Welcome to the Java Community Forum and interact with 2 million technical staff> enter

JAVA can connect to a database in a variety of ways. Based on the needs of different database drivers, there are four types:

1:1 class driver. This is the way JDBC-ODBC bridges are built. However, this method is not suitable for program reuse and maintenance and is not recommended. The ODBC driver of the database is required.

2: Class 2 driver. This is the form of JDBC + vendor API. Vendor APIs are generally written in C. Therefore, this method is not long-lived.

Class 3 drivers. This is the form of JDBC + vendor Database Connection Server + DataBase. In this way, a server dedicated to DATABASE connection is provided between JAVA and DATABASE (generally provided by DATABASE vendors ). The advantage is that the connection can be optimized.

Class 4 driver. This is the connection method of pure JDBC + DATABASE. It is also recommended connection method. This separates applications from databases. Developers only need to focus on the implementation of internal logic, rather than the specific implementation of database connections. There are two Connection Methods: Hard encoding, which is the required parameter for hard-coded database connection in the program; JNDI DataSource, it is to set a datasource data source in the external environment of the program running, also known as (Context), with a jndi name. The program only needs to find this name to get a database connection object.

Connect to the database through JDBC

-Create a program to connect to the database using JDBC, which contains seven steps:

1. Load the JDBC driver:

Before connecting to the database, load the driver of the database to be connected to JVM (java Virtual Machine), which is achieved through the static method forName (String className) of java. lang. Class. For example:

Try {

// Load the MySql Driver Class

Class. forName ("com. mysql. jdbc. Driver ");

// Load the oracle driver

Class. forName ("oracle. jdbc. driver. OracleDriver ");

// Load the sqlserver driver

Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver ");

} Catch (ClassNotFoundException e ){

System. out. println ("The driver class cannot be found. Failed to load the driver! ");

E. printStackTrace ();

}

After the Driver class is loaded, the Driver class instance is registered to the DriverManager class.

2. Provide the URL of the JDBC connection

-The connection URL defines the protocol, sub-protocol, and data source identification used to connect to the database.

-Writing format: Protocol: Sub-Protocol: data source ID

Protocol: always starts with JDBC in jdbc.

Sub-Protocol: the name of the Bridge Connection driver or database management system.

Data source ID: Mark the address and connection port of the database source.

Example: (MySql connection URL)

Jdbc: mysql: // localhost: 3306/test-useUnicode = true & characterEncoding = gbk;

UseUnicode = true: indicates that the Unicode character set is used. If characterEncoding is set

This parameter must be set to true. characterEncoding = GBK: character encoding method.

[1] [2]

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.