SessionBean呼叫資料庫的問題
我想試驗一下用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程式中都是正確的呀。
希望大家幫我看看,到底哪裡出錯了。謝謝!
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程式中都是正確的呀。
希望大家幫我看看,到底哪裡出錯了。謝謝!
相關文章
- 一個關於SessionBean呼叫的問題。SessionBean
- [求助]..新手!遇到struts呼叫sessionbean問題?SessionBean
- 用sessionBean讀取文字資料的問題SessionBean
- 請教:EJB3的SessionBean主動呼叫問題SessionBean
- ???做了一個CMP, 但透過sessionbean呼叫資料時卻有個奇怪的問題??SessionBean
- jsp呼叫javabean封裝資料庫的問題,急!JSJavaBean封裝資料庫
- jsp呼叫javabean,javabean封裝資料庫的問題JSJavaBean封裝資料庫
- sessionbean +bmp取值出錯的問題SessionBean
- 初次使用無狀態sessionbean的問題SessionBean
- 關於jsp 呼叫bean 訪問sql-server資料庫問題JSBeanSQLServer資料庫
- jboss訪問資料庫的問題資料庫
- 資料庫寫入的問題資料庫
- 資料庫同步問題資料庫
- 不同資料庫間傳遞資料的問題資料庫
- Sybase ASE資料庫的license問題資料庫
- 資料庫常見問題資料庫
- 資料庫被鎖問題資料庫
- jbuilder 與資料庫問題UI資料庫
- 資料庫中文問題,急資料庫
- jbuilder資料庫中文問題UI資料庫
- 配置資料庫問題?求救資料庫
- 資料庫關聯問題資料庫
- 資料庫同步更新問題?資料庫
- 資料庫問題求證資料庫
- sybase iq 資料庫 問題資料庫
- 資料庫併發問題資料庫
- Domino 資料庫超大問題資料庫
- 資料庫欄位問題資料庫
- 資料庫啟動問題資料庫
- 資料庫sql的優化問題的面試題資料庫SQL優化面試題
- 安裝資料庫和資料庫解決問題資料庫
- Oracle資料庫中對BLOB資料的操作問題Oracle資料庫
- 解決hive資料庫 插入資料很慢的問題Hive資料庫
- 請問,關於資料庫連線的問題。資料庫
- Laravel5的資料庫表建立問題 資料庫遷移操作報錯問題解決Laravel資料庫
- 做資料庫分離讀寫時,sqlServer資料庫資料同步的問題:資料庫SQLServer
- 資料庫表的唯一索引問題資料庫索引
- 資料庫顯示圖片的問題資料庫