tomcat6.0
server.xml
<Context path="/" docBase="InvoicingMS" debug="0" reloadable="true" >
<Resource name="jdbc/invoJndi"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs"
username="sa"
password="2008"
maxIdle="30"
maxWait="10000"
/>
</Context>catalina->localhost下的InvoicingMS.xml<Context path="/" docBase="InvoicingMS" debug="0" reloadable="true">
<Resource name="jdbc/invoJndi"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs"
username="sa"
password="2008"
maxIdle="30"
maxWait="10000"
/><ResourceLink name="jdbc/invoJndi" global="jdbc/invoJndi" type="javax.sql.DataSource"/> web-info web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/invoJndi</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>测试类package com.lv.db;import java.sql.Connection;import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;/**
* @author lvwenqiang
* 获取数据源
*/
public class DbUtils {
private static InitialContext ctx = null;
private static DataSource dataSource = null;
private static Connection conn=null;
static{
try {
try {
ctx = new InitialContext();
} catch (NamingException e) {
e.printStackTrace();
}
try {
dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/invoJndi");
} catch (NamingException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
static Connection getConn(){
try{
conn=dataSource.getConnection();
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
static void close(){
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
conn=null;
}
}
public static void main(String[] args){
getConn();
}
}
报错信息
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
server.xml
<Context path="/" docBase="InvoicingMS" debug="0" reloadable="true" >
<Resource name="jdbc/invoJndi"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs"
username="sa"
password="2008"
maxIdle="30"
maxWait="10000"
/>
</Context>catalina->localhost下的InvoicingMS.xml<Context path="/" docBase="InvoicingMS" debug="0" reloadable="true">
<Resource name="jdbc/invoJndi"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs"
username="sa"
password="2008"
maxIdle="30"
maxWait="10000"
/><ResourceLink name="jdbc/invoJndi" global="jdbc/invoJndi" type="javax.sql.DataSource"/> web-info web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/invoJndi</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>测试类package com.lv.db;import java.sql.Connection;import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;/**
* @author lvwenqiang
* 获取数据源
*/
public class DbUtils {
private static InitialContext ctx = null;
private static DataSource dataSource = null;
private static Connection conn=null;
static{
try {
try {
ctx = new InitialContext();
} catch (NamingException e) {
e.printStackTrace();
}
try {
dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/invoJndi");
} catch (NamingException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
static Connection getConn(){
try{
conn=dataSource.getConnection();
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
static void close(){
if(conn!=null){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
conn=null;
}
}
public static void main(String[] args){
getConn();
}
}
报错信息
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
解决方案 »
- 请教一个用两台路由器构建跨网段网络的问题!
- Hibernate 一对多自动筛选配置
- ssh调用问题
- 关于Failed to obtain specified collection错误
- 请教:在spring+hibernate+struts框架中如何在程序中处理数据库错误
- javamail问题: 该段程序无法发送给Hotmail的用户
- hibernate中什么方法能返回查询到的结果数量?
- 请教JSP问题!
- 熟悉Struts1.1配置文件的进来拿分
- Nginx删除以后仍然生效
- Java 打包 成jar 文件后运行程序生成一个txt文件,但它不在jar文件中,而是和jar文件在同一级目录下
- 大学生想到大公司,需要学j2EE吗?还是只是学c语言或c++语言
如
Hashtable env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL,"t3://localhost:7001"); Context ctx = new InitialContext(env); from :http://zhidao.baidu.com/question/11727714.html
大家看下面这个例子,它就没用main函数
http://www.ieee.org.cn/dispbbs.asp?boardID=41&ID=48188有关这方面的一个很好的例子
http://www.javaeye.com/topic/245596谢谢楼上的七位