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

When manually connecting to the SQL Server database in the code, an error [java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver] is reported.

I know the general reason is because the driver package is missing, and because it is in the Maven environment, it seems that it will not work after introducing the dependency of sqljdbc4 through Maven, so I can only find another way.

Another way is to copy the sqljdbc4.jar file to the lib directory under WEB-INF in the project, and try it out and you can connect to the SQL Server database normally.

Then I also found that the 2008 version driver is a bit different from the 2000 version. The previous driver class name is [com.microsoft.jdbc.sqlserver.SQLServerDriver], but the version above 2008 is [com.microsoft.sqlserver.jdbc.SQLServerDriver] , have you noticed that the package names of [sqlserver] and [jdbc] have been exchanged, and the prefix of the url has also changed from [jdbc:microsoft:sqlserver://] to [jdbc:sqlserver://], Microsoft really It's logical genius.

If the final application is to be run under Tomcat or some container, the code for loading the driver package class needs to be added to the code, otherwise the above error will be reported.

try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e1) {
    e1.printStackTrace();
}

In addition, sqljdbc4.jar requires a running environment above jre6.

 

"You think I don't know, but I already know."

Reprinted in: https://www.cnblogs.com/yanggb/p/11605947.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324783245&siteId=291194637