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 ());
}
}
}