我在自己项目下的META-INF下自己建了一个context.xml。如下:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:QUAN" username="conqueror"
password="quan" maxActive="20" maxIdle="10" maxWait="5000" />
</Context>然后在自己项目下的WEB-INF中的web.xml里加了代码如下:
<resource-ref>
<description>BBS DB Connection</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>然后我自己写了一个连接工厂类。其中连接方法如下:
public static Connection getConnection() throws NamingException,
SQLException {
Context context = new InitialContext();
if(context == null){
System.out.println("saaaaaaaaaaaaaaaaa");
}else{
System.out.println("wwwwwwwwwwwwww");
}
Context envContext = (Context)context.lookup("java:comp/env");
if(envContext == null){
System.out.println("envContext");
}else{
System.out.println("envContextqqqqqqqqqqqqqqqq");
}
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
return ds.getConnection();
}然后测试就报错了!错误如下:
Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)我测试了一下就是执行到Context envContext = (Context)context.lookup("java:comp/env");这句时报的错!
哪位高人给小弟解决一下啊!万分感谢!
<?xml version='1.0' encoding='utf-8'?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:QUAN" username="conqueror"
password="quan" maxActive="20" maxIdle="10" maxWait="5000" />
</Context>然后在自己项目下的WEB-INF中的web.xml里加了代码如下:
<resource-ref>
<description>BBS DB Connection</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>然后我自己写了一个连接工厂类。其中连接方法如下:
public static Connection getConnection() throws NamingException,
SQLException {
Context context = new InitialContext();
if(context == null){
System.out.println("saaaaaaaaaaaaaaaaa");
}else{
System.out.println("wwwwwwwwwwwwww");
}
Context envContext = (Context)context.lookup("java:comp/env");
if(envContext == null){
System.out.println("envContext");
}else{
System.out.println("envContextqqqqqqqqqqqqqqqq");
}
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
return ds.getConnection();
}然后测试就报错了!错误如下:
Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)我测试了一下就是执行到Context envContext = (Context)context.lookup("java:comp/env");这句时报的错!
哪位高人给小弟解决一下啊!万分感谢!
解决方案 »
- 写入数据库的时候自增的数据怎么办
- 不用JS如何取得页面上某个TR里的TD的值?就是某行某列的值。
- IClass classinfo = (IClass)listclass.elementAt(i),为什么前面要加(IClass)?
- Microsoft Internet Explorer 5.0版本以后有XML数据岛的概念 JSP中怎么好好的利用呢?
- 感谢tangbow给的答案,特此给分20,不好意思....分不多.....
- 裝了jb﹐是否還要安裝tomcat
- 什么工具编写JSP、调试能报出源程序第几条出错?立等。。。。。。
- 点连SQLServer啊!好烦啊,请高手帮忙。
- 如何使用Servlet访问服务器上的文件夹?
- springboot定时器,如何停止?
- struts 2 问题~~~~~~~~~~急急急急急急急急急急!!!!!!!!!!!!!!
- 做一个BBS论坛用什么语言开发好?
DataSource ds = (DataSource)context.lookup("jdbc/myoracle");
这样写应该没问题; 或者是(不知道这样写是否可以,试试看吧)
public static Connection getConnection() throws NamingException,
SQLException {
Context context = new InitialContext();
if(context == null){
System.out.println("saaaaaaaaaaaaaaaaa");
}else{
System.out.println("wwwwwwwwwwwwww");
}
Context envContext = (Context)context.lookup("java:comp/env/");
if(envContext == null){
System.out.println("envContext");
}else{
System.out.println("envContextqqqqqqqqqqqqqqqq");
}
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
return ds.getConnection();
}