Results 1 to 2 of 2
  1. #1
    computerbum is offline Member
    Join Date
    Feb 2010
    Rep Power

    Default create a poolable connection using mysql eclipse helios and apache tomcat 6 with java

    Hello everyone,

    I am trying to setup a poolable connection using eclipse helios, apache tomcat 6, mysql, and java.

    I was able to get a connection through a jsp page that would return data following this tutorial from:

    Apache Tomcat 6.0 - JNDI Datasource HOW-TO

    But now I want to create a class in java that will get my connection from the context.xml.

    I am in desperate need here, please help!

  2. #2
    computerbum is offline Member
    Join Date
    Feb 2010
    Rep Power


    Here is my context.xml stored my META-INF
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
        <!-- Specify a JDBC datasource -->
            maxActive="10" maxIdle="4"

    Here is the class I am running and it fails on this line:

    DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/wrights_auto");

    error message: Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(N

    Java Code:
    package com.adc.wrights.common;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.naming.InitialContext;
    import javax.sql.DataSource;
    public class DAO {
        public void doSomething() throws Exception {
             * Create a JNDI Initial context to be able to
             *  lookup  the DataSource
             * In production-level code, this should be cached as
             * an instance or static variable, as it can
             * be quite expensive to create a JNDI context.
             * Note: This code only works when you are using servlets
             * or EJBs in a J2EE application server. If you are
             * using connection pooling in standalone Java code, you
             * will have to create/configure datasources using whatever
             * mechanisms your particular connection pooling library
             * provides.
            InitialContext ctx = new InitialContext();
              * Lookup the DataSource, which will be backed by a pool
              * that the application server provides. DataSource instances
              * are also a good candidate for caching as an instance
              * variable, as JNDI lookups can be expensive as well.
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/wrights_auto");
             * The following code is what would actually be in your
             * Servlet, JSP or EJB 'service' method...where you need
             * to work with a JDBC connection.
            Connection conn = null;
            Statement stmt = null;
            try {
                conn = ds.getConnection();
                 * Now, use normal JDBC programming to work with
                 * MySQL, making sure to close each resource when you're
                 * finished with it, which allows the connection pool
                 * resources to be recovered as quickly as possible
                stmt = conn.createStatement();
                stmt.execute("select * from clients");
                stmt = null;
                conn = null;
            } finally {
                 * close any jdbc instances here that weren't
                 * explicitly closed during normal code path, so
                 * that we don't 'leak' resources...
                if (stmt != null) {
                    stmt = null;
                if (conn != null) {
                    conn = null;
    	 * @param args
    	 * @throws Exception 
    	 * @throws IOException 
    	 * @throws ServletException 
    	 * @throws SQLException 
    	public static void main(String[] args) throws Exception {
    		// TODO Auto-generated method stub
    		DAO conn = new DAO();
    		//Connection con = conn.dataSource.getConnection();
    Last edited by computerbum; 02-19-2011 at 10:35 PM. Reason: made an error

Similar Threads

  1. Helios do not start tomcat 7
    By pjava in forum Java Servlet
    Replies: 9
    Last Post: 12-23-2010, 06:29 PM
  2. apache tomcat 7 and eclipse
    By marie in forum Eclipse
    Replies: 1
    Last Post: 11-04-2010, 05:40 PM
  3. Hibernate perspective in Eclipse Helios
    By redforce.bala in forum Eclipse
    Replies: 0
    Last Post: 10-01-2010, 07:55 AM
  4. Problem with running eclipse-jee-helios-win32-x86_64
    By in forum Eclipse
    Replies: 2
    Last Post: 09-09-2010, 03:48 PM
  5. Replies: 1
    Last Post: 10-05-2008, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts