JDBC gets Oracle database connection (using Driver)

Source: Internet
Author: User
Tags driver manager

How to get a database connection:

1. Driver Interface:

? The Java.sql.Driver interface is the interface that all JDBC drivers need to implement. This interface is provided to the database vendors, different database vendors to provide different implementations

? The program does not need to go directly to the implementation of the Driver interface class, but by the driver manager class (Java.sql.DriverManager) to call these Driver Implement

2. Load and register the JDBC driver:

? Loading JDBC drivers need to be called Class static methods for classes forname () , passing it to the loaded JDBC class name of the driver

? DriverManager class is a driver manager class that is responsible for managing drivers

? typically not explicitly called DriverManager Class of Registerdriver () method to register an instance of the driver class because Driver The driver class for the interface contains a static block of code that, in this static code block, invokes the Drivermanager.registerdriver () method to register an instance of itself

3. Establish the connection:

? can call DriverManager Class of getconnection () method to establish a connection to the database

? JDBC URL used to identify a driver that is registered with the driver manager through this URL Select the correct driver to establish a connection to the database.

? JDBC URL The standard consists of three parts, separated by a colon between the parts.

–jdbc:< Sub-protocol >:< Sub-name >

– protocol: the protocol in the jdbc URL is always jdbc

– Sub-protocol: a sub-protocol used to identify a database driver

– child name: A way to identify a database. Sub-names can vary depending on the sub-Protocol, and the purpose of the sub-name is to locate the database to provide sufficient information

Public Connection getconnection () throws exception{
String driverclass = null;
String jdbcurl = null;
String user = null;
String password = null;

InputStream in =
GetClass (). getClassLoader (). getResourceAsStream ("jdbc.properties");
Properties Properties = new properties ();
Properties.load (in);
Driverclass = Properties.getproperty ("Driver");
Jdbcurl = Properties.getproperty ("Jdbcurl");
user = Properties.getproperty ("user");
Password = properties.getproperty ("password");

Creating a Driver object from reflection
Driver Driver =
(Driver) Class.forName (Driverclass). newinstance ();

Properties Info = new properties ();
Info.put ("user", user);
Info.put ("password", password);

Get a database connection through the Driver Connect method
Connection Connection = Driver.connect (Jdbcurl, info);

return connection;
}

@Test
public void Testgetconnection () throws exception{
System.out.println (getconnection ());
}

Jdbc.properties file

Driver=oracle.jdbc.driver.oracledriver
Jdbcurl=jdbc:oracle:thin:@localhost : 1521:ORCL
User=scott
Password=tiger

JDBC gets Oracle database connection (using Driver)

Related Article

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.