Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » JSP Interest »

Re: How to trigger an event to fetch data from DB?

Ken Robinson

2005-08-18

Replies:

Try the following:

Add a JNDI data resource in your server.xml file??

Add a reference to that JNDI resource inside of your application's context.

Create a class to access the JNDI resource

Create a class that mirrors or maps the data from your database

Run the query and populate your mapped data class in your jsp page.


Steps3 & 4 are below

---------------------
import javax.naming.*;
import javax.sql.*;
import java.sql.*;


public class prepareStoredProcedure {
  private static DataSource ds;

    public static void main(String[] args) {
    }

  private static void init() throws Exception {
    try {
       Context ctx = new InitialContext();

       if (ctx == null) throw new Exception("Boom - No Context");

       ds =
(DataSource)ctx.lookup("java:comp/env/jdbc/MyDataSourceName");
       System.out.println( ds.toString());
       if (ds == null) throw new Exception("Boom - no datasource
available");
    } catch (Exception ex) {
       ex.printStackTrace();
       throw ex;
    }
  }

  public static Connection getConnection() throws Exception {
    if (ds == null) init();
    return ds.getConnection();
  }


}

-----------------------------------------------------------
public class MyNewMenu {

    public static void main(String[] args) {
    }
    public static List getNavItems(){
          Connection conn     = null;
          ArrayList lRetrievedRecs = new ArrayList();
          try
          {
               conn = prepareStoredProcedure.getConnection();
               String sQuery = " SELECT field1, field2, field3, etc
FROM      tblnavigation ";
               Statement stmt = conn.createStatement();
               ResultSet rs  = stmt.executeQuery(sQuery);
               if ( rs == null) {
                            rs.close();
                            stmt.close();
               }
               while (rs.next()) {
                    Object[] obj = new Object[3];
                    obj[0] = (Integer) new
Integer(rs.getInt("navid"));
                    obj[1] = (Integer) new
Integer(rs.getInt("parentid"));
                    obj[2] = (String) rs.getString("NavTitle");
                    lRetrievedRecs.add(obj);
               }
               rs.close();
               stmt.close();
          }
          catch (SQLException e)
          {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          finally
          {
            try {
                 if(!(conn.isClosed())) conn.close();
            }catch(Exception e) {}
          }
          return lRetrievedRecs;
    }

}
--------------------

Hope this gets you in the right direction.


-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:JSP-INTEREST@(protected)
Sent: Thursday, August 18, 2005 2:46 PM
To: JSP-INTEREST@(protected)
Subject: Re: How to trigger an event to fetch data from DB?

I guess I need a ServletContextListener...

===========================================================================
To unsubscribe: mailto listserv@(protected)
JSP-INTEREST".
For digest: mailto listserv@(protected)
DIGEST".

Some relevant archives, FAQs and Forums on JSPs can be found at:

http://java.sun.com/products/jsp
http://archives.java.sun.com/jsp-interest.html
http://forums.java.sun.com
http://www.jspinsider.com

===========================================================================
To unsubscribe: mailto listserv@(protected)".
For digest: mailto listserv@(protected)".

Some relevant archives, FAQs and Forums on JSPs can be found at:

http://java.sun.com/products/jsp
http://archives.java.sun.com/jsp-interest.html
http://forums.java.sun.com
http://www.jspinsider.com
©2008 junlu.com - Jax Systems, LLC, U.S.A.