我正在嘗試使用Tomcat/JNDI讀取MS SQL Server數據庫表。我從上表沒有問題,看如果我手動硬編碼的數據源到我的Java源代碼,但是當我嘗試使用JNDI來加載數據源,我得到一個ClassNotFoundException:在Tomcat中加載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)
我不希望看到這樣的因爲我將微軟的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()的代碼):
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();
試試這個話題 http://stackoverflow.com/questions/8253958/classnotfoundexception-when-using-custom-sslsocketfactory/31690328#31690328 –