百度空间 | 百度首页 
 
查看文章
 
(引)java.naming.factory.initial
2009-06-28 22:48
修改Tomcat 5.5\conf\context.xml文件
<Resource
name="jdbc/DBPool"
auth="Container"
type="com.microsoft.jdbc.sqlserver.SQLServerDriver"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2" maxWait="5000" maxActive="4"
username="sa" password="sa"
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=mytest"/>


修改java文件
package com.Dbtest;

import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class Dbtest {    
public static void main (String arg[]){
/* 通过JNDI从部署在应用服务器中的数据库连接池中获取数据库连接对象 */
Context env = null;
DataSource ds = null;
Connection conn = null;
try{
System.out.println (System.getProperty("java.naming.factory.initial"));
env = (Context) new InitialContext().lookup("java:comp/env");
ds = (DataSource) env.lookup("jdbc/DBPool");
conn = ds.getConnection();
if (conn != null) System.out.println("connection link successful");
}catch(Exception e){
e.printStackTrace();
}
}
}

用“Run As -- Java Application”运行,出错:
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

以Tomcat应用服务器为例,在Tomcat启动的时候会向系统属性中添加一个属性(默认的web应用):  
System.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY,"org.apache.naming.java.javaURLContextFactory");  
如果有了这个javax.naming.Context.INITIAL_CONTEXT_FACTORY项,在new   InitalContext()的时候就不会报出楼主的错误。  

从异常信息:Need to specify class name in environment or system property, or as an applet parameter,or in an application resource file中可以看出,传递javax.naming.Context.INITIAL_CONTEXT_FACTORY参数的值的方式。
确认一下以下问题:
1.目前你的JVM属性中有没有java.naming.factory.initial一项属性,System.getProperty("java.naming.factory.initial"),如果为null,那么你在这个web应用的jsp页面中肯定也无法获取到正确的InitialContext  
System.getProperty("java.naming.factory.initial")这句话是试试javax.naming.factory.initial属性到底是不是空,因为类中缺少了这个属性的定义,所以在才不能运行成功~!  
但把这个类文件做成一个bean通过jsp调用就可以了,就是因为tomcat肯定对这个东西做了定义~!所以JSP页才不报错.  
总之在类中使用tomcat数据源,就存在这个问题~!
把连接数据库部分写到bean中,在JSP布中调用也没问题,就是加个main函数单独调试bean的时候出现错误~!
原因:连接池的实现是tomcat本身的dbcp,加以个main 就不属于tomcat的了,如果要用的话,可以自己写一个,只要是tomcat环境应用程序,你的调用就没有问题


解决方法:
修改为jsp文件,确实可以运行了。
修改为:
<%@ page import ="java.sql.*"%>
<%@ page import = "javax.sql.*"%>
<%@ page import ="javax.naming.*"%>

<html>
<head>
<title></title>
</head>
<body>
<%
DataSource ds = null;
Context env = null;
Connection conn = null;
try {
System.out.println (System.getProperty("java.naming.factory.initial"));
env = (Context) new InitialContext().lookup("java:comp/env");
ds = (DataSource) env.lookup("jdbc/DBPool");
conn = ds.getConnection();
if (conn != null) System.out.println("connection link successful");
}catch (Exception e){
e.printStackTrace();
}
%>


</body>
</html>

context.xml的内容为 :
<Resource
name="jdbc/DBPool"
auth="Container"
type="com.microsoft.jdbc.sqlserver.SQLServerDriver"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2" maxWait="5000" maxActive="4"
username="sa" password="sa"
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=mytest"/>

参考:
http://topic.csdn.net/t/20050621/14/4096613.html
http://topic.csdn.net/t/20040325/13/2885081.html

类别:Java | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu