users@glassfish.java.net

Re: Issue with DataSource lookup in Glassfish

From: <glassfish_at_javadesktop.org>
Date: Tue, 06 Jul 2010 03:12:10 PDT

Thanks for your help.

I've no idea how to modify web.xml/sun-web.xml files in terms of syntax, so I will need to look into that (will report back if I can figure it out).

I've tried looking up the JDBC Resource directly, the program failed with the same kind of error:

Lookup failed for 'jdbc/MySQLDataSource/world' in SerialContext

If I use jdbc/MySQLDataSource as the lookup string I get a different error message:

'No database selected'

I'm putting the complete app code here in case that sheds some light on anything:

[code]
<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>
<html>
<head><title>Hello world from JSP</title></head>
<body bgcolor="white">
<img src="images/duke.waving.gif">
<%
  InitialContext ctx;
  DataSource ds;
  Connection conn;
  Statement stmt;
  ResultSet rs;

  try {
    ctx = new InitialContext();
    //ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MySQLDataSource");
    ds = (DataSource) ctx.lookup("jdbc/MySQLDataSource/world");
    conn = ds.getConnection();
    stmt = conn.createStatement();
    rs = stmt.executeQuery("SELECT * FROM Country");

    while(rs.next()) {
%>
    <h3>Name: <%= rs.getString("Name") %></h3>
    <h3>Population: <%= rs.getString("Population") %></h3>
<%
    }
  }
  catch (SQLException se) {
%>
    <%= se.getMessage() %>
<%
  }
  catch (NamingException ne) {
%>
    <%= ne.getMessage() %>
<%
  }
%>
</body>
</html>
[/code]

And here's the sun-web.xml file:

[code]
<?xml version="1.0" encoding="UTF-8"?>

<!--
 Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
 Use is subject to license terms.
-->

<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-1.dtd">
<sun-web-app>
  <context-root>/HelloWebApp</context-root>
</sun-web-app>
[/code]

And the web.xml file:

[code]
<?xml version="1.0" encoding="UTF-8"?>

<!--
 Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
 Use is subject to license terms.
-->

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>HelloWebApp</display-name>
  <distributable/>
</web-app>
[/code]
[Message sent by forum member 'codefire']

http://forums.java.net/jive/thread.jspa?messageID=476853