C3P0 Use Example!

Source: Internet
Author: User
Tags connection pooling stmt

       We often consider using database connection pools when using database connections, because the mechanism of database connection pooling makes it more efficient to interact with large numbers of users.

public class Dbpool {private static dbpool Dbpool; private Combopooleddatasource dataSource; static {dbpool = new Dbpool (); Public Dbpool () {try {dataSource = new Combopooleddatasource (); Datasource.setuser ("root"); Datasource.setpassword ("1 23456 "); Datasource.setjdbcurl ("Jdbc:mysql://127.0.0.1:3306/game?user=root&password=123456&useunicode=true"); Datasource.setdriverclass ("Com.mysql.jdbc.Driver"); Sets the initial connection pool size. Datasource.setinitialpoolsize (2); Sets the minimum value of the connection pool. Datasource.setminpoolsize (1); Sets the maximum connection pool value. Datasource.setmaxpoolsize (10); Sets the maximum number of statements in the connection pool. Datasource.setmaxstatements (50); Sets the maximum idle time for a connection pool. Datasource.setmaxidletime (60); catch (Propertyvetoexception e) {throw new RuntimeException (e);}} Public final static Dbpool getinstance () {return dbpool.} public Final Connection getconnection () {try {return Datasou Rce.getconnection (); catch (SQLException e) {throw new RuntimeException ("Cannot get connection from data source", e);} public static void Main (string[] args) throws Sqlexception {Connection con = null; try {con = dbpool.getinstance (). getconnection (); ResultSet rs = con.createstatement (). ExecuteQuery ("SELECT * from Lesogo_user"); while (Rs.next ()) {System.out.println (Rs.getobject (1)); System.out.println (Rs.getobject (2)); System.out.println (Rs.getobject (3)); } catch (Exception e) {} finally {if (con!= null) con.close ();}} }

 

/** * This example shows how to obtain a C3P0 data source and bind it to a Jndi name service. /Public final class Jndibinddatasource {/** * Be sure to load the database driver class, either through Class.forName () [below] or externally (for example, by using- Djdbc.drivers when you start your JVM). */Static {try {Class.forName ("Com.mysql.jdbc.Driver");} catch (Exception e) {e.printstacktrace ();}} public static V OID Main (string[] argv) {try {/** * lets a command-line argument specify the name of the binding of our data source. */String Jndiname = argv[0]; /** * Get data source using preset pool params ... * This is the only c3p0 specific code here * * DataSource unpooled = datasources. Unpooleddatasource ("jdbc:mysql://127 .0.0.1:3306/game?user=root&password=123456&useunicode=true "," root "," 123456 "); DataSource pooled = Datasources.pooleddatasource (unpooled); SYSTEM.OUT.PRINTLN ("Whether can obtain connection:" +pooled.getconnection ()); /** * Creates the initialcontext, and binds the data source to it in the usual way. * We are using the InitialContext version of the constructor parameter, so the Jndi.properties file, * system attributes, or by other means, the JNDI environment, must be set first. * * InitialContext CTX = new InitialContext (); System.out.println ("Jndiname:" + jndiname); Ctx.rebind (Jndiname, pooled); System.out.println ("DataSource bound to Nameservice under the name/"" + Jndiname + '/"'); catch (Exception e) {e.printstacktrace ();}} static void Attemptclose (ResultSet o) {try {if (o!= null) o.close (), catch (Exception e) {e.printstacktrace ();} s tatic void Attemptclose (Statement o) {try {if (o!= null) o.close (); \ catch (Exception e) {e.printstacktrace ();}} St atic void Attemptclose (Connection o) {try {if (o!= null) o.close (); \ catch (Exception e) {e.printstacktrace ();}} PR Ivate Jndibinddatasource () {}}

 

/** * This sample demonstrates how to programmatically use the unpooled data source directly/public final class Usejndidatasource {public static void main (string[] argv) {try {/** * Let a command-line argument specify the name, and we'll look for the data source. */String Jndiname = argv[0]; /** * Create InitialContext, and find the datasource of the usual practices. We are using the InitialContext version of the constructor parameter, so the Jndi.properties file, * system attributes, or by other means, the JNDI environment, must be set first. * * InitialContext CTX = new InitialContext (); /** * Get Data Source ... This is the only c3p0 specific code here/* DataSource ds = (DataSource) ctx.lookup (jndiname); /** * Mastered such a connection thing, the usual way * * Connection con = null; Statement stmt = null; ResultSet rs = null; try {con = ds.getconnection (); stmt = Con.createstatement (); rs = Stmt.executequery ("SELECT * from Lesogo_user"); Rs.next ()) System.out.println (rs.getstring (1)); Finally {/** * I'm trying to approximate the resource management, ' neuroticism explicitly shuts down each resource, but if you only close parental resource habits (such as connecting) and let them approach their children, all c3p0 data sources will certainly be handled properly. * * Attemptclose (RS); Attemptclose (stmt); Attemptclose (con); The catch (Exception e) {e.printstacktrace ();}} static void Attemptclose (ResultSet o) {try {if (o!= null) o.close (), catch (exception e) {e.printstacktrace ();}} static void Attemptclose (Statement o) {try {if (o!= null) o.close (), catch (Exception e) {e.printstacktrace ();} s tatic void Attemptclose (Connection o) {try {if (o!= null) o.close ();} catch (Exception e) {e.printstacktrace ();}} P Rivate Usejndidatasource () {}}

 

/** * This example demonstrates how to programmatically use the data source in a database connection pool/Public final class Usepoolbackeddatasource {public static void main (string[] argv) {try {////acquisition before your data source. Get Data Source ... This is the only c3p0 specific code here DataSource unpooled = Datasources.unpooleddatasource ("Jdbc:mysql://127.0.0.1:3306/lesogo_game2?"). User=root&password=123456&useunicode=true "," root "," 123456 "); DataSource pooled = Datasources.pooleddatasource (unpooled); Connection con = null; Statement stmt = null; ResultSet rs = null; try {con = pooled.getconnection (); stmt = Con.createstatement (); rs = Stmt.executequery ("SELECT * from Lesogo_user"); whi Le (Rs.next ()) System.out.println (rs.getstring (1)); Finally {Attemptclose (RS); Attemptclose (stmt); Attemptclose (Con)}} catch (Exception e) {e.printstacktrace ();}} static void Attemptclose (ResultSet o) {try {if (o!= null) o.close (), catch (Exception e) {e.printstacktrace ();} s tatic void Attemptclose (Statement o) {try {if (o!= null) o.close ();} catch (Exception e) {e.printstacktrace (); } static void Attemptclose (Connection o) {try {if (o!= null) o.close (), catch (Exception e) {e.printstacktrace (); } private Usepoolbackeddatasource () {}}

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.