1

I have created project want to deploy it using Jenkins and Git. I created maven project and added all the dependencies required. My one of class contains following code-

try {
    DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
} catch(Exception e) {
    System.out.println("Problem registering JDBC driver");
}

and indicates error at com.microsoft.jdbc.sqlserver.SQLServerDriver() this line like not able to find com.microsoft package.

I have added maven dependency for Microsoft server driver in POM file-

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.4.0.jre8</version>
    <scope>test</scope>
</dependency>

Now when i try to run that project with clean install it gives error as:

package com.microsoft.jdbc.sqlserver does not exist

and fails the build.
What should I do to remove this error and run my project successfully?

Pavan Divekar
  • 449
  • 2
  • 14
  • did you update the project?maven->updateproject – NKR Jun 28 '18 at 06:10
  • 1
    You should **not** load a JDBC driver like that. Calling `DriverManager.registerDriver` is not for applications to be called, it is **only** for JDBC drivers to register themselves when loaded. If you need to load, then use `Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")` and the driver will register itself. – Mark Rotteveel Jun 28 '18 at 06:53

1 Answers1

1

You include the artifact with <scope>test</scope> but apparently use it in the main code.

Change the scope to compile or (if scope of this artifact is not managed elsewhere) simply remove <scope>test</scope>.

For the future, what helps with this kind of questions is to simply run:

mvn dependency:tree -Dscope=compile

You will see a tree of artifacts considered for the compilation.

lexicore
  • 42,748
  • 17
  • 132
  • 221