Cannot create JDBC driver of class '' for connect URL 'null' : Tomcat & SQL Server JDBC driver

最后都变了- 提交于 2019-12-30 06:25:20

问题


I've tried just about everything I can find out there, if someone is able to help me out, I will be eternally grateful (and a lot more free in my time).

Basically, I have an error in Tomcat 7.0 (both when running within Eclipse and via startup.bat) that says this once data begins to be accessed by my dynamic web application:

Cannot create JDBC driver of class '' for connect URL 'null'
java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(JdbcOdbcDriver.java:507)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(JdbcOdbcDriver.java:476)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(JdbcOdbcDriver.java:307)

I have the sqljdbc4.jar file in my tomcat\lib directory. I have also tried putting this in my WEB-INF/lib, and even my JDK lib directories. I don't think sqljdbc.jar will work, as it is intended for older JDK/JRE installs than mine.

I've heard the context.xml and web.xml files are crucial in getting this to work.

web.xml snippet:

<resource-ref>
<description>LBI DB Connection</description>
<res-ref-name>jdbc/LBIDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref>
<description>OR DB Connection</description>
<res-ref-name>jdbc/ORDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

context.xml

<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/LBIDB" auth="Container"
type="javax.sql.DataSource" username="***" password="***"   driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
url="jdbc:sqlserver:localhost;DatabaseName=YYBackOffice;SelectMethod=cursor;"
maxActive="8" maxIdle="4"/>

<Resource name="jdbc/ORDB" auth="Container"
type="javax.sql.DataSource" username="***" password="***"   driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
url="jdbc:sqlserver:localhost;DatabaseName=XXBackOffice;SelectMethod=cursor;"
maxActive="8" maxIdle="4"/>

The Context tab does have a closing tab, eventually.

Please help! If you need any more information, please let me know. Also, I'm not sure which context.xml ought to be modified, there are 2 in the Tomcat directories, one in the /conf folder, and one in the webapps/appname/META-INF folder. Sorry if it sounds like I'm a bit of a rookie, that's because I am!

Also, I've seen many different examples of the url="..." part of the context.xml, some including port numbers. I have tried several things out online, but nothing seems to work (doesn't help nothing online is my exact data environment, also I suppose it's challenging that this app queries two different DBs at given times).

Thoughts?


回答1:


  1. The context.xml in your web application's META-INF folder will take precedence over the one in the /conf directory, which is really just a generic default.

  2. The open-source JTDS SQL Server driver is way better than Microsoft's. Unless there's an overriding reason, use it instead. The only reason to put it in your tomcat/lib folder is if you're declaring a GlobalNamingResource for the database in your server.xml, otherwise you can just put it in your application's /lib folder.

  3. The JDBC URL for JTDS is: jdbc:jtds:sqlserver://hostname/databasename

  4. The connection driver class for JTDS is: net.sourceforge.jtds.jdbc.Driver




回答2:


In tomcat 6.0.36 it's exactly the opposite way:

CATALINA_HOME/conf/Catalina/your_host/context.xml

will take presedence over the one from

YourApplication/WebContent/META-INF/

After putting data following snippet inside the Context-Tag in Catalina/your_host it worked in my case:

<WatchedResource>WEB-INF/web.xml</WatchedResource>
  <Resource name="jdbc/your_db" auth="Container" type="javax.sql.DataSource"
      maxActive="50" maxIdle="30" maxWait="10000"
      username="your_usr" password="your_pwd" 
      driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://your_host:3306/your_db"/>

See Tomcat Documentation on http://tomcat.apache.org/tomcat-6.0-doc/config/context.html



来源:https://stackoverflow.com/questions/9332276/cannot-create-jdbc-driver-of-class-for-connect-url-null-tomcat-sql-serv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!