JSP, EJB in Jboss 4 with mySQL database. Error in connecting to database

Hi, i using JBoss 4-0-1 with jsp and mySQL database. I get this example from a book using stateless session beans. However i modify it so it can connect to mySQL database.

This is my code for jsp.

<%@ page import="asg.MusicEJB.*,

java.util.*,

javax.naming.Context,

javax.naming.InitialContext,

javax.rmi.PortableRemoteObject" errorPage="error.jsp" %>

<%--

The following 3 variables appear in a JSP declaration.

They appear outside the generated _jspService() method,

which gives themclass scope.

Since we want to initialize our Music EJB object once

and read the Music Collection database to get the

recording titles once, we placethis code inside

method jspInit().

The EJB business method getMusicList()

returns a collection of RecordingVO objects which we

store in ArrayList albums.

--%>

<%!

MusicHome musicHome;

Music mymusic;

ArrayList albums;

Properties properties=new Properties();

publicvoid jspInit(){

properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");

properties.put(Context.PROVIDER_URL,"localhost:3306");

System.out.println("............................in properties");

try{

//Context initial = new InitialContext();

InitialContext jndiContext =new InitialContext(properties);

//Object ref = jndiContext.lookup("MusicEJB");

Object objref = jndiContext.lookup("java:comp/env/ejb/EJBMusic");

musicHome = (MusicHome)PortableRemoteObject.narrow(objref, MusicHome.class);

mymusic = musicHome.create();

albums = mymusic.getMusicList();

System.out.println(".............................in line 64");

}catch (Exception ex){

System.out.println("Unexpected Exception............: " +

ex.getMessage());

ex.printStackTrace();

}

}

%>

<%--

The following scriptlet accesses the implicit

request object to obtain the current URL

and saves it to the session objectfor later retrieval.

It also saves variable mymusic, so we can

make remote calls to our Music EJB, and the collection of

RecordingVO objects. These variables will all be available

to other pages within the session.

--%>

<%

String requestURI = request.getRequestURI();

session.putValue("url", requestURI);

session.putValue("mymusic", mymusic);

session.putValue("albums", albums);

%>

<html>

<head>

<title>Music Collection Database Using EJB & JSP Version 9.7</title>

</head>

<body bgcolor=white>

<h1><b><center>Music Collection Database Using EJB & JSP</center></b></h1>

<hr>

There are <%= albums.size() %> recordings.

<form method="post" action="musicPost.jsp">

Select Music Recording:

<select name="TITLE">

<%

// Generate html <option> elements with the recording

// titles stored in each RecordingVO element.

// Obtain the current title from the session object

// (this will be null the first time).

String title;

String currentTitle = (String)session.getValue("curTitle");

if (currentTitle ==null) currentTitle ="";

RecordingVO r;

Iterator i = albums.iterator();

while (i.hasNext()){

r = (RecordingVO)i.next();

title = r.getTitle();

if (title.equals(currentTitle)){

out.println("<option selected>" + title +"</option>");

}

else{

out.println("<option>" + title +"</option>");

}

}

%>

</select>

<%--

Provide a"View Tracks" button to submit

the requested title to page musicPost.jsp

--%>

<input TYPE="submit" NAME="View" VALUE="View Tracks">

</form>

</body>

</html>

Message was edited by:

chongming

[5766 byte] By [chongminga] at [2007-10-3 1:32:49]
# 1

This is the deployment descriptor for the .ear file.

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

<!DOCTYPE application PUBLIC '-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN' 'http://java.sun.com/dtd/application_1_3.dtd'>

<application>

<display-name>MusicDAOApp</display-name>

<description>Application description</description>

<module>

<web>

<web-uri>war-ic.war</web-uri>

<context-root>music</context-root>

</web>

</module>

<module>

<ejb>ejb-jar-ic.jar</ejb>

</module>

<!--

<module>

<java>app-client-ic.jar</java>

</module>

-->

</application>

And this is the deployment for the ejb class files:

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

<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>

<ejb-jar>

<display-name>MusicEJB</display-name>

<enterprise-beans>

<session>

<display-name>MusicEJB</display-name>

<ejb-name>MusicEJB</ejb-name>

<home>asg.MusicEJB.MusicHome</home>

<remote>asg.MusicEJB.Music</remote>

<ejb-class>asg.MusicEJB.MusicBean</ejb-class>

<session-type>Stateless</session-type>

<transaction-type>Bean</transaction-type>

<env-entry>

<env-entry-name>MusicDAOClass</env-entry-name>

<env-entry-type>java.lang.String</env-entry-type>

<env-entry-value>asg.MusicEJB.MusicDAOCloudscape</env-entry-value>

</env-entry>

<env-entry>

<env-entry-name>dbUrl</env-entry-name>

<env-entry-type>java.lang.String</env-entry-type>

<env-entry-value>jdbc:mysql://localhost/music</env-entry-value>

</env-entry>

<env-entry>

<env-entry-name>dbUserName</env-entry-name>

<env-entry-type>java.lang.String</env-entry-type>

<env-entry-value>chongming</env-entry-value>

</env-entry>

<env-entry>

<env-entry-name>dbPassword</env-entry-name>

<env-entry-type>java.lang.String</env-entry-type>

<env-entry-value>kcm82</env-entry-value>

</env-entry>

<security-identity>

<description></description>

<use-caller-identity></use-caller-identity>

</security-identity>

</session>

</enterprise-beans>

</ejb-jar>

I can combine the jar and war files into a ear file. deploying is alright without any errors.

However when i run the jsp, it prompt this error:

You Have Encountered an Error

Stack Trace

java.lang.NullPointerException

at org.apache.jsp.musicGet_jsp._jspService(org.apache.jsp.musicGet_jsp:111)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)

at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)

at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:159)

at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)

at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)

at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)

at java.lang.Thread.run(Thread.java:595)

How to i solve the error? I look at he catch results in the command prompt of JBoss , i found that when running, the code will be caught by the exception and display this:int.java:527)

at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWor

kerThread.java:112)

at java.lang.Thread.run(Thread.java:595)

13:55:21,375 INFO [STDOUT] Unexpected Exception............: EJBException:; nes

ted exception is:

javax.ejb.EJBException: getMusicList: SQLException during DB Connection:

The url cannot be null

13:55:21,375 INFO [STDOUT] java.rmi.ServerException: EJBException:; nested exce

ption is:

javax.ejb.EJBException: getMusicList: SQLException during DB Connection:

The url cannot be null 13:55:21,375 INFO[STDOUT]at org.jboss.ejb.plugins.LogInterceptor.handleException(LogInterceptor.java:352)

13:55:21,375 INFO [STDOUT]at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:196)

What can i do to solve the problem? please helpMessage was edited by:

chongming

Message was edited by:

chongming

Message was edited by:

chongming

chongminga at 2007-7-14 18:30:45 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...