You are on page 1of 2

Instrucciones para la definicin de un Datasource en Tomcat 6 Mdulo4

1. Copy the JDBC Driver's jar into $CATALINA_HOME/lib

2. Add a resource entry to $CATALINA_HOME/config/context.xml


<Context <Resource name="jdbc/bibliotecads" auth="Container" type="javax.sql.DataSource" maxActive="10" maxIdle="2" maxWait="5000" username="biblioteca" password="biblioteca" driverClassName="org.apache.derby.jdbc.ClientDriver" url="jdbc:derby://localhost:1527/C:\\MisBasesDeDatos\\Atrium" /> </Context>

3. Add the following entry to the application WEB-INF/web.xml


<web-app <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/bibliotecads</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>

Master Cert. Experto Java JavaEE Struts Liferay Ajax

Instrucciones para la definicin de un Datasource en Tomcat 6 Mdulo4


4. In the program code use the following to obtain a connection
public static Connection getConexion() { Connection con = null; try { /* * The InitialContext is made available to web application * components (for read-only access). */ Context ctx = new InitialContext(); /* * Look up the datasource from the starting portion of the JNDI * namespace being served up by tomcat. All configured entries * and resources are placed in the java:comp/env portion of the * JNDI namespace. When accessing the datasource programmatically, * remember to prepend java:/comp/env to your JNDI lookup */ DataSource ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/bibliotecads"); /* * finally get the connection object */ con = ds.getConnection(); System.out.println("conexion "+con+" obtenido desde el pool: "+ds); } catch (Exception e) { throw new RuntimeException( "\nNo se puede conectar dado que datasource=jdbc/bibliotecads+ "\n" + e.getMessage() ); } return con; } public static void cerrar(Connection conn) throws SQLException{ if (conn != null) { conn.close(); System.out.println("conexion devuelta al pool"); } }

Master Cert. Experto Java JavaEE Struts Liferay Ajax

You might also like