Managing connections using DBCP connection pooling

Source: Internet
Author: User
Tags connection pooling stmt

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

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.