连接池问题 Need to specify class name in envir
<resource-ref>
<res-ref-name>news</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
运行:
public static void main(String[] args){
try {
Context ctx = new InitialContext();
Context envContext = (Context)ctx.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("news");
Connection conn = ds.getConnection();
conn.close();
System.out.print("连接成功");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
报错:
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)
at dao.ForumDao.main(ForumDao.java:33)用实例类进行测试时候遇到了上述问题,用Servlet或jsp页面却是成功的,没有错误。为什马呢?
浏览器访问test.jsp没问题,显示连接成功!
test.jsp:
<%@ page language="java" import="javax.naming.*,javax.sql.*,java.sql.*" pageEncoding="GBK"%>
<%
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/news");
Connection con = ds.getConnection();
out.print("连接成功");
%>
相关推荐
Introduction to iUMLite 2.20
menu. - Give your database a name and specify its location. - After saving, you must acquire a lock to
SCJP Sun® Certified Programmer for Java™ 6 Study Guide chapter 10
by the class name:```bashjava com.example.myapp.MainClass```If the class is not found, you might need
CGLIB2 is not available&org.objectweb.asm.Type
关于Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify
Introducing_WCF_in_NET_Framework_3_5英文版
The service class should be marked with the `[ServiceBehavior]` attribute to specify behavior attributes
INTRODUCTION TO PYTHON SCRIPTING FOR MAYA ARTISTS.
They specify the attributes and methods that objects of the class will have.- **Inheritance and Polymorphism


Web 开发

