Dbcp database connection pool and dbcp database connection

Source: Internet
Author: User

Dbcp database connection pool and dbcp database connection
Dbcp is an apache product.

Package address: http://commons.apache.org/proper/commons-pool/download_pool.cgi

Download appropriate packages based on your jdk version

Package to be imported: commons-dbcp-1.4.jar commons-pool-1.6.jar

Dbcp has two methods to obtain the Connection: 1. BasicDataSource 2. BasicDataSourceFactory

Let's take a look at BasicDataSource.

Configuration File Properties required

The content of the db. properties file is as follows:

Url = jdbc: mysql: // localhost: 3306/test? UseUnicode = true & characterEncoding = UTF-8
User = root
Password = 123123
Forname = com. mysql. jdbc. Driver

  

Public class Dbcp {

Private static String url = null;
Private static String user = null;
Private static String password = null;
Private static String driverClassName = null;

Public static BasicDataSource ds = null;
Static {
Properties prop = new Properties ();
InputStream inputStream = Dbcp. class. getResourceAsStream ("/propp. properties ");
Try {

        
Prop. load (inputStream );

// Obtain the url from the configuration file
Url = prop. getProperty ("url ");

// Obtain the connection name user from the configuration file
User = prop. getProperty ("user ");

Password = prop. getProperty ("password ");

// Connection driver
DriverClassName = prop. getProperty ("forname ");
Try {
Class. forName (driverClassName). newInstance ();
} Catch (InstantiationException e ){
E. printStackTrace ();
} Catch (IllegalAccessException e ){
E. printStackTrace ();
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}

// Create an object
Ds = new BasicDataSource ();

// Set the value obtained from the configuration file to BasicDataSource
Ds. setUrl (url );
Ds. setUsername (user );
Ds. setPassword (password );
Ds. setDriverClassName (driverClassName );

// Set the initialization size
Ds. setInitialSize (5 );

// Set the maximum number of connections
Ds. setMaxActive (10 );

// Set the maximum wait time to be exceeded
Ds. setMaxWait (3000 );
} Catch (IOException e ){
E. printStackTrace ();
}
}

// Obtain a Connection
Public static Connection getCon (){
Try {
Return ds. getConnection ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Return null;
}

// Simple print Test
Public static void main (String [] args ){
For (int I = 0; I <15; I ++ ){
System. out. println (getCon ());
}
}
}

 

Compared with BasicDataSource, BasicDataSourceFactory is simpler.

Configuration File unchanged

Public class Dbcp2 {

Public static BasicDataSource ds = null;
Static {
Properties prop = new Properties ();
InputStream inputStream = Dbcp2.class. getResourceAsStream ("/db. properties ");
Try {
Prop. load (inputStream );

// Directly pass the configuration file object

// Note that the field naming method of the configuration file must correspond to the method provided by BasicDataSource.

// For example, ds. setUrl (url); the field should be a url

Ds = (BasicDataSource) BasicDataSourceFactory. createDataSource (prop );
} Catch (IOException e ){
E. printStackTrace ();
} Catch (Exception e ){
E. printStackTrace ();
}
}

Public static Connection getCon (){
Try {
Return ds. getConnection ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Return null;
}

Public static void main (String [] args ){
For (int I = 0; I <8; I ++ ){
System. out. println (getCon ());
}
}
}

 

 

  

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.