Error in SQL Server connection with Java in Resteasy API

0

I'm using the Microsoft 6.0 JDBC driver in a one-time project API using Java (JRE 1.8) with resteasy using Eclipse Oxygen and Tomcat 7 (I tried with other versions and presented the same behavior). My connection code is as follows:

public class Conexao {
    public Conexao() {}

    public Connection conectar() {
        final String USUARIO = "***";
        final String SENHA = "***";
        final String URL = "jdbc:sqlserver://server:1433;databaseName=dbName";

        Connection con = null; 
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(URL, USUARIO, SENHA);
            System.out.println(con.toString());
        } catch (ClassNotFoundException e) {
            System.out.println("Erro ao encontrar o driver");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("Erro ao conectar com o banco");
            e.printStackTrace();
        } 

        return con;
    }

    public Boolean fecharConexao(Connection con) throws SQLException {
        try {
            con.close();
        } catch (SQLException e) {
            System.out.println("Erro ao fechar a conexão");
            e.printStackTrace();
        }

        return con.isClosed();
    }

}

I have .jar imported into my build path:

I have a test class that executes and returns the connection successfully, just like the date.

class ConexaoTest {

    @Test
    void testConectar() throws SQLException {
        Conexao conexao = new Conexao();
        Connection con = conexao.conectar();
        assertEquals(false, con.isClosed());
    }

    @Test
    void testFecharConexao() throws SQLException {
        Conexao conexao = new Conexao();
        Connection con = conexao.conectar();
        assertEquals(true, conexao.fecharConexao(con));
        assertEquals(true, con.isClosed());
    }

}

However, in my code when I run inside other packages during API calls, I always get the connection as null because the driver was not found.

Erro ao encontrar o driver
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at br.gov.rn.natal.semut.util.Conexao.conectar(Conexao.java:17)
    at br.gov.rn.natal.semut.dao.DaoGenerico.execProcedure(DaoGenerico.java:17)
    at br.gov.rn.natal.semut.service.ServicoGenericoImpl.execProcedureSimples(ServicoGenericoImpl.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:140)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:402)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:209)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
dez 11, 2017 10:53:47 AM org.apache.catalina.core.StandardWrapperValve invoke

Does anyone have any idea what this problem might be?

    
asked by anonymous 11.12.2017 / 14:57

1 answer

2

The solution I found was to import the jdbc driver from Maven. I put the following code in my pom.xml :

<dependency>
 <groupId>com.microsoft.sqlserver</groupId>
 <artifactId>mssql-jdbc</artifactId>
 <version>6.2.1.jre8</version>
</dependency>

Following the Microsoft documentation .

    
13.12.2017 / 15:13