1

I've established a database connection in my program but after creating a jar program it is throwing an

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

It looks like program don't see jdbc driver which I have installed and added to artifact:

enter image description here

I've tried changing a versions changing link looking for class name in JDBC but I've found same one that I've used before which worked in intelij but not in jar

There is a code that I'm using for connection

package howareyoufeeling.database;

import java.sql.*;

public class DatabaseOperations {
    static Statement statement;

    public static void connectWithAzureDatabase() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String connectionUrl =
                "jdbc:sqlserver://howareyoufeeling.database.windows.net:1433;" +
                        "database=HowAreYouFeeling;" +
                        "user=wikimmax@howareyoufeeling;password=********;" +
                        "encrypt=true;trustServerCertificate=false;" +
                        "hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
        try {
            Connection connection = DriverManager.getConnection(connectionUrl);
            System.out.println("Successfull DB connection " + connection.getSchema());
            statement = connection.createStatement();

        } catch (SQLException e) {
            e.printStackTrace();

        }
    }
    public static void sendDataQuery(String query){
        try {
            statement.execute(query);
        } catch (SQLException e) {
            e.printStackTrace();

        }
    }
}

It looks like dependency is not in jar file but it is i checked it by decompiling and file size matches that includes a driver

Please help

3
  • Are you using Gradle or Maven to manage dependencies? Commented Aug 19, 2019 at 14:53
  • I'm using maven Commented Aug 19, 2019 at 15:00
  • Can you post your pom.xml? Commented Aug 20, 2019 at 6:54

2 Answers 2

0

It's a packaging issue if you get that exception at runtime.

Your executable JAR needs to have all the dependencies built inside it. There's a Maven plugin that can do it for you.

I'd recommend moving your database configuration outside of code so you can modify it more easily.

Sign up to request clarification or add additional context in comments.

2 Comments

What do you mean by moving Database configuration outside ? plus this is strange because i've also tried adding jdbc directly do library as a jar in this case it should compile.
If I was writing this app in Spring Boot the configuration of the data source would be done in application.yml. I'd use a Hikari data source connection pool. I would not be writing your DatabaseUtils class at all.
0

Problem has been solved. The main wrong thing there was that I've tried to build a jar as application not as as Javafx app and database jar file was inside my build jar

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.