jdbc/Connection Pool Connection database

Source: Internet
Author: User
Tags connection pooling

Import Java.io.fileinputstream;import Java.sql.connection;import Java.sql.drivermanager;import Java.util.properties;import org.apache.commons.dbcp.basicdatasource;/** * This class is responsible for managing the connection to the database * when the business logic needs to use a database connection, only through the current class * static method to get the connection. * The advantage of this is that the work of connecting data is maintained by the current class, so that when the database connection changes in the future, only this class change can be *. * @author Administrator * */public class Dbutil {//DBCP connection pool private static Basicdatasource Ds;static{try {//read config file//java.ut Il. The properties are used to read the configuration file properties prop = new properties ();//Read and parse the profile contents Prop.load ("New FileInputStream") through the file stream (" Config.properties "));/* * string GetProperty (string key) * Gets the corresponding value (the content to the right of" = ") based on the key of each item in the configuration file (" = "to the left) */string drivername = Prop.getproperty ("drivername"); String url = prop.getproperty ("url"); String username = prop.getproperty ("username"); String Password = prop.getproperty ("password");//maximum number of connections int maxactive = Integer.parseint (Prop.getproperty ("maxactive") );//maximum wait time int maxwait = Integer.parseint (Prop.getproperty ("maxwait"));d s = new Basicdatasource (); Ds.setdriverclassname (drivername);d S.SetUrl (URL);d s.setusername (username);d S.setpassword (password);//maximum number of connections in connection pool ds.setmaxactive (maxactive);//Maximum wait time getconnection effective ds.setmaxwait (maxwait);} catch (Exception e) {e.printstacktrace ();}} /** * Get a database connection * @return * @throws Exception */public static Connection getconnection () throws Exception{try {/* * Connection Pool provides a Method: * Connection getconnection () * This method will return an idle connection in the current connection pool * Because the connection pool can set the time-out when it is created, the effect of this time is reflected here, when there is no idle connection in the connection pool *, the method will go into a blocking state, etc. Pending connection, the method throws a time-out exception when the timeout period of the setting elapses after the connection pool still has no idle connection * available. */return ds.getconnection ();} catch (Exception e) {System.out.println ("Connection Database Exception"); throw e;}} /** * Dbutil class Add code to close database connection * */public static void Close (Connection conn) {if (conn!=null) {try {conn.close ()} catch (Except Ion e) {e.printstacktrace ();}}} /** * Rollback database transaction */public static void rollback (Connection conn) {if (conn! = null) {try {conn.rollback ();} catch (Exception e) { E.printstacktrace ();}}} }

  

Need to import jar packages for connection pooling

Config.properties text:

#
Drivername=com.mysql.jdbc.driver
Url=jdbc:mysql://localhost:3306/mytest1
Username=root
password=123456
#
Maxactive=5
maxwait=5000

To insert data into the database:

Import Java.sql.connection;import Java.sql.statement;public class Mysqlinsert {public static void main (string[] args) { Connection conn = Null;try{conn = Dbutil.getconnection (); Statement st = Conn.createstatement (); String sql= "INSERT into Yasuo" +     "(name,salary,age)" + "values" +     "(' MHz ', 10000,21)"; SYSTEM.OUT.PRINTLN (SQL); st.executeupdate (SQL);} catch (Exception e) {e.printstacktrace ();} Finally{dbutil.close (conn);}}}

  

jdbc/Connection Pool Connection database

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.