Welcome to the CSDN-markdown Editor, csdn-markdown
Use dbcp in java ee
When using java for website development, access to database data is essential, while dbcp has a unique advantage in managing database connections.
Dbcp
DBCP (DataBase connection pool), DataBase connection pool. It is a java connection pool project on apache and a connection pool component used by tomcat. Using dbcp alone requires 2 Packages: commons-dbcp.jar, commons-pool.jar Since establishing a database connection is a very time-consuming behavior, so through the connection pool in advance with the database to establish some connections, put in memory, when the application needs to establish a database connection, apply for one in the connection pool, and then store it back. -- [Baidu encyclopedia]
Simply put, a database connection pool compiled by apache can be used directly. It is faster and more efficient than self-written data access programs.
Usage
Code block
If the preparation is complete, the following code is started:
DBManager class
Import java. SQL. connection; import java. SQL. databaseMetaData; import java. SQL. SQLException; import java. util. properties; import javax. SQL. dataSource; import org. apache. commons. dbcp. basicDataSourceFactory; import org. apache. commons. logging. log; import org. apache. commons. logging. logFactory; public class DBManager {private static final Log log = LogFactory. getLog (DBManager. class); private static final String ConfigFile = "dbcp. properties "; private static DataSource dataSource; static {Properties dbProperties = new Properties (); try {dbProperties. load (DBManager. class. getClassLoader (). getResourceAsStream (configFile); dataSource = basicperformancefactory. createDataSource (dbProperties); Connection conn = getConn (); DatabaseMetaData mdm = conn. getMetaData (); log.info ("Connected to" + mdm. getDatabaseProdu CtName () + "" + mdm. getDatabaseProductVersion (); if (conn! = Null) {conn. close () ;}} catch (Exception e) {log. error ("failed to initialize the connection pool:" + e); e. printStackTrace () ;}} private DBManager () {}public static final Connection getConn () {Connection conn = null; try {conn = dataSource. getConnection ();} catch (SQLException e) {log. error ("failed to get database Connection:" + e) ;}return conn ;}public static void closeConn (Connection conn) {try {if (conn! = Null &&! Conn. isClosed () {conn. setAutoCommit (true); conn. close () ;}} catch (SQLException e) {log. error ("failed to close database connection:" + e );}}}
ConfigFile = "dbcp. properties" refers to a configuration file. The specific content is as follows:
Dbcp. properties
DriverClassName = com. mysql. jdbc. driverurl = jdbc: mysql: // localhost: 3306/Database Name username = root password = 1234556 maxActive = 30 maxIdle = 10 maxWait = 1000 removeAbandoned = trueremoveAbandonedTimeout = 180
Please search for the meaning of each parameter.
Note: The dbcp. properties file is stored in the src file.
Test class: atatdbtest class
Package DBbase; import java. SQL. connection; import java. SQL. SQLException; public class TomcatDbTest {public static void main (String [] args) {Connection conn = DBManager. getConn (); if (conn! = Null) {System. out. println ("established successfully"); try {conn. close ();} catch (SQLException e) {System. out. println ("failed to close the connection"); e. printStackTrace ();} return;} System. out. println ("failed to connect to the database! ");}}
Conclusion
The above instructions and codes refer to the following websites to pay tribute to the providers.
[Reference website for code and instructions]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.