Idea Community Edition Maven-tomcat plug-in configure Jndi data source

Source: Internet
Author: User
Tags auth
Preface:

Because in the learning process, the tutorial is carried out using eplices, and I am using idea, so the configuration process is recorded
Operating environment: OS X 10, Idea Community Edition, Java8, MySQL 5.1.38, tomcat7-maven-plugin steps:

Create the context.xml in the Web-inf folder of the Web project, as follows

<!--context.xml-->
<?xml version= "1.0" encoding= ' UTF-8 '?>
<context>
    < watchedresource>web-inf/web.xml</watchedresouurce>
    <resource name= "jdbc/msg"
              auto= " Container "
              type=" Javax.sql.DataSource "
              username=" "Root"
              password= "hh13532550193!"
              Driverclassname= "Com.mysql.jdbc.Driver"
              maxactive= "maxidle="
              />
  </context>

Add the path to the context.xml file in the TOMCAT7 dependency configuration in the project's Pom.xml file so that TOMCAT7 can refer to the file correctly:

<!--pom.xml  -->
<build>
 <plugins>
     <plugin>
         <groupId> Org.apache.tomcat.maven</groupid> .....
         ....
         <configuration> ...
             <contextfile>context.xml file path </contextFile>
         </configuration>
         ....
     </plugin>
 </plugins>
</build>

Declare in Web.xml

<!--web.xml-->
<resource-ref>
    <res-ref-name>jdbc/msg</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

To get a database link in Java code:

import javax.naming.Context; import javax.naming.InitialContext; import
Javax.sql.DataSource;
Import java.sql.*;
.....
Context Initctx = new InitialContext ();
Context Envctx = (context) initctx.lookup ("java:comp/env");
DataSource ds = (DataSource) envctx.lookup ("jdbc/msg"); Connection conn = Ds.getconnection (); 

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.