Introduction to the configuration and functionality of the JDBC data source and connection pool under eclipse

Source: Internet
Author: User
Tags auth connection pooling stmt

Today in the arithmetic web version of the encounter a confusion, because the need to put each of the resulting sub-deposit into

Database, so it is necessary to repeat the load drive, establish a connection and other operations, so that on the one hand write programs inconvenient, increase the amount of program, on the other hand, there is a sharp decline in the performance of the database, then how to solve this problem?

The approach I've learned is to address this problem in the form of JDBC data sources and connection pooling. The connection using DataSource to establish a database does not need to load the JDBC driver, nor does it require the DriverManager class to get the DataSource object by querying a Jndi server. Then call the Getconnection () method of the DataSource object to establish a connection to the database. The following code is an instance of a connection that uses DataSource to establish a database:

Creating connection and statement objects
Connection Conn=null;
Statement stmt=NULL;
Try{Context CTX=NewInitialContext ();
The Javax.naming.Context interface defines the binding of objects and names,
And the method of querying objects by name lookup ().
DataSource DS= (DataSource) ctx.lookup ("java:comp/env/jdbc/calculation"); Conn=ds.getconnection (); stmt=conn.createstatement (); String SQL="INSERT into Biaodashi values ('"+result+"')"; Stmt.execute (SQL); Stmt.close (); Conn.close (); } Catch(Exception e) {System. out. println (e); }

Here are the connection pooling technologies:

Connection pooling Technology pre-establishes multiple database connection objects, and then saves the connection object to the connection pool, when a party client requests to arrive, pulls a connection object from the pool for customer service, and when the request is complete, the client program calls the close () method to put the connection object back into the pool.

So, how do you use this technology under Eclipse, the first thing you have to do is configure the JDBC data source:

1. Open a Web project, create a new XML file called context under the Meta-inf directory in the WebContent directory,

2. Open the Context.xml file, the following is the full contents of the Context.xml (can be copied directly into your context.xml file (* representative of their own according to the actual situation filled)):

<?XML version= "1.0" encoding= "UTF-8"?><ContextPath="/*"reloadable= "true">    <!--Configure the data source -    <ResourceAuth= "Container"Driverclassname= "Com.mysql.jdbc.Driver"maxactive= "+"Maxldle= "+"maxwait= "10000"name= "jdbc/*"Password="****"type= "Javax.sql.DataSource"URL= "Jdbc:mysql://localhost:3306/*?autoreconnect=true"username= "root"/></Context>

A basic explanation of the above configuration files is required:

Context path= "/*", this is a virtual directory, is equal to give you a real data source file an alias, * can be arbitrarily named;

Name= "jdbc/*", which is the Jndi name of the specified resource relative to the java:comp/env context, * can be arbitrarily named, but keep in mind that the following connection to the database is used.

password= "* * * *" is your database password.

Url= "Jdbc:mysql://localhost:3306/*?autoreconnect=true"

Specifies the URL to connect to the database.

(Just change the * number to the name of your database, and the rest can be fixed.) )

  

Next is the configuration of the Web. xml file:

1. Open the Web. xml file and add the following code between <web-app></web-app>:

(You can directly copy and paste into your Web. xml file in the <web-app></web-app> (* stands for oneself according to the actual situation fill))

<Resource-ref>    <Description>MySQL DBCP</Description>    <Res-ref-name>jdbc/*</Res-ref-name>    <Res-type>Javax.sql.DataSource</Res-type>    <Res-auth>Container</Res-auth>  </Resource-ref>

The content between <res-ref-name></res-ref-name> here represents the value of the name attribute in the configuration context.xml file above.

 

At this point, the configuration of the JDBC data source and connection pool under Eclipse is ready, and the topmost instance will work as expected.

Introduction to the configuration and functionality of the JDBC data source and connection pool under eclipse

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.