Tomcat, Java & SQL Server 2008 R2: Cannot create J

2020-03-31 09:28发布

问题:

I am using Tomcat and Java (through Eclipse) and SQL Server 2008 R2

I am getting

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of 
class '' for connect URL 'null'

I also tried deploying to Tomcat 7 and I got exactly the same error. (I made sure that in this case the sqljdbc4.jar was in the lib directory of Tomcat, rather than just in the WAR)

Can anyone help? Thanks!

My code is as follows:

Server.xml

<GlobalNamingResources>
<Resource type="javax.sql.DataSource" auth="Container"
        name="jdbc/charmDB"
        factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        url="jdbc:sqlserver://localhost:1433;databaseName=CH;
        integratedSecurity=true"
        />

Web.xml

<resource-ref>
  <description>CH Database</description>
  <res-ref-name>jdbc/charmDB</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref> 

Java code

InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/charmDB");
Connection con = ds.getConnection();

回答1:

Resources declared in the the GlobalNamingResources will not be visable by your web application unless you link them to the application's context.

Otherwise, you could define the resource in the default context or the web application's context.



回答2:

Ive tried the above. Just wanted to add also. You can add in server.xml also, but ensure you are not using Resource-Params. Put the parameters in the Resource Element only. If you are on linux then ensure sqlodbc4.jar is placed in tomcat/lib found under /usr/share folder.