I am trying to connect to my SQL Server 2008 database from Java and I'm having the same problem from this thread.
String userName = "xxxx";
String password = "xxxx";
String url = "jdbc:sqlserver:xxx.xxx.xxx.xxx;databaseName=asdfzxcvqwer;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
connection = DriverManager.getConnection(url, userName, password);
I keep getting a ClassNotFoundException
Whenever I try to load the driver from Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at repositories.RepositoryBase.<init>(RepositoryBase.java:22)
at repositories.ProductsRepository.<init>(ProductsRepository.java:13)
at api.Products.init(Products.java:31)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:865)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:136)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I've made sure the necessary jdbc and jtds drivers are added to the library and CLASSPATH is set as well. I'm honestly not sure what went wrong here.
Edit: Upon suggestion, I tried to download this jdbc jar and place it in my WEB-INF/lib, then set my CLASSPATH variable to that location. Still, the same problem is happening.
Edit2: Never mind, completely reinstalling Eclipse made it work. This is pretty frustrating...
The JDBC driver files are installed in C:\program files\microsoft SQL server <ver> JDBC Driver\lib.
Before you connect to a SQL Server database, SQL Server must first be installed on either your local computer or a server, and the JDBC driver must be installed on your local computer.
You can determine the version of the JDBC driver that you installed, by calling the getDriverVersion method of the OracleDatabaseMetaData class. You can also determine the version of the JDBC driver by executing the following commands: java -jar ojdbc5.
com.microsoft.sqlserver.jdbc.sqlserverdriver not found error occurs when JDBC driver is missing on Controller application server. As part of our Server administration services, Bobcares responds to all inquiries, large and small.
The driver with name com.microsoft.jdbc.sqlserver.SQLServerDriver is from the very old SQL Server 2000 JDBC driver. In the SQL Server 2005 JDBC driver, Microsoft changed this to com.microsoft.sqlserver.jdbc.SQLServerDriver (note the switch of order between sqlserver and jdbc.
When the driver is loaded, you can establish a connection by using a connection URL and the getConnection method of the DriverManager class: Starting from JDBC API 4.0, the DriverManager.getConnection () method is enhanced to load JDBC drivers automatically.
This error comes when you try to connect to the Microsoft SQL Server database from the Java program but the required JDBC driver is not available in Classpath or driver is available in CLASSPATH but the class loader is not able to find it due to classpath intricacies.
You dont need both jTDS and JDBC in your classpath. Any one is required. Here you need only sqljdbc.jar
.
Also, I would suggest to place sqljdbc.jar
at physical location to /WEB-INF/lib
directory of your project rather than adding it in the Classpath via IDE. Then Tomcat takes care the rest. And also try restarting Tomcat.
You can download Jar from : www.java2s.com/Code/JarDownload/sqlserverjdbc/sqlserverjdbc.jar.zip
EDIT:
As you are supplying Username and Password when connecting,
You need only jdbc:sqlserver://localhost:1433;databaseName=test
, Skip integratedSecurity
attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With