MyEclipse Web project JSP using a JNDI database connection pool to connect to the MySQL database

Source: Internet
Author: User
Tags auth

On the internet to check a lot, and finally realized. Let's write the procedure below:

First, create a file context.xml under Webroot/meta-inf, with the following:

<?XML version= "1.0" encoding= "UTF-8"?> <Context>    <Resourcename= "Jdbc/connectionpool"Auth= "Application"type= "Javax.sql.DataSource"username= "root"Password= "root"Driverclassname= "Com.mysql.jdbc.Driver"URL= "Jdbc:mysql://localhost:3306/sow"maxactive= "8"Maxidle= "4"/> </Context>

Where the Name property is the names of the database connection pool, the Auth property is the scope of the database connection pool, application for this web project, and the following sow for the URL attribute is the database name;

Next, add the configuration in Web. xml:

 <Resource-ref>         <Description>News DataSource</Description>         <Res-ref-name>Jdbc/connectionpool</Res-ref-name>         <Res-type>Javax.sql.DataSource</Res-type>         <Res-auth>Application</Res-auth></Resource-ref>

Note that the attributes inside are consistent.

Next, the new myjsp.jsp is used as a test

<%@ page language= "Java"Import= "java.util.*" pageencoding= "UTF-8"%><%@ pageImport= "java.sql.*"%><%@ pageImport= "javax.naming.*"%><%@ pageImport= "Javax.sql.DataSource"%><%String Path=Request.getcontextpath (); String BasePath= Request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >Context Initctx=NewInitialContext (); Context CTX= (Context) initctx.lookup ("Java:comp/env")); //Get Connection Pool objectDataSource ds = (DataSource) ctx.lookup ("Jdbc/connectionpool"); //Create a connectionConnection conn =ds.getconnection (); System.out.println ("Conn=" +conn); Statement stmt=conn.createstatement (); String MySQL= "SELECT * FROM NOTIFICATION_TB"; ResultSet myrs=stmt.executequery (MySQL);//Execute Query%> <body>This is my JSP page.<br> <table> <% while(Myrs.next ()) {%> <%String Notitheme=myrs.getstring ("Notitheme"); String noticontent=myrs.getstring ("Noticontent"); %> <tr><td><%=notitheme%></td><td><%=noticontent%></td></tr > <%System.out.println (Notitheme);              System.out.println (noticontent); %> <%        }     %> </table> </body>

Then reboot Tomcat and publish your website to access http://localhost:8080/PW_Application/MyJsp.jsp to see the results.

MyEclipse Web project JSP using a JNDI database connection pool to connect to the MySQL database

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.