Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 798
aohm1989 Avatar asked Feb 17 '12 16:02

aohm1989


2 Answers

  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

like image 138
Matt Brock Avatar answered Nov 03 '22 00:11

Matt Brock


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

like image 20
Picrochole Avatar answered Nov 03 '22 00:11

Picrochole