Example of configuring the JNDI link to the Oracle Data Source in Tomcat in Eclipse

Source: Internet
Author: User

Example of configuring the JNDI link to the Oracle Data Source in Tomcat in Eclipse

Recently, I switched to a new company and connected to the database using the JNDI method for the first time.

At the beginning, I couldn't find out where the database address was configured. Later I went through the code and found that the jndiName parameter was passed in during dataSource initialization in spring through the JndiObjectFactoryBean class, baidu JndiObjectFactoryBean knows that the dataSource is obtained in this way through JNDI.

Because the project is distributed (Jboss is used in a unified manner), it is too troublesome to deploy and configure the Jboss ports separately, so you want to use tomcat instead. Jboss configuration of JNDI is relatively simple. A new tomcat service in eclipse will automatically create a Servers project, which contains context. xml, server. for configuration files such as xml, you must be careful not to configure the configuration file in the directory where tomcat is located, but to configure the configuration file here.

The configuration method is as follows:

1. Add the following configuration information to the context. xml file under the Servers project:
<Context>

<Resource name = "jdbc/test" auth = "Container"
Type = "javax. SQL. DataSource"
Username = "sean"
Password = "sean"
DriverClassName = "Oracle. jdbc. driver. OracleDriver"
Url = "jdbc: oracle: thin: @ localhost: 1521: orcl"
MaxActive = "100"
MaxIdle = "30"
MaxWait = "10000" type = "codeph" text = "/codeph"/>

</Context>

2. web. xml configuration under the Servers Project

<Resource-ref>
<Description> OracleDataSource </description>

<Res-ref-name> jdbc/test </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Res-auth> Container </res-auth>
</Resource-ref>

3. Obtain dataSource from spring

<! -- Modify the report data access method to jndi -->
<Bean id = "dataSource" name = "default_ds" class = "org. springframework. jndi. JndiObjectFactoryBean">
<Property name = "jndiName" value = "jdbc/test"/>
<Property name = "resourceRef" value = "true"/>
</Bean>

 

4. Use a data source to connect to a database
Private Connection getConnection () throws NamingException {
Connection conn = null;
String jndi = "jdbc/test ";
Context initContext = new InitialContext ();
Context envContext = (Context) initContext. lookup ("java:/comp/env ");
DataSource ds = (DataSource) envContext. lookup (jndi );
If (ds! = Null ){
Try {
Conn = ds. getConnection ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}

Return conn;
}


Public List <String> selectById (int id) throws InstantiationException, IllegalAccessException {
Connection con = null;
Try {
Con = getConnection ();
} Catch (NamingException e1 ){
// TODO Auto-generated catch block
E1.printStackTrace ();
}
List <String> list = new ArrayList <String> ();
String SQL = "select * from myusers where id =? ";
Try {
PreparedStatement pstmt = con. prepareStatement (SQL );
Pstmt. setInt (1, id );
ResultSet rs1_pstmt.exe cuteQuery ();
If (rs. next ()){
List. add (rs. getString (1 ));
List. add (rs. getString (2 ));
List. add (rs. getString (3 ));
System. out. println (rs. getString (1 ));
System. out. println (rs. getString (2 ));
System. out. println (rs. getString (3 ));
}
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return list;
}

For more Tomcat tutorials, see the following:

Install and configure the Tomcat environment in CentOS 6.6

Install JDK + Tomcat in RedHat Linux 5.5 and deploy Java Projects

Tomcat authoritative guide (second edition) (Chinese/English hd pdf + bookmarks)

Tomcat Security Configuration and Performance Optimization

How to Use Xshell to view Tomcat real-time logs with Chinese garbled characters in Linux

Install JDK and Tomcat in CentOS 64-bit and set the Tomcat Startup Procedure

Install Tomcat in CentOS 6.5

Tomcat details: click here
Tomcat: click here

This article permanently updates the link address:

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.