"JDBC" uses connection pooling technology to access Oracle

Source: Internet
Author: User
Tags connection pooling

You need to import the relevant jar package before accessing the database using a connection pool.

First, you need to import the Oracle driver package, usually in the directory where the Oracle database is installed, there is a Ojdbc6.jar driver package in the reader's directory.

You then need to import and connect pool-related jar packages, such as: Commons-dbcp-1.4.jar, and Commons-pool-1.5.6.jar packages.

Here is a toolkit that is packaged with a connection pool:

Driverclassname=oracle.jdbc.OracleDriverurl=jdbc:oracle:thin: @localhost: 1521: Xepassword= 517839username=system
db.properties

Note: This db.properties file can not be changed casually, because driverclassname,url,password,username these values can be exactly the same as the property name inside the wrapper class, if the inconsistency can be set using the following method:

Setdriverclassname () SetUrl () Setusername () SetPassword ()
 PackageCom.xdl.util;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;Importjava.util.Properties;ImportJavax.sql.DataSource;Importorg.apache.commons.dbcp.BasicDataSourceFactory; Public classdbcputilproperties {Private  StaticDataSource DataSource; Static{InputStream InputStream= Dbcputilproperties.class. getClassLoader (). getResourceAsStream ("Db.properties"); Properties Pro=NewProperties (); Try{pro.load (InputStream); DataSource=basicdatasourcefactory.            CreateDataSource (PRO); /*** can also set the driver, user name, account number, password, if the property name in the db.properties changed, can be manually set by the following way * DATASOURCE.SETDRIVERCLASSNAME (pr             O.getproperty ("Driverclassname"));             * Datasource.seturl (pro.getproperty ("url"));             * Datasource.setusername (Pro.getproperty ("username"));             * Datasource.setpassword (Pro.getproperty ("password")); */        } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }     Public   StaticConnection getconnection () {Try {            returndatasource.getconnection (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return  NULL; }     Public  Static  voidReleaseresource (Connection conn,statement st,resultset rs) {if(rs! =NULL) {            Try{rs.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{RS=NULL; }        }        if(St! =NULL) {            Try{st.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{St=NULL; }        }        if(Conn! =NULL) {            Try{conn.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{conn=NULL; }        }    }    }
Dbcputilproperties.java

The Dbcputilproperties.java is packaged to load the driver, get the connection, release the resources.

Here is a test class:

 Package com.xdl.test; Import java.sql.Connection; Import com.xdl.util.DBCPUtilProperties;  Public class dbcputilpropertiestest {    publicstaticvoid  main (string[] args) {         // TODO auto-generated Method stub        Connection  conn = dbcputilproperties.getconnection ();        SYSTEM.OUT.PRINTLN (conn);    }}
Dbcputilpropertiestest.java

"JDBC" uses connection pooling technology to access Oracle

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.