Obtain the database connection through Driver and the driver database connection

Source: Internet
Author: User
Tags driver database

Obtain the database connection through Driver and the driver database connection

Take a look at the file. There is a properties configuration file under the current package, and there is a lib folder under the root directory, which contains the mySql driver jar package

Driver: an interface. The database vendor must provide the implemented interface to obtain the database connection. You can use the implementation Class Object of the Driver to obtain the database connection.
* 1. Add the mySql driver
* 1.1 decompress mysql-connector-java-5.1.39.zip 1.2 create a lib directory in the current project 1.3
* Copy the mysql-connector-java-5.1.39-bin.jar to the lib directory, right-click 1.4, buildPath, add
* Add buildPath to the class path

The first method is to directly obtain the database connection without the configuration file.

1 @ Test 2 public void testDriver () throws Exception {3 // 1. create a Driver implementation Class Object 4 Driver driver = new com. mysql. jdbc. driver (); 5 // 2. prepare the basic information for connecting to the database, url, user, password 6 String url = "jdbc: mysql: // localhost: 3306/test"; 7 8 Properties info = new Properties (); 9 info. put ("user", "root"); 10 info. put ("password", "123456"); 11 12 // 3. call connect (url, info) of the Driver interface to obtain the database Connection 13 connection Connection = driver. connect (url, info); 14 15 System. out. println (connection); // com. mysql. jdbc. JDBC4Connection @ 73f71216}

 

To make the program decoupled and more generic, use the following solution:

Compile a common method. without modifying the source program, you can obtain the connection solution of any database.
The full class name, url, user, and password of the implementation class are put into a configuration file, which is decoupled from the specific database by modifying the configuration file.

Code:

Public Connection getConnection () throws Exception {String driverClass = null; String jdbcUrl = null; String user = null; String password = null; // read jdbc under the class path. properties file 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"); // creates a Driver object through reflection. driver driver = (Driver) Class. forName (driverClass ). newInstance (); Properties info = new Properties (); info. put ("user", user); info. put ("password", password); // obtain the database connection through the connect Method of the Driver. connection connection = driver. connect (jdbcUrl, info); return connection;} @ Test public void testGetConnection () throws Exception {System. out. println (getConnection (); // com. mysql. jdbc. JDBC4Connection @ 1556938}

The properties file is as follows:

#driver=oracle.jdbc.driver.OracleDriver#jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl#user=scott#password=javadriver=com.mysql.jdbc.DriverjdbcUrl=jdbc:mysql://localhost:3306/testuser=rootpassword=123456

In this way, if you want to connect to other databases, you only need to modify this file without modifying the code.

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.