Bromon Original, respect copyright three, database connection pool
Since there is no web environment, we need to implement a database connection pool. Apache has a project called Commons DBCP. This is a database connection pool implemented based on Apache's own object pool (Apache ", we can use it directly Apache's software is not necessarily the best, but it is greatly better than we write.
Commons DBCP requires three .jar:
Commons-Collectes-3.1.jar, Commons-DBCP-1.2.1.jar, Commons-Pool-1.2.jar
These three files can be downloaded under the Apache - Jakarta - Commons project, join the project.
The code constructs a database connection pool is as follows:
Package org.bromon.games.al; import java.sql. *;
Import com.gwnet.games.antilord.util. *;
Import org.apache.commons.dbcp.connectionFactory;
Import org.apache.commons.dbcp.basicdataser;
Import org.apache.commons.dbcp.dataSourceConnectionFactory;
Private static BasicDataSource BDS = New BasicDataSource ();
Private static connectionFactory FAC = NULL;
// Initializing the connection pool
BDS.SetDriverClassName ("org.postgreSQL.Driver); // Database driver
BDS.SETURL ("JDBC: PostgreSQL: // LocalHost: 5432 / MYDB"); // Database URL
BDS.SETUSERNAME ("postgres"); // DBA account
BDS.SETPASSWORD ("xxxxxxxx"); // password
Bds.setInitialsize (100); // Initialization connection
BDS.SETMAXIDLE (10); // Maximum number
BDS.SETMAXWAIT (1000 * 60); // Timeout Recycling Time
FAC = New DataSourceConnectionFactory (BDS); // Get the connection factory
Connection conn = fac.createConnection (); // Get a connection from the pool
Conn.close (); // Release connection, back to the pool
// Destroy the connection pool
BDS.Close ();
BDS = NULL;
FAC = NULL;
Please handle all kinds of exceptions in your own operation. Next: How to randomly generate users' cards