Connecting to MS SQL Server using Windows authentication and JTDS - sql-server

I am unable to connect to SQL Server using Windows Authentification.
This is is the exception being thrown:
Caused by: java.io.IOException: SSO Failed: Native SSPI library not loaded. Check the java.library.path system property.
at net.sourceforge.jtds.jdbc.TdsCore.sendMSLoginPkt(TdsCore.java:1889)
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:587)
... 35 more
Here is the Spring configuration:
#Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("net.sourceforge.jtds.jdbc.Driver");
dataSource.setUrl(...);
return dataSource;
}
The following VM argument is defined:
-Djava.library.path=C:\x64\SSO
This points to ntlmauth.dll (from jtds-1.3.1-dist.zip)
The following dependency is defined in the pom:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>com.springsource.net.sourceforge.jtds</artifactId>
<version>1.2.2</version>
</dependency>
Have obviously omitted something - any help is very appreciated!

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.

Error with tomcat when deploying Google App Engine application

I'm following this tutorial to deploy my API on GAE with a SQL Cloud DB and it's been several hours now that I'm testing all the possibilities but I still encounter several errors that I can't fix. When I exclude the tomcat dependency I get the error :
java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletMapping
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:206) ~[spring-test-5.3.4.jar:5.3.4]
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132) ~[spring-test-5.3.4.jar:5.3.4]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244) ~[spring-test-5.3.4.jar:5.3.4]
at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:138) [spring-test-5.3.4.jar:5.3.4]
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$6(ClassBasedTestDescriptor.java:350) [junit-jupiter-engine-5.7.1.jar:5.7
When I remove this exclusion from my pom.xml :
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
the app works fine locally and connect well to my SQL Cloud DB but when I deploy with flex env I get those error on myproject.region.r.appspot.com :
Error: Server Error
The server encountered an error and could not complete your request.
Please try again in 30 seconds.
and in cloud console Error reporting :
java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution
at
org.eclipse.jetty.annotations.ServletContainerInitializersStarter.doStart (ServletContainerInitializersStarter.java:68)
at
org.eclipse.jetty.util.component.AbstractLifeCycle.start (AbstractLifeCycle.java:68)
at
org.eclipse.jetty.servlet.ServletContextHandler.startContext (ServletContextHandler.java:330)
at
org.eclipse.jetty.webapp.WebAppContext.startWebapp (WebAppContext.java:1406)
at
com.google.apphosting.runtime.jetty9.AppEngineWebAppContext.startWebapp (AppEngineWebAppContext.java:175)
at
org.eclipse.jetty.webapp.WebAppContext.startContext (WebAppContext.java:1368)
at
org.eclipse.jetty.server.handler.ContextHandler.doStart (ContextHandler.java:778)
Thank you.

Spring Boot 2.1.2 Create dynamic DB connection just to run a query

In the Spring Boot 2.1.2 project, there is a default PostgreSQL db connection defined in application.properties:
spring.datasource.url=jdbc:postgresql://host:port/dbName
spring.datasource.username=username
spring.datasource.password=pass
Not any problem with Postgre DB, however, I need to connect another DB (MsSql) by JDBC connection rarely.
I just need to run a query, and process result list from a view of mssql table.
What I tried is;
Adding jdbc drivers to Pom.xml for Mssql:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.2.1.jre11</version>
<scope>test</scope>
</dependency>
Connect to sql-server and run query:
#GetMapping(path = "/getKonteynersFromSifirAtikServis/", produces = "application/json")
public ResponseEntity<ResponseStatusDto> getKonteynersFromSifirAtikServis() {
try {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl("jdbc:mysql://host:1433/msDbName");
dataSource.setUsername("ms_user");
dataSource.setPassword("ms_pass");
JdbcTemplate template = new JdbcTemplate(dataSource);
SqlRowSet sqlRowSet = template.queryForRowSet("SELECT * FROM dbo.viewName;");
} catch (Exception e) {
return new ResponseEntity<ResponseStatusDto>(ResponseStatusDto.createErrorDto(e.getMessage(), e), HttpStatus.ACCEPTED);
}
}
This did not work. It throws:
Could not load JDBC driver class [com.microsoft.sqlserver.jdbc.SQLServerDriver]
I have been getting hard to load mssql-jdbc inside pom definition. It is resolved to make version 7.2.1.jre11.
Scope of the dependency is set as test.
test This scope indicates that the dependency is not required for
normal use of the application, and is only available for the test
compilation and execution phases. This scope is not transitive.
Remove the same and attempt a run

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
................................^...........

Database connection pointing to wrong database

I have a web project in eclipse using glassfish. I have the following datasource entries in context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/TestDS" type="javax.sql.DataSource"
url="jdbc:oracle:thin:#server:1521:db1"
driverClassName="oracle.jdbc.OracleDriver" username="test" password="test" />
</Context>
When I run my application
DatabaseMetaData dmd = connection.getMetaData();
String name = dmd.getDatabaseProductName();
Database product name is always Apache Derby
What could be the reason for this? I am trying to connect to Oracle Database however connection is made to Apache Derby.
How can I resolve this issue?
Thanks
Update 1
public static DatabaseConnection getInstance(String name)
throws DatabaseException {
DatabaseConnection instance;
DataSource ds;
try {
InitialContext ctx = new InitialContext();
ds = (DataSource) new InitialContext().lookup("java:comp/env/"
+ name);
} catch (NamingException e) {
e.printStackTrace();
throw new DatabaseException("###data source is invalid ###" + e);
}
instance = new MYDataSource(ds);
return instance;
}
seems as if you are mixing some stuff up here. context.xml obviously is a Apache Tomcat configuration file. In GlassFish you need to either use the glassfish-resources.xml or configure your domain directly via either the admin GUI or the domain.xml.
There is a basic example of how to do this with GlassFish 3.1 in the GlassFish docs:
http://docs.oracle.com/cd/E18930_01/html/821-2416/ggndx.html
Have fun!
Markus
I have resolved this issue.
I created a a JDBC Resource under GlassFish and pointed connection to my connection created under Date Source Explorer. It created a sun-resources.xml. I had to manually rename this to glassfish-resource.xml. By default there are no entry for password,so I had to manually add <property name="Passwrod" value="password" />. When I ran my web application I was getting this error
java.sql.SQLException: Error in allocating a connection. Cause: No PasswordCredential found
So to resolve this error I added a property in JDBC Connection Pool in glassfish admin server. Now I could connect to Oracle database.
Not sure why I have to do two steps manually.
Thanks

Resources