Tomcat allows you toconfigure DataSources for your J2EE web application in a context.xml file that is stored in your web application project.
1. Install the JDBC Driver
2. Install the .jar file(s) containing the JDBC driver in Tomcat's common/lib folder.
You do not need to put them in your application's WEB-INF/lib folder.
When working with J2EE DataSources, the web application server manages connections for your application.
3. Create META-INF/context.xml
In the root of your web app directory structure, create a folder named META-INF (all caps).
Inside that folder, create a file named context.xml that contains a Resource like this :
(example: SQL server)
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/myDB" auth="Container"
type="javax.sql.DataSource" username="wally" password="wally"
type="javax.sql.DataSource" username="wally" password="wally"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost;DatabaseName=mytest;
SelectMethod=cursor;" maxActive="8" />
</Context>
4. Access the data in a servlet :
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/myDB");
Connection c = ds.getConnection();
...
c.close();
Note: when doing the DataSource lookup, you must prefix the JNDI name ofthe resource with java:comp/env/
No comments:
Post a Comment
Note: only a member of this blog may post a comment.