Common data Sources in Java

Source: Internet
Author: User

Data Source: Stores all information that establishes a database connection. Just as you can find files in the file system by specifying a file name, you can find the appropriate database connection by providing the correct data source name.

Create DataSource in 1.JNDI mode

1.1 Configure the relevant connection information for the data source, which can be configured in the Conf/context.xml file in the Tomcat installation directory.
It is configured as follows:

[HTML]View PlainCopy
  1. <Context>
  2. ......
  3. <!--MySql --
  4. <Resource name="jdbc/orclight" auth="Container "
  5. type= "Javax.sql.DataSource"maxactive= " maxidle="
  6. maxwait= "10000"username= "root" password="root "
  7. driverclassname="Com.mysql.jdbc.Driver"
  8. url="jdbc:mysql://localhost:3306/orclight"/>
  9. ......
  10. </Context>

1.2 Create a data source in a Jndi way in the program to get the database connection to the appropriate operation

The code is as follows:

[Java]View PlainCopy
    1. /Initialize Jndi context, create DataSource object
    2. Context Initcontext = new InitialContext ();
    3. Context context = (context) initcontext.lookup ("java:comp/env");
    4. Datasourcedatasource = (DataSource) context.lookup ("jdbc/orclight");

2.Apache provides a dbcp way to create a data source

2.1 To create a data source in this way, you must first prepare two jar files: Commons-dbcp.jar and Commons-pool.jar.

2.2 Data sources created in this way are no longer javax.sql.DataSource. DataSource, but Org.apache.commons.dbcp.BasicDataSource.

The code is as follows:

[Java]View PlainCopy
  1. Create a Basicdatasource object
  2. Basicdatasource ds = new Basicdatasource ();
  3. Ds.setdriverclassname ("Com.mysql.jdbc.Driver");
  4. Ds.seturl ("jdbc:mysql://localhost:3306/orclight");
  5. Ds.setusername ("root");
  6. Ds.setpassword ("root");
  7. Ds.setinitialsize (50);
  8. Ds.setmaxactive (100);
  9. Ds.setmaxidle (30);
  10. Ds.setmaxwait (10000);
  11. //Turn off data source connections
  12. Ds.close ();

3.c3p0 way to create a data source

3.1 Creating a data source using C3P0 method you should first prepare a jar file: C3p0-0.9.1.2.jar, put it in the Web/lib directory,

You can use C3P0 to create a data source in your project

The data source object created by 3.2 3.2 C3p0 is also not a DataSource object, but Combopooleddatasource.

The code is as follows:

[Java]View PlainCopy
    1. Create a Combopooleddatasource object
    2. Combopooleddatasource ds = new Combopooleddatasource ();
    3. Ds.setdriverclass ("Com.mysql.jdbc.Driver");
    4. Ds.setjdbcurl ("jdbc:mysql://localhost:3306/orclight");
    5. Ds.setuser ("root");
    6. Ds.setpassword ("root");
    7. Ds.setinitialpoolsize (50);
    8. Ds.setmaxpoolsize (100);
    9. Ds.setmaxidletime (10000);

Common data Sources in Java

Related Article

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.