JNDI获得数据源

javadata 2007-01-06 03:39:59
我在 JB下边
Driver:
weblogic.jdbc.mssqlserver4.Driver
URL:
jdbc:weblogic:mssqlserver4:News@localhost:1433
Database name:
weblogic:mssqlserver4:News@localhost:1433
JNDI name:
DataSource
实现了数据库的连接(weblogic的数据源 是不?)

现在我引用这个数据源
导入了这些包:
import java.sql.*;
import javax.naming.Context;
import javax.naming.InitialContext;
获得数据源的程序如下:
public Connection getConnection() throws Exception {
try {
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("DataSource");
return ds.getConnection();
} catch (Exception e) {
System.out.println("不能获取数据源");
e.printStackTrace();
return null;
}
}

可我在调用实体BEAN进行数据库的插入时 老是说 找不到数据源

麻烦给小弟指点一下
...全文
640 7 打赏 收藏 举报
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
javadata 2007-02-05
  • 打赏
  • 举报
回复
搞定
javadata 2007-01-15
  • 打赏
  • 举报
回复
好了上边的问题也解决了
可为什么
我插入数据库老是空呢
我用他进行了数据的插入
try {
System.out.println("ejbCreate()方法被调用");
con = getConnection();
String sqlStr = "insert into UserIfometion(UserName,Userpwd,"
+ "RealName,sex,UserItrduce)"
+ " values (?,?,?,?,?)";
pstmt = con.prepareStatement(sqlStr);
pstmt.setString(1, username);
pstmt.setString(2, userpwd);
pstmt.setString(3, realname);
pstmt.setString(4, sex);
pstmt.setString(5, useritrduce);
System.out.println(sqlStr);
System.out.println("888888888888888888888888888888888888844哦");
pstmt.executeUpdate();
pstmt.execute();
System.out.println(username);
System.out.println(userpwd);
System.out.println(realname);
System.out.println(sex);
System.out.println(useritrduce);

System.out.println("dhhhhhhhhhhhhhhhhhddddddddddd哦");

return null;
} catch (Exception e) {
throw new CreateException(e.toString());
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
下边是测试端的
try{
home.create("杨涌旗","66113576","栀子花开","男","TNND");

}catch(Exception e){}

我能显示 测试端的值 但是 插不进去
可我有个自增的字段他的值却是在变化的
TNND
javadata 2007-01-10
  • 打赏
  • 举报
回复
还没呢
JB里边
才学不知道咋个整 别笑哈 呵呵
Daem0n(瓶中的精灵) 给指点一下嘛
Daem0n 2007-01-10
  • 打赏
  • 举报
回复
你在配置文件里面配置数据源了没有
javadata 2007-01-09
  • 打赏
  • 举报
回复
xia现在又报这个错
我改成了
Context env = (Context) new InitialContext().lookup("java:comp/env");
DataSource source = (DataSource) env.lookup("DataSource");//这个地方要和conf里面的名称相统一

Daem0n(瓶中的精灵) 说的lookup("xxxx")里边是不是写数据源呢 我写了他还报下边那错
C:\bea\jdk150_03\bin\javaw -classpath "F:\EJB\regrter\classes;C:\bea\weblogic90\server\lib\weblogic_sp.jar;C:\bea\weblogic90\server\lib\weblogic.jar;C:\bea\weblogic90\server\lib\webservices.jar;C:\bea\weblogic90\javelin\lib\javelinx.jar;C:\bea\weblogic90\common\lib\log4j.jar;C:\Borland\JBuilder2006\thirdparty\jakarta-struts-1.2-lib\antlr.jar;C:\Borland\JBuilder2006\thirdparty\jakarta-struts-1.2-lib\commons-beanutils.jar;C:\Borland\JBuilder2006\thirdparty\jakarta-struts-1.2-lib\commons-collections.jar;C:\Borland\JBuilder2006\thirdparty\jakarta-struts-1.2-lib\commons-digester.jar;C:\Borland\JBuilder2006\thirdparty\jakarta-struts-1.2-lib\commons-fileupload.jar;C:\Borland\JBuilder2006\thirdparty\jakarta-struts-1.2-lib\commons-logging.jar;C:\Borland\JBuilder2006\thirdparty\jakarta-struts-1.2-lib\commons-validator.jar;C:\Borland\JBuilder2006\thirdparty\jakarta-struts-1.2-lib\jakarta-oro.jar;C:\Borland\JBuilder2006\thirdparty\jakarta-struts-1.2-lib\struts.jar;F:\Y2\J2EE\driver\mssqlserver4v65.jar;C:\bea\jdk150_03\lib\tools.jar;C:\bea\jdk150_03\lib\dt.jar;C:\bea\jdk150_03\lib\htmlconverter.jar;C:\bea\jdk150_03\lib\jconsole.jar;C:\bea\jdk150_03\sample\jnlp\servlet\jnlp.jar;C:\bea\jdk150_03\sample\jnlp\servlet\jnlp-servlet.jar;C:\bea\jdk150_03\sample\jnlp\servlet\jardiff.jar;C:\bea\jdk150_03\jre\lib\jsse.jar;C:\bea\jdk150_03\jre\lib\im\indicim.jar;C:\bea\jdk150_03\jre\lib\im\thaiim.jar;C:\bea\jdk150_03\jre\lib\deploy.jar;C:\bea\jdk150_03\jre\lib\jce.jar;C:\bea\jdk150_03\jre\lib\ext\localedata.jar;C:\bea\jdk150_03\jre\lib\ext\dnsns.jar;C:\bea\jdk150_03\jre\lib\ext\sunjce_provider.jar;C:\bea\jdk150_03\jre\lib\ext\sunpkcs11.jar;C:\bea\jdk150_03\jre\lib\charsets.jar;C:\bea\jdk150_03\jre\lib\rt.jar;C:\bea\jdk150_03\jre\lib\plugin.jar" regrter.UserInfoTestClient1
javax.ejb.CreateException: java.lang.NullPointerException
at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:191)
at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:315)
at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:250)
at regrter.UserInfo_1571he_HomeImpl_900_WLStub.create(Unknown Source)
at regrter.UserInfoTestClient1.main(UserInfoTestClient1.java:211)
Caused by: javax.ejb.CreateException: java.lang.NullPointerException
at regrter.UserInfoBean.ejbCreate(UserInfoBean.java:49)
at regrter.UserInfo_1571he_Impl.ejbCreate(UserInfo_1571he_Impl.java:263)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at weblogic.ejb20.manager.DBManager.create(DBManager.java:1371)
at weblogic.ejb20.manager.DBManager.remoteCreate(DBManager.java:1319)
at weblogic.ejb20.internal.EntityEJBHome.create(EntityEJBHome.java:269)
at regrter.UserInfo_1571he_HomeImpl.create(UserInfo_1571he_HomeImpl.java:80)
at regrter.UserInfo_1571he_HomeImpl_WLSkel.invoke(Unknown Source)
at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:503)
at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:224)
at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:393)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:389)
at weblogic.rmi.internal.BasicServerRef.access$300(BasicServerRef.java:56)
at weblogic.rmi.internal.BasicServerRef$BasicExecuteRequest.run(BasicServerRef.java:903)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:179)
Daem0n 2007-01-09
  • 打赏
  • 举报
回复
Context env = (Context) new InitialContext().lookup("java:comp/env");
DataSource source = (DataSource) env.lookup("xxxx");//这个地方要和conf里面的名称相统一
javadata 2007-01-06
  • 打赏
  • 举报
回复
老报这个错: 是为什么啊 不知道我是那没配置对 还是别的呢?
javax.naming.NameNotFoundException: Unable to resolve 'Employee'. Resolved '' [Root exception is javax.naming.NameNotFoundException: Unable to resolve 'Employee'. Resolved '']; remaining name 'Employee'
at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:191)
at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:315)
at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:250)
at weblogic.jndi.internal.ServerNamingNode_900_WLStub.lookup(Unknown Source)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:374)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:362)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at empbmpapp.EmployeeTestClient1.initialize(EmployeeTestClient1.java:55)
at empbmpapp.EmployeeTestClient1.<init>(EmployeeTestClient1.java:43)
at empbmpapp.EmployeeTestClient1.main(EmployeeTestClient1.java:353)
Caused by: javax.naming.NameNotFoundException: Unable to resolve 'Employee'. Resolved ''
at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1123)
at weblogic.jndi.internal.BasicNamingNode.lookupHere(BasicNamingNode.java:250)
at weblogic.jndi.internal.ServerNamingNode.lookupHere(ServerNamingNode.java:171)
at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:204)
at weblogic.jndi.internal.RootNamingNode_WLSkel.invoke(Unknown Source)
at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:503)
at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:224)
at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:393)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:389)
at weblogic.rmi.internal.BasicServerRef.access$300(BasicServerRef.java:56)
at weblogic.rmi.internal.BasicServerRef$BasicExecuteRequest.run(BasicServerRef.java:903)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:207)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:179)
java.lang.NullPointerException
at empbmpapp.EmployeeTestClient1.main(EmployeeTestClient1.java:356)
发帖
Java EE

6.7w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
帖子事件
创建了帖子
2007-01-06 03:39
社区公告
暂无公告