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
- <Context>
- ......
- <!--MySql --
- <Resource name="jdbc/orclight" auth="Container "
- type= "Javax.sql.DataSource"maxactive= " maxidle="
- maxwait= "10000"username= "root" password="root "
- driverclassname="Com.mysql.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/orclight"/>
- ......
- </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
- /Initialize Jndi context, create DataSource object
- Context Initcontext = new InitialContext ();
- Context context = (context) initcontext.lookup ("java:comp/env");
- 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
- Create a Basicdatasource object
- Basicdatasource ds = new Basicdatasource ();
- Ds.setdriverclassname ("Com.mysql.jdbc.Driver");
- Ds.seturl ("jdbc:mysql://localhost:3306/orclight");
- Ds.setusername ("root");
- Ds.setpassword ("root");
- Ds.setinitialsize (50);
- Ds.setmaxactive (100);
- Ds.setmaxidle (30);
- Ds.setmaxwait (10000);
- //Turn off data source connections
- 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
- Create a Combopooleddatasource object
- Combopooleddatasource ds = new Combopooleddatasource ();
- Ds.setdriverclass ("Com.mysql.jdbc.Driver");
- Ds.setjdbcurl ("jdbc:mysql://localhost:3306/orclight");
- Ds.setuser ("root");
- Ds.setpassword ("root");
- Ds.setinitialpoolsize (50);
- Ds.setmaxpoolsize (100);
- Ds.setmaxidletime (10000);
Common data Sources in Java