• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

No suitable driver found for jdbc:jtds:

 
Franklin Wormwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm new here. I have an extremely frustrating problem when using JTDS. According to Marty Hall's
http://www.slideshare.net/martyhall/java-7-programming-tutorial-accessing-databases-with-jdbc
"In Java SE 6.0 and later (JDBC 4.0 and later), the driver
is loaded automatically." So I tested my code by removing:

Class.forName("net.sourceforge.jtds.jdbc.Driver");

Now, I created a dynamic web project in eclipse. I first created a class then called the DriverManager.getConnection() method with the microsoft sql server's URL connection, database, user and password. Then I put the JTDS1.3.1.jar in the lib directory of my project so it should be in the classpath. I ran my project as a java application and it connected successfully. I want to test this in a servlet so I created a servlet then called the method that would connect my project to sql server. But when I run the project this occurs:

java.sql.SQLException: No suitable driver found for jdbc:jtds:sqlserver://localhost:1433`/sample

I'm confused. Why doesn't the JTDS driver run with the server? I'm using jdk8, eclipse kepler, and tomcat 7. I found out JTDS1.3.1 is not jdbc 4.0 compliant but some users in stackoverflow said that if the file "META-INF\services\java.sql.Driver" is inside the jar file the driver will automatically be loaded. I extracted the jar file and "META-INF\services\java.sql.Driver" is there. What should I do?
 
K. Tsang
Bartender
Posts: 3583
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the jdbc driver file in the tomcat lib folder? If not, is the file packaged with your app's jar file or war file?

If you are a web app, you ought to use connection pool rather than using jdbc connection.

And welcome to the Ranch.
 
Franklin Wormwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Is the jdbc driver file in the tomcat lib folder? If not, is the file packaged with your app's jar file or war file?

Nope it's not in the tomcat lib folder. Should I put it there? I'm not using war file at the moment. I'm just running my project through eclipse and tomcat 7.
 
Paul Clapham
Sheriff
Posts: 21554
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Franklin Wormwood wrote:I'm not using war file at the moment. I'm just running my project through eclipse and tomcat 7.


I would suggest you stop doing that. To use Tomcat in a real environment (as opposed to just testing or playing around) there are certain procedures you must follow. Creating a war file is one of them. So if Eclipse is confusing this process, you should stop running Tomcat via Eclipse. Otherwise you're going to get something working perfectly in "Eclipse+Tomcat" and then find it doesn't work right in a standalone Tomcat environment.
 
Franklin Wormwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Franklin Wormwood wrote:I'm not using war file at the moment. I'm just running my project through eclipse and tomcat 7.


I would suggest you stop doing that. To use Tomcat in a real environment (as opposed to just testing or playing around) there are certain procedures you must follow. Creating a war file is one of them. So if Eclipse is confusing this process, you should stop running Tomcat via Eclipse. Otherwise you're going to get something working perfectly in "Eclipse+Tomcat" and then find it doesn't work right in a standalone Tomcat environment.

Haha yes I'm doing that just for testing purposes. Now I turned my project into a war file, run it via tomcat, the exception still occurs. I put the Class.ForName back then recreate the war file and it works. Why is that?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Franklin Wormwood wrote:I put the Class.ForName back then recreate the war file and it works. Why is that?

In the slides it says that as of Java 6 it is no longer required to load the JDBC driver via Class.forName(), but that's not accurate. The JDBC driver itself must also be JDBC 4 compliant for auto-loading to work. You can check that by looking at the diver JAR, it should have a file called java.sql.Driver in META-INF/services and that file should contain the fully qualified name of the driver class.
 
Franklin Wormwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:
Franklin Wormwood wrote:I put the Class.ForName back then recreate the war file and it works. Why is that?

In the slides it says that as of Java 6 it is no longer required to load the JDBC driver via Class.forName(), but that's not accurate. The JDBC driver itself must also be JDBC 4 compliant for auto-loading to work. You can check that by looking at the diver JAR, it should have a file called java.sql.Driver in META-INF/services and that file should contain the fully qualified name of the driver class.

I checked and the java.sql.Driver file is there inside META-INF/services. though in sourceforge.net it states that JTDS is jdbc 3.0 compliant not 4.0, the java.sql.driver is really inside the jar file. But what's confusing is the auto-loading works in a simple java application (even in a class inside a web dynamic project), but throws an sqlException when I ran it with a servlet.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just out of curiosity: why are you using jTDS? Microsoft provides an excellent JDBC driver for SQLServer - I have a hard time imagining that jTDS is better than that.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The easy way to test whether the problem has anything to do with autoloading would be to add that line back in when you run the code in the servlet container; have you tried that?
 
Franklin Wormwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:The easy way to test whether the problem has anything to do with autoloading would be to add that line back in when you run the code in the servlet container; have you tried that?

You mean add the Class.ForName back? Yes, I tried and it worked.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible that the auto-loading only works for libraries that are loaded at JVM startup time, not for libraries loaded later by other classloaders. Try putting the jar file in Tomcat's lib directory (instead of WEB-INF/lib) and see if that makes a difference.
 
Franklin Wormwood
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:It's possible that the auto-loading only works for libraries that are loaded at JVM startup time, not for libraries loaded later by other classloaders. Try putting the jar file in Tomcat's lib directory (instead of WEB-INF/lib) and see if that makes a difference.

Ok, I just added the jar file in tomcat/lib then restart it. Nothing changed. Successfully connected to java ap, not in server.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic