[英]JNDI not to find DataSource from SQLServer
我有一个MS SQLServer 2008,数据库名为:“ conpool”。 并在那里创建了一个具有id,fullName和age的表。 现在,我已在Java中的Netbeans 7.3和Tomcat 7.0.30中实现了一个连接池,该池在每次与数据库的连接时都不会绑定,并且我可以对数据库进行查询。
我的连接池:
// ------------------------开始JDBC连接池-------------------- --------------
public static void main(String[] args) throws SQLException, NamingException {
// Get DataSource
Context initialContext = new InitialContext();
System.out.println("Test1: a object from InitialContext has been created");
Context context = (Context) initialContext.lookup("java:/comp/env");
System.out.println("Test2: Context lookup was OKAY");
//The JDBC Data source that was created in SQLServer
DataSource datasource = (DataSource) context.lookup("jdbc/conpool");
System.out.println("Test3: DataSource lookup with JNDI was successfully");
System.out.println("JDBC Connecction Pool is created successfully");
//-------------------------Ende JDBC Connection Pool--------------------------------------
//Using a connection from the pool
Connection connection = datasource.getConnection();
System.out.println("Test4: Datasource Connection was successfully");
if (connection == null) {
throw new SQLException("Error establishing connection!");
//System.out.println("DBConnection Failed!!! ");
}
//Using the connection to access the database
//--------------------------------Beginn der Queryprocess------------------------------------------
String query = "SELECT * FROM conpool.dbo.Personen";
PreparedStatement statement = connection.prepareStatement(query);
ResultSet rs = statement.executeQuery();
System.out.println("Test5: All Object for a single Query are created");
while (rs.next()) {
System.out.println(rs.getString(query));
}
//Connection will be closed
connection.close();
//-------------------------------Ende der Queryprocess--------------------------------------
}
我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>Tomcat-ConnectionPooling with MS SQLServer</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/conpool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
我的context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/ITWService">
<!-- Specify a JDBC datasource -->
<Resource name="jdbc/conpool" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="dbo" password="" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=conpool"/>
</Context>
现在,我得到了这种错误:
跑:
线程“主”中的异常javax.naming.NoInitialContextException:需要在环境或系统属性中或作为applet参数或在应用程序资源文件中指定类名称:javax.naming.spi上的java.naming.factory.initial。在javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)处的NamingManager.getInitialContext(NamingManager.java:645)在javax.naming.InitialContext.lookup(InitialContext.java:325)处的javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325) .java:392),位于com.akapoor.itwservice.server.RestServer.main(RestServer.java:136)Java结果:1成功构建(总时间:1秒)
请帮助我解决我的问题。 我会非常感谢你,我很困惑。 我尝试了一切,但没有帮助..请告诉我我的问题是为什么我没有连接到数据库
您需要处于托管环境(如应用程序服务器)中,才能创建不带参数的InitialContext。
由于您发布的代码具有主要方法,因此我假设您已将代码作为独立应用程序执行。 但是在您的问题中,您写道您的应用程序在Tomcat上运行,因此您需要将其部署到您的容器中并在其中调用它。
还是有一个为什么要远程访问JNDI的原因?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.