JAVA-DBCP Connection Pool

Source: Internet
Author: User

To create a project:

To import a jar package:

See.

Jdbcconn.java get the data source class:

Package Com.gordon.jdbcconn;import Java.io.inputstream;import Java.util.properties;import Javax.sql.DataSource; Import Org.apache.commons.dbcp.basicdatasource;public class Jdbcconn {private static String drivername = Null;private static string URL = Null;private static string USER = Null;private static string PASSWORD = Null;private static Basicdatas Ource DataSource = new Basicdatasource (), static {try {properties props = new Properties (); InputStream in = Jdbcconn.class. getClassLoader (). getResourceAsStream ("Db.properties");p rops.load (in);D rivername = Props.getproperty ("DRIVERNAME" ); url = props.getproperty ("url"); user = Props.getproperty ("user"); PASSWORD = Props.getproperty ("PASSWORD");} catch (Exception e) {throw new RuntimeException (e);}} public static DataSource Getdatasource () throws Exception {/* Driverclassname database driver name URL database address username user name password Password maxactive maximum number of connections Minidle minimum idle connection maxidle maximum idle connection initialsize Initialize connection * /datasource.setdriverclassname (drivername);d Atasource.seturl (URL);d Atasource.setUsername (USER);d Atasource.setpassword (PASSWORD); return datasource;}} 

  

Dbcptest.java Test class:

Package Com.gordon.dbcp;import Java.util.list;import Org.apache.commons.dbutils.queryrunner;import Org.apache.commons.dbutils.handlers.arraylisthandler;import Com.gordon.jdbcconn.jdbcconn;public class DBCPTest { public static void Main (string[] args) {try {selectfunction (); InsertFunction (); Selectfunction ();} catch (Exception e) { System.out.println (E.getmessage ());}} public static void InsertFunction () throws Exception {queryrunner qr = new Queryrunner (Jdbcconn.getdatasource ()); String sql = "INSERT into user (name) VALUES (?)"; O Bject[] params = {"TestName"};int res = qr.update (sql, params), if (res > 0) {System.out.println ("insert success.");} els e {System.out.println ("Insert Faild.");}} public static void Selectfunction () throws Exception {queryrunner qr = new Queryrunner (Jdbcconn.getdatasource ()); String sql = "SELECT * from user"; object[] params = {}; List<object[]> objlist = qr.query (sql, New Arraylisthandler (), params); for (object[] objs:objlist) {for (Object ob J:OBJS) {SysteM.out.print (obj + "");} System.out.println ();}}}

  

JAVA-DBCP Connection Pool

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.