Configure Data source-read Data source connection database

Source: Internet
Author: User
Tags auth

Several important steps to connect a database with a connection pool:
Step One:
1. Copy the database's Mysql-connector-java-5.0.8-bin.jar file to the Lib folder in the Tomcat installation directory.
2, in Tomcat's Conf/context.xml

Note: Chinese is not allowed in the configuration file, including Chinese in comments
<!--tomcat to add database driver files--

<resource name= "Jdbc/news"
Auth= "Container" type= "Javax.sql.DataSource" maxactive= "100"
maxidle= "maxwait=" 10000 "username=" root "password=" root "
Driverclassname= "Com.mysql.jdbc.Driver"
Url= "Jdbc:mysql://localhost:3306/mydb2"/>



which
Name: Represents your connection pool, which is the address you want to access the connection pool
Auth: Is the connection pool management attribute, container represents the container management
Type: Is the kind of object
Driverclassname: Is the database-driven name
URL: Is the address of the database
Username: is the user name of the login database
Password: is the password of the login database
Maxidle, maximum idle number, maximum idle time for database connections. Over idle time, the database is connected
The connection will be marked as unavailable and then released. Set to 0 to indicate no limit.
Maxactive, the maximum number of database connections for the connection pool. Set to 0 to indicate no limit.
Maxwait, the maximum connection wait time is established. If this time is exceeded, the exception will be received. Set to-1 to indicate
Unlimited.

3. Configure the application's Web. xml file (optional)
<resource-ref>
<res-ref-name>jdbc/news</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Step Two:
1. Write Java code to get the data source object associated with the logical name
Key code:
Public Connection GetConnection2 () {
try {
Initializing context
Context cxt=new InitialContext ();
Gets the data source object associated with the logical name
is the environment naming context (environment naming context (ENC)
Context ctx2= (context) Cxt.lookup ("java:comp/env");
DataSource ds= (DataSource) ctx2.lookup ("Jdbc/news");

/* Another way of writing, the two methods are different in nature
* DataSource ds= (DataSource) cxt.lookup ("Java:comp/env/jdbc/news");
*/
Conn=ds.getconnection ();
} catch (Namingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Return conn;
}


Step Three:
1. Writing JSP pages: Verifying data source connections
Key code:
<%
Basedao basedao=new Basedao ();
Connection Conn=basedao.getconnection2 ();

%>
<%=conn%>

Configure Data source-read Data source 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.