Tomcat connection to SQL server - sql-server

I connected a SQL server and its driver to my Dynamic Web project. I created a test class (not part of the website) that successfully connects to my database.
However, when I try to connect to my database in my website (in a java servlet actually) and run it on my server/Apache Tomcat I get the exception:
class not found
I don't understand why this is happening. My server is working, and the database connection works in a regular java class. Do I have to set up something else when using a SQL server on a (Tomcat) web server?
Any help would be much appreciated!

I suppose you are using a JDBC driver to connect to your SQLserver from the servlet.
Make sure you have copied the JDBC jar file into the Tomcat app you are actually running the servlet from. Ensure that you are accessing that servlet in particular from your front-end (web site) and not another servlet by mistake which does not have the JDBC classes in its class path:
Typically the jar file goes into webapps/your_app_name/WEB_INF/lib
can you share the stack trace, on which class is your code breaking?

If you are using Maven then you may need to remove the <scope> notation. I was using eClipse, Maven, MSSql. My pom.xml shows the dependancy was grey and marked as test. I removed the line and it worked great.
Change:
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.1.4.jre8-preview</version>
<scope>test</scope>
</dependency>
to:
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.1.4.jre8-preview</version>
</dependency>

Related

Mulesoft Anypoint Studio will not let me connect to SQL Server database

I continue to get a error saying:
Error trying to load driver: com.microsoft.sqlserver.jdbc.SQLServerDriver : Cannot load class 'com.microsoft.sqlserver.jdbc.SQLServerDriver':
Class 'com.microsoft.sqlserver.jdbc.SQLServerDriver' has no package mapping for region 'domain/default/app/s-claims'
I have the dependency for it in my pom.xml:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.2.jre8</version>
</dependency>
I also have the shared library in the pom:
<sharedLibrary>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</sharedLibrary>
When trying to connect directly to SQL Server using the same credentials, I am successful.
I am fairly new to Mulesoft so It could be something simple I am missing. Does anyone have any ideas?
Apparently there was just a issue with the studio. I didn't change anything and tested my connection again and it worked after redeploying.

spring boot configuration with MS SQL Server 2016

I am trying to connect to Microsoft SQL Server 2016 from spring boot application. SQL server is configured using windows authentication.
Following are the configuration
application.properties file
spring.datasource.url=jdbc:sqlserver://K877DTRV:1433;databaseName=testdb;integratedSecurity=true
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.initialize=true
pom.xml
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</dependency>
java version is 1.8.spring boot version is 2.0.3.RELEASE
I am getting the below error while starting the tomcat
Cannot load driver class: com.microsoft.sqlserver.jdbc.SQLServerDriver
Should I do any additional configurations?
Note: I am using Windows Authentication.
Can anyone please help ?
Setting a version to the pom entry for mssql-jdbc worked like a charm
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version><!--$NO-MVN-MAN-VER$-->
</dependency>

Tomcat not able to find Microsoft SQL driver class

Tomcat version - apache-tomcat-9.0.0.M19
I am trying to connect my spring application with microsoft sql database.
driverClassName=com.microsoft.sqlserver.jdbc.SQLserverDriver
Maven dependency -
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
This jar is available in the generated war file and i have also placed the jar in the lib folder of the tomcat but i'm still getting the following error when i try to deploy my application on Tomcat -
Cannot load JDBC driver class 'com.microsoft.sqlserver.jdbc.SQLserverDriver'
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLserverDriver
What am i missing?
You have a typo in your config. The driver class is named
com.microsoft.sqlserver.jdbc.SQLServerDriver
not
com.microsoft.sqlserver.jdbc.SQLserverDriver
................................^...........

Is there any SolrJ documentation available for connecting with Solr 4.0 or ahead

I'm unable to connect to my Solr instance on Tomcat from SolrJ. I've been through the documentation shown at "http://wiki.apache.org/solr/Solrj" , but its pretty outdated - primarily because it has no reference on Solr cores. I'm wondering if somebody could point me to the latest documentation OR advise on how to connect SolrJ with Solr4.0 or ahead - although my instance is running just 1 core for now.
Here's my connection string: "localhost:8080/solr-example/collection1/". Do you know which jars to add along with solrj? Thats where the trouble might be. For examples, the SolrJ wiki references a jar called commons-codec-1.3.jar which is not to be found anywhere, in the solr 4.0 zip file.
Calling Solr from Java with SolrJ
You can specify the Core directly in the URL.
HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/my_core");
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery(q);
solrQuery.setStart(start);
solrQuery.setRows(rows);
QueryResponse response = server.query(solrQuery);
HttpSolrServer is reusable for more queries.
The communication between the Solr Server and SolrJ happens via HTTP with a custom binary format. Can you Browse the Solr Web Admin at http://localhost:8080/solr? Depending on your installation, you might need to adjust the port (8080 is default on Tomcat, jetty uses 8983).
Also, did you deploy Solr with a generic name or did you include the Version? Than your URL would be http://localhost:8080/solr-4.2.1/my_core
Dependencies
These are the minimum dependencies you need for using SolrJ. Add these to your pom.xml, if you are using maven.
<dependency>
<artifactId>solr-solrj</artifactId>
<groupId>org.apache.solr</groupId>
<version>4.2.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
If you are not using maven, you need the following jars:
solr-solrj-4.2.0
zookeeper-3.4.5
commons-io-2.1
httpclient-4.2.3
httpcore-4.2.2
commons-codec-1.6
httpmime-4.2.3
wstx-asl-3.2.3
slf4j-simple-1.5.6
slf4j-api-1.7.2
commons-logging-1.1.1

Setting up maven dependency for SQL Server

I am developing a portlet where I have Hibernate access to SQL Server database. I set up maven dependencies for it and try to find out SQL Server connector on the same way I know MySql has it.
Still my Google-search gives only Mysql if I search for SQL Server connector. What is the right maven dependency value?
Download the driver JAR from the link provided by Olaf and add it to your local Maven repository with;
mvn install:install-file -Dfile=sqljdbc4.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0 -Dpackaging=jar
Then add it to your project with;
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
Answer for the "new" and "cool" Microsoft.
Yay, SQL Server driver now under MIT license on
GitHub: https://github.com/Microsoft/mssql-jdbc
Maven Central: http://search.maven.org/#search%7Cga%7C1%7Cmssql-jdbc
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
Answer for the "old" Microsoft:
For my use-case (integration testing) it was sufficient to use a system scope for the JDBC driver's dependency as such:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>3.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/sqljdbc4.jar</systemPath>
<optional>true</optional>
</dependency>
That way, I could put the JDBC driver into local version control. No need to have each developer manually set stuff up in their own repositories.
I took inspiration from this answer to another Stack Overflow question and I've also blogged about it here.
There is also an alternative: you could use the open-source jTDS driver for MS-SQL Server, which is compatible although not made by Microsoft.
For that driver, there is a maven artifact that you can use:
http://jtds.sourceforge.net/
From http://mvnrepository.com/artifact/net.sourceforge.jtds/jtds :
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
UPDATE nov 2016, Microsoft now published its MSSQL JDBC driver on github and it's also available on maven now:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
I believe you are looking for the Microsoft SQL Server JDBC driver: http://msdn.microsoft.com/en-us/sqlserver/aa937724
Be careful with the answers above. sqljdbc4.jar is not distributed with under a public license which is why it is difficult to include it in a jar for runtime and distribution. See my answer below for more details and a much better solution. Your life will become much easier as mine did once I found this answer.
https://stackoverflow.com/a/30111956/3368958
Even after installing the sqlserver jar, my maven was trying to fetch the dependecy from maven repository. I then, provided my pom the repository of my local machine and it works fine after that...might be of help for someone.
<repository>
<id>local</id>
<name>local</name>
<url>file://C:/Users/mywindows/.m2/repository</url>
</repository>
<dependency>
<groupId>com.hynnet</groupId>
<artifactId>sqljdbc4-chs</artifactId>
<version>4.0.2206.100</version>
</dependency>
This worked for me(if you use maven)
https://search.maven.org/artifact/com.hynnet/sqljdbc4-chs/4.0.2206.100/jar
It looks like Microsoft has published some their drivers to maven central:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>

Resources