Originally posted by Valentin Tanase:
It has everything to do with your error. Install the mssql libraries corectely and we shall see from there. You might set data sources and connection pools as well, but you first need to install the required libraries.
Regards.
Account.java---> remote interface
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Account extends EJBObject
{
public
String getDate() throws RemoteException;
public String getVc() throws RemoteException;
public String getCheck() throws RemoteException;
public double getAmount() throws RemoteException;
}
AcHome.java-->Home Interface
import java.rmi.RemoteException;
import javax.ejb.*;
public interface AcHome extends EJBHome
{
public Account create(String id,String date,String vc,String check,double amount) throws RemoteException,CreateException;
public Account findByPrimaryKey(String id) throws FinderException,RemoteException;
}
AcEJB.java-->EJB
java ( Entity bean )
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
public class AcEJB implements EntityBean
{
private String id;
private String date;
private String vc;
private String check;
private double amount;
private EntityContext context;
private Connection con;
private String dbName="java:comp/env/jdbc/MyDataSource";
public String getDate()
{
return date;
}
public String getVc()
{
return vc;
}
public String getCheck()
{
return check;
}
public double getAmount()
{
return amount;
}
public String ejbCreate(String id,String date,String vc,String check,double amount) throws CreateException
{
try
{
insertRow(id,date,vc,check,amount);
}
catch(Exception ex)
{
throw new EJBException("ejbCreate: "+ex.getMessage());
}
this.id=id;
this.date=date;
this.vc=vc;
this.check=check;
this.amount=amount;
return id;
}
public String ejbFindByPrimaryKey(String primaryKey)throws FinderException
{
boolean result;
try
{
result=selectByPrimaryKey(primaryKey);
}
catch(Exception ex)
{
throw new EJBException("ejbFindByPrimaryKey: "+ex.getMessage());
}
if(result)
{
return primaryKey;
}
else
{
throw new ObjectNotFoundException("Row for id "+primaryKey+" not found");
}
}
public void ejbRemove()
{
try
{
deleteRow(id);
}
catch (Exception ex)
{
throw new EJBException("ejbRemove: "+ex.getMessage());
}
}
public void setEntityContext(EntityContext context)
{
this.context=context;
try
{
makeConnection();
}
catch (Exception ex)
{
throw new EJBException("Unable to connect to Database."+ex.getMessage());
}
}
public void unsetEntityContext()
{
try
{
con.close();
}
catch (SQLException ex)
{
throw new EJBException("unsetEntityContext: "+ex.getMessage());
}
}
public void ejbActivate()
{
id=(String)context.getPrimaryKey();
}
public void ejbPassivate()
{
id=null;
}
public void ejbLoad()
{
try
{
loadRow();
}
catch(Exception ex)
{
throw new EJBException("ejbLoad: "+ex.getMessage());
}
}
public void ejbStore()
{
try
{
storeRow();
}
catch(Exception ex)
{
throw new EJBException("ejbLoad: "+ex.getMessage());
}
}
public void ejbPostCreate(String id,String date,String vc,String check,double amount){}
private void makeConnection() throws NamingException,SQLException
{
InitialContext ic=new InitialContext();
DataSource ds=(DataSource)ic.lookup(dbName);
con=ds.getConnection();
}
private void insertRow(String id,String date,String vc,String check,double amount) throws SQLException
{
String insertStatement="insert into Account_Holder_Transaction values (?,?,?,?,?)";
PreparedStatement prepStmt=con.prepareStatement(insertStatement);
prepStmt.setString(1,id);
prepStmt.setString(2,date);
prepStmt.setString(3,vc);
prepStmt.setString(4,check);
prepStmt.setDouble(5,amount);
prepStmt.executeUpdate();
prepStmt.close();
}
private void deleteRow(String id) throws SQLException
{
String deleteStatement="delete from Account_Holder_Transaction where cAccount_id=?";
PreparedStatement prepStmt=con.prepareStatement(deleteStatement);
prepStmt.setString(1,id);
prepStmt.executeUpdate();
prepStmt.close();
}
private boolean selectByPrimaryKey(String primaryKey) throws SQLException
{
String selectStatement="select cAccount_id "+"from Account_Holder where cAccount_id=?";
PreparedStatement prepStmt=con.prepareStatement(selectStatement);
prepStmt.setString(1,primaryKey);
ResultSet rs=prepStmt.executeQuery();
boolean result=rs.next();
prepStmt.close();
return result;
}
private void loadRow() throws SQLException
{
String selectStatement="select dDate_of_transaction,vcParticulars,cCheck_no,mAmount from Account_Holder_Transaction where cAccount_id=?";
PreparedStatement prepStmt=con.prepareStatement(selectStatement);
prepStmt.setString(1,this.id);
ResultSet rs=prepStmt.executeQuery();
if(rs.next())
{
this.date=rs.getString(1);
this.vc=rs.getString(2);
this.check=rs.getString(3);
this.amount=rs.getDouble(4);
prepStmt.close();
}
else
{
prepStmt.close();
throw new NoSuchEntityException("Row for id"+id+" not found in database.");
}
}
private void storeRow() throws SQLException
{
String updateStatement="update Account_Holder_Transaction set dDate_of_transaction=?,vcParticulars=?,cCheck_no=?,mAmount=? where cAccount_id=?";
PreparedStatement prepStmt=con.prepareStatement(updateStatement);
prepStmt.setString(1,date);
prepStmt.setString(2,vc);
prepStmt.setString(3,check);
prepStmt.setDouble(4,amount);
prepStmt.setString(5,id);
prepStmt.executeUpdate();
prepStmt.close();
int rowcount=prepStmt.executeUpdate();
prepStmt.close();
if(rowcount==0)
{
throw new EJBException("Storing Row for id "+id+" failed.");
}
}
}
AccountBean.java-->Bean Accessing EJB
import java.util.*;
import java.io.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class AccountBean
{
private String action;
private String id;
private String dateTran;
private String vc;
private String check;
private double amount;
private AcHome accountHome;
private Account account;
public AccountBean()
{
try
{
Context ic=new InitialContext();
java.lang.Object objref=ic.lookup("java:comp/env/ejb/Account");
accountHome=(AcHome)PortableRemoteObject.narrow(objref,AcHome.class);
}
catch(Exception re)
{
System.err.println("Couldn't locate Account Home");
re.printStackTrace();
}
reset();
}
public String processRequest()
{
String message="";
System.out.println("Process request called ");
System.out.println(this);
try
{
if(action.equals("save"))
{
account=accountHome.create(id,dateTran,vc,check,amount);message="Recorded details for account '"+id+"'";
}
}
catch(Exception e)
{
message=e.toString();
}
return message;
}
public String getAction()
{
System.out.println("Getting Action");
return action;
}
public void setAction(String a)
{
System.out.println("Setting Action : "+a);
action=a;
}
public String getId()
{
System.out.println("Getting id");
return id;
}
public void setId(String i)
{
System.out.println("Setting id : "+i);
id=i;
}
public String getDateTran()
{
System.out.println("Getting transaction date");
return dateTran;
}
public void setDateTran(String f)
{
System.out.println("Setting transaction date : "+f);
dateTran=f;
}
public String getVc()
{
System.out.println("Getting particulars");
return vc;
}
public void setVc(String l)
{
System.out.println("Setting Action : "+l);
vc=l;
}
public String getCheck()
{
System.out.println("Getting check number");
return check;
}
public void setCheck(String b)
{
System.out.println("Setting check number : "+b);
check=b;
}
public double getAmount()
{
System.out.println("Getting amount");
return amount;
}
public void setAmount(double a)
{
System.out.println("Setting Amount : "+a);
amount=a;
}
private void reset()
{
final String emptyString="";
final double zero=0.0;
setAction(emptyString);
setId(emptyString);
setDateTran(emptyString);
setVc(emptyString);
setCheck(emptyString);
setAmount(zero);
}
private void loadFromEJB()
{
try
{
dateTran=account.getDate();
setDateTran(dateTran);
setVc(account.getVc());
setCheck(account.getCheck());
double a=account.getAmount();
setAmount(a);
}
catch(Exception re)
{
System.err.println("Failed to load AccountBean from AcEJB.");
re.printStackTrace();
}
}
public String toString()
{
StringBuffer output=new StringBuffer();
output.append("Action : "+action);
output.append("Id : "+id);
output.append("Transaction Date : "+dateTran);
output.append("Particulars : "+vc);
output.append("Check Number : "+check);
output.append("Amount : "+amount);
return output.toString();
}
}
Account.jsp
<html>
<
jsp:useBean id="accountBean" scope="session" class="AccountBean"/>
<jsp:setProperty name="accountBean" property="*"/>
<%! String status; %>
<% status=accountBean.processRequest(); %>
<html>
<head>
<title>Account Interface</title>
</head>
<body bgcolor="pink">
<font size=5 color="#CC0000">
<h1><b><center>Earnest Bank ATM</center></b></h1>
<br>
<form method=POST action=Account.jsp>
<BR>
<br>
<table border=0>
<tr>
<td>
Account ID
</td>
<td>
<INPUT type=text name="id" size="8" value="<jsp:getProperty name="accountBean" property="id"/>">
</td>
<td>
Date Of Transaction
</td>
<td>
<INPUT type=text name="dateTran" size="8" value="<jsp:getProperty name="accountBean" property="dateTran"/>">
</td>
</tr>
<tr>
<td>
Particulars
</td>
<td>
<INPUT type=text name="vc" size="8" value="<jsp:getProperty name="accountBean" property="vc"/>">
</td>
<td>
Check Number
</td>
<td>
<INPUT type=text name="check" size="8" value="<jsp:getProperty name="accountBean" property="check"/>">
</td>
</tr>
<tr>
<td>
Amount
</td>
<td>
<INPUT type=text name="amount" size="8" value="<jsp:getProperty name="accountBean" property="amount"/>">
</td>
</tr>
</table>
<br>
<br>
<INPUT type=hidden name="action" value="save">
<br>
<br>
<InPUT type=submit name="submit" value="Submit">
</form>
</FONT>
</body>
</html>
<hr>
<h3><b>Status :</b></h3> <%=status%>
</html>
</html>
--> I feel that the code doesn't have any error
-->Could U tell where to find jar file[MS-SQL],either on cd or sql installation directory