图3 |
图4 |
2、我的这个SessionBean要实现客户端程序和数据库的接口。也就是说所有的SQL语句可以通过这个SessionBean来执行并返回处理结果。
首先定义了一个SQL语句类的接口
package hello;
public interface DefaultSQLContext { public String getSQL(); public String[] getParameterType(); public int getType(); } |
|
所有的封装了SQL语句的类都必须要实现该接口。
例如:hello.TestSQL.java
package hello; public class TestSQL implements DefaultSQLContext{ static String[] paraValue; public String getSQL(){ return "select baseitemid,datasource,baseitemname,maxlength,description from ctm_baseitem where businesstypeid=?"; } public String[] getParameterType(){ return new String[]{"varchar"}; } //执行SQL的动作类型,是查询还是更新语句; //0是查询语句,1表示更新语句; public int getType(){ return 0; } } |
|
EJB的程序如下所示:
hello.HelloWord.java文件如下所示:
package hello; import java.rmi.*; import javax.ejb.*; import java.sql.*; import java.util.Vector; public interface HelloWorld extends EJBObject { public void execute() throws RemoteException; public Vector getQueryResult() throws RemoteException; } |
提供了执行SQL语句的execute()方法,getQueryResult()方法获得查询获得值。
Hello. HelloWorldBean文件如下所示,真正的执行程序:
package hello;
import java.rmi.*; import javax.ejb.*; import javax.sql.*; import javax.naming.*; import java.util.*; import java.sql.*;
public class HelloWorldBean implements SessionBean { private SessionContext sessionContext; private defaultSQLContext sql; private String sqlContext = ""; private Connection con = null; private Context initCtx = null; private Vector vResult,vColumnName; private int iReturn = 1; String[] paraValue;
public void init(){ try{ initCtx= new InitialContext(); DataSource ds = (javax.sql.DataSource) initCtx.lookup("DBSource"); con= ds.getConnection(); System.out.println("connection = "+con); }catch(Exception e){System.out.println(e);} } /* //设置SQLContext的类名; public void setSqlContext(String str){ sqlContext = str; } */ private void initSQLContext(){
init();
try{ Class aclass= Class.forName(sqlContext); sql = (defaultSQLContext)aclass.newInstance(); }catch (Exception e){ System.out.println("cannot initialize instance"+sqlContext); System.out.println(e); } }
/* public void setParaValue(String[] str){ this.paraValue = str; } */
public void execute(){ String[] paraType; int iColumnCount = 0; ResultSet rs = null; ResultSetMetaData rsm= null; Vector vInnerValue; //首先获得SQLContext; initSQLContext();
try{ PreparedStatement ps = con.prepareStatement(sql.getSQL()); System.out.println("SQL = "+sql.getSQL()); if(sql.getParameterType()!=null&¶Value!=null) { paraType = sql.getParameterType(); for(int i=0;i { if(paraType[i].equalsIgnoreCase("varchar")||paraType[i].equalsIgnoreCase("varchar2")) ps.setString(i+1,paraValue[i].trim()); else if(paraType[i].equalsIgnoreCase("integer")||paraType[i].equalsIgnoreCase("int")) ps.setInt(i+1,Integer.parseInt(paraValue[i].trim())); else if(paraType[i].equalsIgnoreCase("long")) ps.setLong(i+1,Long.parseLong(paraValue[i].trim())); else if(paraType[i].equalsIgnoreCase("date")) ps.setDate(i+1,java.sql.Date.valueOf(paraValue[i].trim())); else if(paraType[i].equalsIgnoreCase("boolean")) ps.setBoolean(i+1,Boolean.getBoolean(paraValue[i].trim())); else if(paraType[i].equalsIgnoreCase("double")) ps.setDouble(i+1,Double.parseDouble(paraValue[i].trim())); else ps.setString(i+1,paraValue[i].trim()); System.out.print("ParameterType["+i+"] = "+paraType[i]); System.out.println(" ParameterValue["+i+"] = "+paraValue[i]); }//end for }//end if
if(sql.getType()==0) //查询; { vResult = new Vector(); System.out.println("execute Query!"); rs = ps.executeQuery(); rsm= rs.getMetaData(); iColumnCount= rsm.getColumnCount(); while(rs.next()){ vInnerValue= new Vector(); for(int j=1;j<=iColumnCount;j++) { Object o = rs.getObject(j); if(o==null) vInnerValue.addElement(""); else vInnerValue.addElement(o.toString()); }//end for vResult.addElement(vInnerValue); }//end while vColumnName = new Vector(); //获得字段名; for(int i=1;i<=iColumnCount;i++) vColumnName.addElement(rsm.getColumnName(i)); }//end if 查询; else if(sql.getType()==1) //更新语句; { try{ System.out.println("execute Update!"); iReturn = ps.executeUpdate(); con.commit(); }catch(Exception ex) { iReturn=0; con.rollback(); System.out.println("update ejb error!"+ex); } } ps.close(); }catch (Exception ne){ System.out.println(ne); } }
public Vector getQueryResult(){ return vResult; }
public Vector getColumnName(){ return vColumnName; }
public int getUpdateResult(){ return iReturn; } /* public void ejbCreate(){ System.out.println("ejbCreate()"); }
public void ejbCreate(String sqlContext){ ejbCreate(sqlContext,null); System.out.println("ejbCreate()"); } */ public void ejbCreate(String sqlContext,String[] paraValue) { this.sqlContext = sqlContext; this.paraValue = paraValue; System.out.println("ejbCreate()"); }
public void ejbRemove() { } public void ejbActivate() { } public void ejbPassivate() { }
public void setSessionContext(SessionContext context) { sessionContext = context; }
}
hello. HelloWorldHome文件(定义Home接口)如下所示: package hello; import java.rmi.*; import javax.ejb.*;
public interface HelloWorldHome extends EJBHome{ public HelloWorld create(String sqlContext,String[] paraValue) throws RemoteException, CreateException; }
客户端测试程序: package hello; import javax.naming.*; import javax.rmi.PortableRemoteObject; import java.util.*; import java.sql.*;
public class HelloWorldBeanClient1 { private static HelloWorldHome helloWorldHome = null; private static HelloWorld hello = null;
static{ Context ctx = null; Hashtable ht = new Hashtable(); System.out.println("Initializing bean access."); try{ ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); ht.put(Context.PROVIDER_URL,"t3://localhost:7001"); ctx = new InitialContext(ht); //look up jndi name Object ref = ctx.lookup("testBean"); //cast to Home interface helloWorldHome = (HelloWorldHome) PortableRemoteObject.narrow(ref, HelloWorldHome.class); System.out.println("Get the ejbHome."); String[] paraValue = new String[]{"16"}; hello=helloWorldHome.create("hello.testSQL",paraValue); }catch(Exception e){System.out.println("cannot get the ejbBean!"+e);} }
public HelloWorldBeanClient1() { Vector vInnerValue; try { //hello.setParaValue(value); //hello.setSqlContext("hello.testSQL"); hello.execute(); Vector columnName = hello.getColumnName(); Vector result = hello.getQueryResult(); for(int i=0;i { vInnerValue = (Vector)result.elementAt(i); for(int j=0;j { System.out.print(columnName.elementAt(j)+" = "); System.out.println(vInnerValue.elementAt(j)); } }
//System.out.println("execute Result = "+hello.getUpdateResult()); }catch(Exception e) { System.out.println("Failed initializing bean access."); e.printStackTrace(); } }
/**Main method*/ public static void main(String[] args) { HelloWorldBeanClient1 client = new HelloWorldBeanClient1(); } } } |
|
 
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
2/2 首页 上一页 1 2 |