• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

giving ClassCastException while deploying ejb2.0 Bmp application on jboss4.5

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kd ejb project

studentHome.java
package kda;

import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.ejb.FinderException;

public interface studentHome extends EJBHome {
public StudentRemote create(Integer studentId, String name) throws CreateException, RemoteException;
public StudentRemote findByPrimaryKey(Integer pk) throws FinderException, RemoteException;
public void makeDbTable() throws RemoteException,SQLException ;
public void deleteDbTable() throws RemoteException,SQLException;

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

StudentRemote.java

package kda;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface StudentRemote extends EJBObject {
public String getName() throws RemoteException ;
public void setName(String name) throws RemoteException;

}

-------------------------------------------

StudentBean.java

package kda;

import java.rmi.RemoteException;
import java.security.Identity;
import java.security.Principal;
import java.util.Properties;
import java.sql.*;
import java.sql.SQLException;
import javax.ejb.EJBException;
import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.ejb.EJBLocalObject;
import javax.ejb.EJBObject;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.RemoveException;
import javax.ejb.TimerService;
import javax.sql.DataSource;
import javax.transaction.UserTransaction;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.lang.*;


public class StudentBean implements EntityBean, EntityContext {
public Integer studentId;
public String name;
public EntityContext context;

public void ejbPostCreate(Integer studentId, String name){
}

public Integer ejbCreate(java.lang.Integer studentId, String name)throws CreateException{
this.studentId = studentId ;
this.name = name ;

Connection con = null;
PreparedStatement ps = null;
try{
con = this.getConnection() ;
ps = con.prepareStatement("insert into student(studentId, name) values(?,?)") ;

ps.setInt(1,studentId.intValue());
ps.setString(2,name) ;

if(ps.executeUpdate() != 1){
throw new CreateException("Failed to add student to database") ;
}
return studentId;

}
catch(SQLException e){
throw new EJBException(e) ;
}
finally{
try{ ps.close(); }catch(Exception e){System.out.println("1 Error while closing Statement");}
try{ con.close(); }catch(Exception e){System.out.println("2 Error while closing connection");}
}
}

public Integer ejbFindByPrimaryKey(Integer primaryKey)throws FinderException{
System.out.println("ejbFindByPrimaryKey() primaryKey=" + primaryKey) ;

Connection con = null ;
PreparedStatement ps = null;
ResultSet result = null;
try{
con = this.getConnection() ;
ps = con.prepareStatement("select StudentId from student where StudentId = ?");
ps.setInt(1,primaryKey.intValue());

result = ps.executeQuery() ;
if(!result.next()){
throw new ObjectNotFoundException("Cannot find student with StudentId = "+studentId) ;
}
}
catch(SQLException e){throw new EJBException(e);}
finally{
try{ result.close(); } catch (Exception e){} ;
try { ps.close() ; } catch (Exception e){} ;
try { con.close(); } catch (Exception e){ };
}
return primaryKey;

}

public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}


public void ejbLoad() throws EJBException, RemoteException {
// TODO Auto-generated method stub
Integer primaryKey = (Integer)context.getPrimaryKey();
System.out.println("ejbLoad() pk=" + primaryKey);

Connection con = null;
PreparedStatement ps = null ;
ResultSet result=null;

try{
con = this.getConnection();
ps = con.prepareStatement("select name from student where studentId=?");
ps.setInt(1,primaryKey.intValue());
result = ps.executeQuery();

if(result.next()){
studentId = primaryKey;
name = result.getString("name");
}
else{
throw new EJBException();
}
}
catch(SQLException e){
throw new EJBException(e);
}
finally{
try {result.close(); } catch(Exception e){}
try {ps.close();} catch(Exception e){}
try {con.close();} catch(Exception e){}
}
}


public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub

}


public void ejbRemove() throws RemoveException, EJBException,
RemoteException {
// TODO Auto-generated method stub
System.out.println ("ejbRemove() pk="+studentId);

Connection con = null;
PreparedStatement ps = null;
try{
con = this.getConnection ();
ps = con.prepareStatement("delete from student where studentId = ?");
ps.setInt (1, studentId.intValue ());

if (ps.executeUpdate () != 1){
throw new EJBException ("ejbRemove unable to remove bean");
}
}
catch (SQLException e){
throw new EJBException (e);
}
finally{
try { ps.close (); } catch (Exception e) {}
try { con.close (); } catch (Exception e) {}
}


}


public void ejbStore() throws EJBException, RemoteException {
// TODO Auto-generated method stub
System.out.println("ejbStore() pk="+studentId);

Connection con = null ;
PreparedStatement ps = null;
try{
con = this.getConnection();
ps = con.prepareStatement("update student set name=? where studentId=?");

ps.setString(1,name);
ps.setInt(2,studentId.intValue());

if(ps.executeUpdate() != 1){
throw new EJBException("ejbStore unable to update table");
}
}
catch(SQLException e){throw new EJBException(e);}
finally{
try{ ps.close();}catch(Exception e){}
try{ con.close();}catch(Exception e){}
}

}


public void setEntityContext(EntityContext ctx) throws EJBException,
RemoteException {
// TODO Auto-generated method stub
context = ctx;
}


public void unsetEntityContext() throws EJBException, RemoteException {
// TODO Auto-generated method stub
context = null;
}


public EJBLocalObject getEJBLocalObject() throws IllegalStateException {
// TODO Auto-generated method stub
return null;
}


public EJBObject getEJBObject() throws IllegalStateException {
// TODO Auto-generated method stub
return null;
}


public Object getPrimaryKey() throws IllegalStateException {
// TODO Auto-generated method stub
return null;
}


public Identity getCallerIdentity() {
// TODO Auto-generated method stub
return null;
}


public Principal getCallerPrincipal() {
// TODO Auto-generated method stub
return null;
}


public EJBHome getEJBHome() {
// TODO Auto-generated method stub
return null;
}


public EJBLocalHome getEJBLocalHome() {
// TODO Auto-generated method stub
return null;
}


public Properties getEnvironment() {
// TODO Auto-generated method stub
return null;
}


public boolean getRollbackOnly() throws IllegalStateException {
// TODO Auto-generated method stub
return false;
}


public TimerService getTimerService() throws IllegalStateException {
// TODO Auto-generated method stub
return null;
}


public UserTransaction getUserTransaction() throws IllegalStateException {
// TODO Auto-generated method stub
return null;
}


public boolean isCallerInRole(Identity arg0) {
// TODO Auto-generated method stub
return false;
}


public boolean isCallerInRole(String arg0) {
// TODO Auto-generated method stub
return false;
}


public Object lookup(String arg0) {
// TODO Auto-generated method stub
return null;
}


public void setRollbackOnly() throws IllegalStateException {
// TODO Auto-generated method stub

}
public String getName(){
System.out.println("setName()");
return name;
}
public void setName (String n){
System.out.println ("setName()");
name = n;
}
public void ejbHomeMakeDBTable()throws RemoteException,SQLException{
PreparedStatement ps = null;
Connection con = null;

try{
con = this.getConnection ();

System.out.println("Creating table student...");
ps = con.prepareStatement ("CREATE TABLE student ( " +"StudentId INT PRIMARY KEY, " +"NAME CHAR(30))" );
ps.execute();
System.out.println("...done!");
}
finally
{
try { ps.close (); } catch (Exception e) {}
try { con.close (); } catch (Exception e) {}
}

}
public void ejbHomeDeleteDBTable()throws RemoteException,SQLException{
PreparedStatement ps = null;
Connection con = null;

try
{
con = this.getConnection ();

System.out.println("Dropping table student...");
ps = con.prepareStatement ("DROP TABLE student");
ps.execute();
System.out.println("...done!");
}
finally
{
try { ps.close (); } catch (Exception e) {}
try { con.close (); } catch (Exception e) {}
}

}
private Connection getConnection()throws SQLException{
try{
javax.naming.Context jndiCntx =new InitialContext();
DataSource ds =(DataSource)jndiCntx.lookup("java:comp/env/jbdc/studentbmpHom");
return ds.getConnection ();
}
catch (NamingException e){
throw new EJBException (e);
}

}

}



ejb-jar.xml

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar id="ejb-jar_ID" version="2.1" 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/ejb-jar_2_1.xsd">
<enterprise-beans>
<entity>
<display-name>KedaFaMo</display-name>
<ejb-name>StudentBean</ejb-name>
<home>kda.studentHome</home>
<remote>kda.StudentRemote</remote>
<ejb-class>kda.StudentBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>false</reentrant>
<resource-ref>
<res-ref-name>jdbc/studentbmpHom</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</entity>
</enterprise-beans>
</ejb-jar>

---------------------------------------------------------
Fa web project

Appli.jsp

<title>html page</title>
<script type="text/javascript">
function login_validation(){
if(document.frm.ftName.value==""){
alert("first name empty");
document.frm.ftName.focus();
}
else if(document.frm.stName.value==""){
alert("last name empty");
document.frm.stName.focus();
}
else{
//alert("sarsw");

document.frm.action="famothe.jsp";
document.frm.submit();

}
}
</script>
</head>
<body>
<form name="frm" method="post">
Enter your first name: <input type="text" name="ftName" size="20"/>
Enter your second name: <input type="text" name="stName" size="20"/>
  <input type="button" name="B1" value="login" onClick="login_validation()" />

</form>
</body>
</html>
--------------------------------------------------------------------
famothe.jsp


<%@ page language="java" import="kda.*,kda.StudentBean,javax.naming.*,javax.naming.NamingException,javax.rmi.PortableRemoteObject,java.rmi.RemoteException,java.util.Properties,java.util.Hashtable,javax.sql.DataSource,java.sql.Connection" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="javax.ejb.CreateException"%>
<%@page import="javax.ejb.FinderException"%>
<%@page import="kda.studentHome"%>
<%@page import="kda.StudentRemote"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>entity bean application</title>
</head>
<body>
<%
String s1="createtable";
String s2="droptable";
studentHome hom;
StudentRemote stud;
String cr=request.getParameter("ftName");
String dr=request.getParameter("stName");
try{

Hashtable ht1=new Hashtable();

ht1.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
ht1.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
ht1.put("java.naming.provider.url","localhost:1099");

Context ic2 = new InitialContext(ht1);
Object o=ic2.lookup("java:jdbc/studentbmpHom");

hom=(studentHome)PortableRemoteObject.narrow(o,studentHome.class);


if(s1==cr){%>
out.println ("Creating database table...");
hom.makeDbTable();
<%}
else if(s2==dr){%>
out.println ("Dropping database table...");
hom.deleteDbTable ();
<%}
else{ %>

stud = hom.create(new Integer(1),"John");
stud = hom.create(new Integer(2),"Jackson");
stud = hom.create(new Integer(3),"Rosy");
stud = hom.create(new Integer(4),"Ana");


<%for(int i=1;i<=4;i++){%>
Integer pk = new Integer(i);
StudentRemote stud1 = hom.findByPrimaryKey(pk);
out.println("Name " + i + " : " + stud1.getName());
<%}
}
}

catch(Exception e5){System.out.print("Exception"+ e5);}

%>
</body>
</html>
---------------------------------------------------------

jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<resource-ref>
<res-ref-name>jdbc/studentbmpHom</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<jndi-name>java:jdbc/studentbmpHom</jndi-name>
</resource-ref>
</jboss-web>
-------------------------------------------------

web.xml

<resource-ref>
<description>dbconnection</description>
<res-ref-name>jdbc/studentbmpHom</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

------------------------------------------------



14:38:25,015 INFO [TomcatDeployer] deploy, ctxPath=/famoclt, warUrl=file:/D:/work/.metadata/.plugins/org.jboss.ide.eclipse.as.core/JBoss_4.2_Server/deploy/famoclt.war/

14:39:24,687 INFO [STDOUT] Exceptionjava.lang.ClassCastException


 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ami,

If you look at your post, you'll find that it's really hard to read the contents. Could you please format it properly by wrapping it in code blocks, wherever appropriate? Also, please use the Preview button to check before submitting the post. And only post relevant data instead of the entire set of files/code.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic