There are 4 jar packages that need to be referenced, namely Commons-pool2-2.4.2.jar, Commons-dbcp2-2.1.1.jar, Mysql-connector-java-5.1.42-bin.jar, Commons-logging-1.2.jar
Missing one will error (Exception in thread "main" java.lang.noclassdeffounderror:org/apache/commons/pool2/ Pooledobjectfactory) means I can't find this bag.
I need to go to these bags on the website.
Connection pooling is managed through the Basicdatasource object to the connection pool, and we need to set the key information about the database to the connection pool, like JDBC, and the rest of the operation is the same as JDBC.
1 Importjava.sql.Connection;2 ImportJava.sql.ResultSet;3 Importjava.sql.SQLException;4 Importjava.sql.Statement;5 6 ImportOrg.apache.commons.dbcp2.BasicDataSource;7 8 /**9 * @authorGod Yu JianzhiTen * @date creation time: May 19, 2017 pm 7:00:04 One */ A Public classDbpooltest { - - Public StaticBasicdatasource ds =NULL; the - Public Final StaticString driver_name = "Com.mysql.jdbc.Driver"; - Public Final StaticString user_name = "root"; - Public Final StaticString PASSWORD = "123456"; + Public Final StaticString Db_url = "Jdbc:mysql://localhost/shen_db?useunicode=true&characterencoding=utf-8&usessl=false"; - + Public Static voidDbpoolinit () { ADS =NewBasicdatasource (); at Ds.seturl (db_url); - Ds.setdriverclassname (driver_name); - Ds.setusername (user_name); - Ds.setpassword (PASSWORD); - } - in Public Static voiddbpooltest () { -Connection conn =NULL; toStatement stmt =NULL; +ResultSet rs =NULL; - Try { theconn =ds.getconnection (); *stmt =conn.createstatement (); $rs = Stmt.executequery ("SELECT * from Students");Panax Notoginseng while(Rs.next ()) { -System.out.println (rs.getstring ("name")); the } +}Catch(SQLException e) { A e.printstacktrace (); the}finally { + Try { - if(Conn! =NULL) $ conn.close (); $ if(stmt! =NULL) - stmt.close (); - if(rs! =NULL) the rs.close (); -}Catch(SQLException E1) {Wuyi //Ignore the } - Wu } - } About $ Public Static voidMain (string[] args) { - dbpooltest.dbpoolinit (); - dbpooltest.dbpooltest (); - } A +}
Managing connections using DBCP connection pooling