Getting A Data Source From Tomcat
Join the DZone community and get the full member experience.Join For Free
// You can setup a data source in tomcat using a context file // or you can set one up using the Administration web pages // as well. Either way you do it, here is the simple code to // get the data source from Tomcat so you can start pulling // out database connections.
// Obtain our environment naming context Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); // Look up our data source by the name we gave it when we created it. // In this case that's "jdbc/EmployeeDB". DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB");
Opinions expressed by DZone contributors are their own.