Java connection to databases

Source: Internet
Author: User

In daily work, some people often ask questions about how to connect to the database. Now I want to write it down and help some people.

1. Load a JDBC driver for the corresponding database

Before establishing a connection to a database, you must first load the JDBC driver of the database. After loading, the driver will be automatically registered to the JDBC driver list. There are two methods to load a JDBC driver.

A) Specify the drive in the command line mode or use a colon to split the drive list:

The command is as follows:

C:/> JAVA-djdbc. Drivers = com. company1.driver: COM. company2.driver youproject

B) Method 2: Call the class. forname () method in the program. It is recommended to use ....

Try

{

String drivername = "com. Imaginary. SQL. msql. msqldriver ";

Class. forname (drivername). newinstance ();

}

Catch (classnotfoundexception E1)

{

// Catch cocould not find database driver exception.

}

2. Connect to the database.
  
There is a small difference between the databases to be connected in the background.

A) connect to the Oracle database.

Connection connection = NULL;

Try

{

// Load the JDBC driver;

String drivername = "oracle. JDBC. Driver. oracledriver ";

Class. forname (drivername). newinstance ();

// Create a connection to the database;

String servername = "127.0.0.1 ";

String SERVERPORT = "1521 ";

String serverid = "datebase1"

String username = "hello ";

String userpsw = "world ";

String url = "JDBC: Oracle. Thin: @" + servername + ":" + SERVERPORT + ":" + serverid;

Connection = drivermanager. getconnection (URL, username, userpsw );

}

Catch (classnotfoundexception E1)

{

// Catch cocould not find database driver exception.

}

Catch (sqlexception E2)

{

// Catch cocould not connect to the database exception.

}

B) connect to an SQL Server database.

Connection connection = NULL;

Try

{

// Load the JDBC driver;

String drivername = "com. Microsoft. JDBC. sqlserver. sqlserverdriver ";

Class. forname (drivername). newinstance ();

// Create a connection to the database;

String servername = "127.0.0.1 ";

String SERVERPORT = "1433 ";

String serverid = servername + SERVERPORT;

String username = "hello ";

String userpsw = "world ";

String url = "JDBC: jsqlconnect: //" + serverid;

Connection = drivermanager. getconnection (URL, username, userpsw );

}

Catch (classnotfoundexception E1)

{

// Catch cocould not find database driver exception.

}

Catch (sqlexception E2)

{

// Catch cocould not connect to the database exception.

}

C) connect to a MySQL database ....

Connection connection = NULL;

Try

{

// Load the JDBC driver;

String drivername = "org. gjt. Mm. MySQL. Driver ";

Class. forname (drivername). newinstance ();

// Create a connection to the database;

String servername = "127.0.0.1 ";

String serverid = "Database ";

String username = "hello ";

String userpsw = "world ";

String url = "JDBC: mysql: //" + servername + "/" + serverid;

Connection = drivermanager. getconnection (URL, username, userpsw );

}

Catch (classnotfoundexception E1)

{

// Catch cocould not find database driver exception.

}

Catch (sqlexception E2)

{

// Catch cocould not connect to the database exception.

}

The above three database connection methods are similar. Because different databases are accessed and the database drivers used are different, the Code may be slightly different on the surface, but on the surface, internal

1. Load a specific database JDBC driver.

2. connect to a database.

3. Then, you can perform specific operations on a specific database.

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.