各位高手帮我看个问,急急!!!!!!!!!!!!!!!!!!!!!
我所有的配置都如网页所说配置的,也是按照网上的代码写的,包也发布了,可是运行NameTestClient1出现错误的提示?原因何在?
网上的文章步骤如下:
1、首先开发环境是Jbuilder6+weblogic6.1, 对于数据库,因为这里是测试,所以用的是sql server,如果用oracle,作相应的修改就可以了。
2、这里以一个容器管理实体bean为例,在jbuilder里面先建立一个工程(注意目录不能带空格),然后new-Enterprise里面选ejb1.x entity bean modeler,然后new一个ejb模块,我这里取名contain,,其他默认,点ok,然后next,下面就是连接数据库的一些设置了!
driver就是数据库驱动程序,下拉框可以自己选,oracle,sqlserver的,这里用sqlserver,选jdbc-odbc桥:sun.jdbc.odbc.JdbcOdbcDriver,URL: jdbc:odbc:finance (finance是数据源,事先应该配好,这里就不说了),然后是username和password, JNDI name写上finance,然后next,jbuilder开始连接数据,如果连接成功,会把数据库当中的表显示出来,我们这里在数据里面只建了一个只有一个字段的表name,字段名name,varchar型的。选上name,加到selected里面,NEXT,在NEXT,在出来的画面里面给BEAN选择主健,然后一路NEXT一直到FINISH,这个时候JBUILDER就已经把本地,远程和实体BEAN的文件给你健好了。
3、三个文件名字分别为:
Name.java 远程接口
NameBean.java 实体Bean
NameHome.java 本地接口
下面三个文件的代码如下:
Name.java
import java.rmi.*;
import javax.ejb.*;
public interface Name extends EJBObject {
public String getName() throws RemoteException;
public void setName(String name) throws RemoteException;
}
NameHome.java
import java.rmi.*;
import javax.ejb.*;
public interface NameHome extends EJBHome {
public Name create(String name) throws RemoteException, CreateException;
public Name findByPrimaryKey(String primaryKey) throws RemoteException, FinderException;
}
NameBean.java
import java.rmi.*;
import javax.ejb.*;
import java.sql.*;
import java.util.*;
public class NameBean implements EntityBean {
EntityContext ctx;
public String name;
public String ejbCreate(String name) throws CreateException {
this.name = name;
return null;
}
public void ejbPostCreate(String name) throws CreateException {
}
public void ejbLoad() {
}
public void ejbStore() {
}
public void ejbRemove() throws RemoveException {
}
public void ejbActivate() {
}
public void ejbPassivate() {
}
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
}
public void unsetEntityContext() {
ctx = null;
}
public String getName() throws RemoteException {
return name;
}
public void setName(String name) throws RemoteException {
this.name = name;
}
}
写完以后调试一下整个工程,看看有没有错误
4、到weblogic里面去配置连接池和数据源
打开weblogic的服务和控制台,点jdbc-conncetion pools,然后新建立一个连接池,
我的配置如下:
name:myConnectionPool
url:jdbc:odbc:finance
drive class:sun.jdbc.odbc.JdbcOdbcDriver
Properties:user=sa;password=
然后target里面选上myserver
然后连数据源jdbc-datasource,如下:
name:finance
jndi name: finance
pool name: myConnectionPool
然后target里面选上myserver
然后点ejb-选刚才我们建立的bean:contain,还是把target里面选上myserver
如果配置的时候碰到什么困难,察看一下weblogic的帮助,里面写得非常详细!
5、到jbuilder里面去写客户端代码
client.java
import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
public class NameTestClient1 {
private NameHome nameHome = null;
//Construct the EJB test client
public NameTestClient1() {
try {
//get naming context
Context ctx = getInitialContext();
//look up jndi name
Object ref = ctx.lookup("Name");
//cast to Home interface
nameHome = (NameHome) PortableRemoteObject.narrow(ref, NameHome.class);
nameHome.create("sssss");
}
catch(Exception e) {
e.printStackTrace();
}
}
private Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
}
return new InitialContext(properties);
}
catch(Exception e) {
System.out.println("Unable to connect to WebLogic server at " + url);
System.out.println("Please make sure that the server is running.");
throw e;
}
}
//----------------------------------------------------------------------------
// Utility Methods
//----------------------------------------------------------------------------
public NameHome getHome() {
return nameHome;
}
//Main method
public static void main(String[] args) {
NameTestClient1 client = new NameTestClient1();
// Use the getHome() method of the client object to call Home interface
// methods that will return a Remote interface reference. Then
// use that Remote interface reference to access the EJB.
}
}
我这里只写了一个create(“ssss”),执行以后看看数据库是不是在数据库name表里面增加一条ssss的记录了,那就成功了
*****************************************
我在jbulider6里运行NameTestClient1的时候错误如下:
C:\JBuilder6\jdk1.3.1\bin\javaw -classpath "C:\ejb\zhp11\classes;C:\bea\wlserver6.0\lib\weblogic_sp.jar;C:\bea\wlserver6.0\lib\weblogic.jar;C:\JBuilder6\jdk1.3.1\demo\jfc\Java2D\Java2Demo.jar;C:\JBuilder6\jdk1.3.1\jre\lib\i18n.jar;C:\JBuilder6\jdk1.3.1\jre\lib\jaws.jar;C:\JBuilder6\jdk1.3.1\jre\lib\rt.jar;C:\JBuilder6\jdk1.3.1\jre\lib\sunrsasign.jar;C:\JBuilder6\jdk1.3.1\lib\dt.jar;C:\JBuilder6\jdk1.3.1\lib\htmlconverter.jar;C:\JBuilder6\jdk1.3.1\lib\tools.jar" zhp11.NameTestClient1
-- Initializing bean access.
javax.naming.NameNotFoundException: Unable to resolve Name. Resolved: '' Unresolved:'Name' ; remaining name ''
at weblogic.rmi.internal.AbstractOutboundRequest.sendReceive(AbstractOutboundRequest.java:90)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteRef.java:247)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteRef.java:225)-- Failed initializing bean access.
at weblogic.jndi.internal.ServerNamingNode_WLStub.lookup(ServerNamingNode_WLStub.java:121)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:323)
at javax.naming.InitialContext.lookup(InitialContext.java:350)
at zhp11.NameTestClient1.<init>(NameTestClient1.java:27)
at zhp11.NameTestClient1.main(NameTestClient1.java:209)