主题:配置JNDI数据源
数据库: MSSQL 服务器: Tomcat
META-INF下的context.xml配置文件:
Copy code
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sa" password="" driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://localhost/pubs"
/>
</Context>
WEB-INF下的web.xml文件:
Copy code
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="[url=http://java.sun.com/xml/ns/j2ee]http://java.sun.com/xml/ns/j2ee[/url]"
xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance]http://www.w3.org/2001/XMLSchema-instance[/url]"
xsi:schemaLocation="[url=http://java.sun.com/xml/ns/j2ee]http://java.sun.com/xml/ns/j2ee[/url]
[url=http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]"
id="Test4">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>Database connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
其实的我的数据源配置是没有问题的,问题在于如果我写个Jsp调用连接池,取连接一切正常.
但是如果写一个Java类想测试下就完了,总报异常.
如下:
jsp:
Copy code
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*,com.DBConnection" %>
<%
try
{
//初始化查找命名空间
Context ctx = new InitialContext();
//InitialContext ctx = new InitialContext();亦可
//找到DataSource,对名称进行定位java:comp/env是必须加的,后面跟你的DataSource名
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
//取出连接
Connection conn = ds.getConnection();
System.out.println("connection pool connected !!");
} catch (NamingException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
e.printStackTrace();
}finally
{
//注意不是关闭,是放回连接池.
conn.close();
}
}
%>
用这个Jsp测试正常,也可以取数据,没问题.
但是如果我写个测试类,如下:
Copy code
package com;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
Connection conn = ds.getConnection();
conn.close();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
异常如下:
Copy code
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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.Test.main(Test.java:18)
我就不明白了,其实是一样的代码,放在jsp里没有任何问题(已经测试多个正常),
放在main方法里就异常.
JDBC就没有这个问题,难道连接池不允许在Java类中使用?
META-INF下的context.xml配置文件:
Copy code
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="sa" password="" driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://localhost/pubs"
/>
</Context>
WEB-INF下的web.xml文件:
Copy code
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="[url=http://java.sun.com/xml/ns/j2ee]http://java.sun.com/xml/ns/j2ee[/url]"
xmlns:xsi="[url=http://www.w3.org/2001/XMLSchema-instance]http://www.w3.org/2001/XMLSchema-instance[/url]"
xsi:schemaLocation="[url=http://java.sun.com/xml/ns/j2ee]http://java.sun.com/xml/ns/j2ee[/url]
[url=http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd]http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd[/url]"
id="Test4">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>Database connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
其实的我的数据源配置是没有问题的,问题在于如果我写个Jsp调用连接池,取连接一切正常.
但是如果写一个Java类想测试下就完了,总报异常.
如下:
jsp:
Copy code
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*,com.DBConnection" %>
<%
try
{
//初始化查找命名空间
Context ctx = new InitialContext();
//InitialContext ctx = new InitialContext();亦可
//找到DataSource,对名称进行定位java:comp/env是必须加的,后面跟你的DataSource名
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
//取出连接
Connection conn = ds.getConnection();
System.out.println("connection pool connected !!");
} catch (NamingException e) {
System.out.println(e.getMessage());
} catch (SQLException e) {
e.printStackTrace();
}finally
{
//注意不是关闭,是放回连接池.
conn.close();
}
}
%>
用这个Jsp测试正常,也可以取数据,没问题.
但是如果我写个测试类,如下:
Copy code
package com;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
try {
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
Connection conn = ds.getConnection();
conn.close();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
异常如下:
Copy code
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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.Test.main(Test.java:18)
我就不明白了,其实是一样的代码,放在jsp里没有任何问题(已经测试多个正常),
放在main方法里就异常.
JDBC就没有这个问题,难道连接池不允许在Java类中使用?