我目前正在尝试使用Tomcat / JNDI读取MS SQL Server数据库表。如果我手动将数据源硬编码到我的Java源代码中,我可以毫无问题地从表中读取,但是当我尝试使用JNDI加载数据源时,我得到一个ClassNotFoundException:
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerConnectionPoolDataSource
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
TestServlet.doGet(TestServlet.java:53)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
我不希望看到这个,因为我将Microsoft的JDBC驱动程序(sqljdbc4.jar)存储在Tomcat的lib目录(而不是应用程序的WEB-INF / lib)中,因为当我将DataSource硬编码到Java中时它工作正常源代码。有没有人知道为什么会发生这种情况或知道在使用JNDI资源时必须指定的其他任何东西让Tomcat将lib目录添加到类路径中?
context.xml中:
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource name="jdbc/dspr" auth="Container" type="javax.sql.DataSource"
username="<USERNAME OMITTED>"
password="<PASSWORD OMITTED>"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerConnectionPoolDataSource"
url="<URL OMITTED>"
validationQuery="select 1"
/>
</Context>
TestServlet.java(doPost()code):
PrintWriter out = response.getWriter();
try {
// Servlet fails with a ClassNotFoundException when this block is
//used rather than the block below
InitialContext initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/dspr");
// =========
// Servlet works when this is uncommented and the above block is commented out
//SQLServerConnectionPoolDataSource ds = new SQLServerConnectionPoolDataSource();
//ds.setURL("<URL OMITTED>");
//ds.setUser("<DATABASE USER OMITTED>");
//ds.setPassword("<DATABASE PASSWORD OMITTED>");
// =========
Connection c = ds.getConnection();
Statement s = c.createStatement();
boolean good = s.execute("select * from DS.test_table1");
System.out.println("** QUERY GOOD? " + good);
ResultSet rs = s.getResultSet();
while (rs.next()) {
String col = rs.getString("col1");
String val = rs.getString("val1");
System.out.println(col + " | " + val);
}
c.close();
} catch (NamingException e) {
throw new ServletException(e);
} catch (SQLException e) {
throw new ServletException(e);
}
out.write("hello");
out.close();
答案 0 :(得分:0)
Tomcat包含Apache DBCP和Apache Pool,因此您只需提供驱动程序类名称。
<Resource name="jdbc/dspr" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="<USERNAME OMITTED>"
password="<PASSWORD OMITTED>"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="<URL OMITTED>" />
请参阅Configure Tomcat 6 DataSource using Sql Server 2005中的示例。以下怎么样?您可以将其注入到servlet中,而不是查找资源。见Connect to Datasource without resource-ref in web.xml
答案 1 :(得分:0)
事实证明,我使用com.microsoft.jdbc.sqlserver.SQLServerConnectionPoolDataSource
作为驱动程序类名。事实上,它是com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource
。阿卡 - PEBCAK。