SessionBean呼叫資料庫的問題

trainking發表於2003-08-20
我想試驗一下用SessionBean呼叫資料庫,因此寫了以下程式:
deleteEJBBean.java:

package MydeleteEJB;
import java.rmi.RemoteException;
import java.sql.*;
import javax.ejb.*;
import javax.sql.*;
import javax.naming.*;

public class deleteEJBBean implements SessionBean
{String Driver="org.gjt.mm.mysql.Driver";
String URL="jdbc:mysql://localhost:3306/CustomerDB";
String user="root";
String password="java123";
private Connection con;
private SessionContext context;

public void deleteRecord(int ssn)
{
rs=null;
try {
Statement smt=con.createStatement();
ResultSet rs=smt.executeQuery("select * from customer");
rs.absolute(ssn);
rs.deleteRow();
System.out.println("OK");
}
catch(Exception e)
{System.out.println("wrong");
e.printStackTrace();}
}


public deleteEJBBean(){}


public void ejbCreate()throws CreateException{
String selectStatement="SELECT * FROM customer";
try{
makeConnection();
}
catch(Exception e)
{throw new CreateException(e.getMessage());}
}



public void ejbRemove(){
try{
con.close();
}
catch(SQLException ex){
throw new EJBException(ex.getMessage());
}
}



public void ejbActivate(){
try{
makeConnection();
}
catch(Exception ex){
throw new EJBException(ex.getMessage());
}}



public void ejbPassivate(){
try{
con.close();
}
catch(SQLException ex){
throw new EJBException(ex.getMessage());
}}



public void setSessionContext(SessionContext sc){
this.context=sc;}



private void makeConnection() throws NamingException,SQLException{
try{
Class.forName(Driver);
}
catch(Exception e){System.out.println("cannot load driver:"+Driver);
e.printStackTrace();}
try{
Connection con=DriverManager.getConnection(URL,user,password);
if(!con.isClosed())System.out.println("Open success!");
}
catch(SQLException SE){System.out.println("Open Failed");
SE.printStackTrace();}
}
}

呼叫該SessionBean的Servlet如下:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.ejb.FinderException;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import javax.ejb.CreateException;


import MydeleteEJB.deleteEJB;
import MydeleteEJB.deleteEJBHome;

public class deleteServlet extends HttpServlet{
deleteEJBHome home=null;
public void init() throws ServletException{
try{
InitialContext ic=new InitialContext();
Object objref=ic.lookup("MydeleteEJB");
home=(deleteEJBHome)PortableRemoteObject.narrow(objref,deleteEJBHome.class);
}
catch(Exception e){
e.printStackTrace();
throw new ServletException(e.getMessage());
}
}


public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{res.setContentType("text/html;charset=GBK");
PrintWriter out=res.getWriter();
if(req.getParameterValues("strtext")!=null)
{String str=req.getParameter("strtext");
int ssn=Integer.parseInt(str);
try{
deleteEJB dl=home.create();
out.println("You have done home.create()");
dl.deleteRecord(ssn);
out.println("You have done deleteRecord()");
}
catch(Exception e){out.println("call EJB wrong");
e.printStackTrace();}
out.println("You have deleted the record:"+str);
}
else
out.println("Parameter is null");


}
}

執行後,Servlet頁面顯示:
You have done home.create() You have done deleteRecord() You have deleted the record:1好像沒有錯
但是WebLogic的DOS介面中顯示:
Open success!
wrong
java.lang.NullPointerException
at MydeleteEJB.deleteEJBBean.deleteRecord(deleteEJBBean.java:30)
at ydeleteEJB.deleteEJBBean_5xkxu6_EOImpl.deleteRecord(deleteEJBBean_5x
kxu6_EOImpl.java:36)
at MydeleteEJB.deleteEJBBean_5xkxu6_EOImpl_WLSkel.invoke(Unknown Source)

at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:288)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:257)
at weblogic.rmi.internal.BasicServerRef.dispatch(BasicServerRef.java:158)
at weblogic.rmi.internal.ServerRequest.sendOneWayRaw(ServerRequest.java:92)
at weblogic.rmi.internal.ServerRequest.sendReceive(ServerRequest.java:112)
at weblogic.rmi.internal.BasicRemoteRef.invoke(BasicRemoteRef.java:133)
at weblogic.rmi.internal.ProxyStub.invoke(ProxyStub.java:35)
at $Proxy89.deleteRecord(Unknown Source)
at deleteServlet.doPost(deleteServlet.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:263)
at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSe
rvletContext.java:2390)
at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestIm
pl.java:1959)
at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:137)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)

這樣的話,就是我EJB中的Statement smt=con.createStatement();一句出錯了。但我看了很久,也不知道為什麼這裡會出錯,我這些語句我以前寫在一般的java程式中都是正確的呀。
希望大家幫我看看,到底哪裡出錯了。謝謝!

相關文章