Java.Lang.Classnotfoundexception: Com.Microsoft.Sqlserver.Jdbc.Sqlserverdriver [Solution]

This error comes when y'all elbow grease to connect to Microsoft SQL Server database from Java programme but the required JDBC driver is non available inwards Classpath or driver is available inwards CLASSPATH but the class loader is non able to notice it due classpath intricacies. Depending upon your situation, a solution could live on equally uncomplicated equally downloading whatsoever of sqljdbc.jar, sqljdbc4.jar or sqljdbc41.jar, based upon the Java version y'all are using too adding them into CLASSPATH equally set CLASSPATH = %CLASSPATH%; (path to Microsoft JDBC driver) inwards Windows. BTW, inwards most of the cases "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver" comes because of classpath intricacies. For example, if your Java programme is using -cp or -classpath alternative but y'all added the JAR into CLASSPATH environs variable or vice-versa.

Similarly, inwards Java spider web application JDBC drivers are required to live on nowadays inwards WEB-INF/lib directory or if y'all are running on Tomcat thus tomcat/lib directory equally well. Finding solution becomes tardily in ane lawsuit y'all know how classpath concept industrial plant inwards Java, a must know for whatsoever Java developer.

Let's elbow grease to notice out the existent displace of java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver too how to solve this nasty mistake which is preventing your Java programme to connect to Microsoft SQL Server 2008, 2012 or fifty-fifty 2019 database.

By the way, if y'all actually desire to sympathize Java database programming from scratch, I propose y'all read the book, Practical database programming amongst Java book past times Ying Bai. This majority explains every of import exceptional of Java database connectivity too JDBC amongst the uncomplicated example.

error comes when y'all elbow grease to connect to Microsoft SQL Server database from Java programme but java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver [Solution]



Which JDBC Driver JAR to use?

In lodge to connect to whatsoever database including SQL Server from Java program, nosotros purpose JDBC (Java Database Connectivity) API. This API is nowadays inwards JDK itself thus y'all don't require whatsoever additional JAR to purpose this but because it has to back upwards unlike database vendor, it has provided a Driver interface, which is required for database vendors similar Oracle, MySQL or Microsoft SQL Server to implement, if they desire to allow their database access via JDBC.

This Driver implementation flat is a must for connecting to database. If y'all know the piffling fleck of history, at that topographic point are 4 types of JDBC drivers, but directly days almost all database back upwards type 4, pure Java JDBC driver, which comes equally JAR file too quite tardily to use.


All y'all require to produce is include those JAR e.g. mysql-connector.jar for MySQL, ojdbc6.jar for Oracle 11g database too sqljdbc4.jar for SQL Server into your Java application's classpath.


Sometimes y'all may notice that at that topographic point is fifty-fifty multiple JDBC driver JAR available for the same database e.g. sqljdbc.jar, sqljdbc4.jar or sqljdbc41.jar for MSSQL database. This is because of unlike JDBC version available inwards unlike JREs.

The latest version of JDBC is JDBC 4.1 released inwards Java 7, which is supported past times Microsoft inwards sqljdbc41.jar, too y'all must purpose this JAR if y'all are connecting Microsoft SQL Server 2008 or 2012 from Java 1.7 or Java 1.8. If y'all purpose this JAR amongst JRE version less than 1.7, y'all volition acquire exception e.g. SQLFeatureNotSupportedException.

Similarly, if y'all are connecting to SQL Server database from Java 1.6 thus purpose sqljdbc4.jar, which supports JDBC 4.0 version, using this JAR inwards Java 1.5 volition lawsuit inwards Exception. Similarly, if y'all are connecting to MSSQL database from JRE 1.5 thus purpose sqljdbc.jar, which uses JDBC 3.0.

See  Core Java Volume II Advanced Features past times Cay S. Horstmann to acquire to a greater extent than nigh novel JDBC features introduced inwards JDBC 3.0 too JDBC 4.0 on JDK 7.




com.microsoft.sqlserver.jdbc.SQLServerDriver

In lodge to charge the JDBC driver from sqljdbc.jar, Java uses Class.forName() method. This method takes the advert of the flat which implement java.sql.Driver interface as String  and tries to charge that flat past times searching into classpath e.g. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").

As a lawsuit of this method telephone holler upwards JVM  (class loader) volition await for a flat com.microsoft.sqlserver.jdbc.SQLServerDriver inwards your Java program's classpath. If it doesn't constitute the flat it throws "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver".

In gist Java, it could live on your uncomplicated Java programme which is executing this describe of piece of work too inwards corporation Java, it could live on a web server or corporation server or a framework executing this code on your behalf.

error comes when y'all elbow grease to connect to Microsoft SQL Server database from Java programme but java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver [Solution]


How to cook java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver

As I said, depending upon private province of affairs solution of this mistake could live on piffling different, but this is what y'all should elbow grease :


1) Check if y'all receive got downloaded JDBC Driver for Microsoft SQL Server i.e. whatsoever of sqljdbc.jar, sqljdbc4.jar or sqljdbc41.jar, if non thus delight download it from hither http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 too add together into CLASSPATH of your Java program.


2) Make certain y'all download the right JDBC driver JAR for SQL SERVER depending upon your Java version e.g. sqljdbc.jar if y'all are connecting to Microsoft SQL server from Java 1.5, sqljdbc4.jar if y'all are running Java 1.6 or sqljdbc41.jar if y'all are using Java 1.7 or Java 8.


3) Make certain y'all alone add together ane of these JAR, non all or them or fifty-fifty ii of them.


4) If y'all are connecting to Microsoft SQL Server from Tomcat or whatsoever corporation server e.g. JBoss or Weblogic, thus brand certain y'all include MSSQL JDBC driver JAR inwards WEB-INF/lib directory. Sometimes, y'all likewise require to add together this into tomcat/lib if at that topographic point are multiple application which is connecting to SQL Server.


5) For gist Java application, connecting to SQL server from Windows environs too using CLASSPATH environs variable, append the location of sqljdbc4.jar (or the JAR y'all are using) into CLASSPATH equally set CLASSPATH = %CLASSPATH%; {location of driver jar}. Please notice, separator inwards windows is semicolon (;)


6) For Java application connecting to Microsoft SQL Server from UNIX based systems e.g. Linux, include this JAR file into the flat path equally export CLASSPATH = ${CLASSPATH} : (directory of driver jar}. Please note, the separator inwards Linux is a colon(:). BTW, don't acquire confused betwixt using same JAR file inwards Windows too Linux, Java is platform independent too that's why y'all tin sack purpose same JAR file inwards all platforms. Also, fifty-fifty though MSSQL alone runs on Windows machine, y'all tin sack nonetheless connect to it from Linux using Java client.


7) If your Java programme is using -cp or -classpath alternative e.g. java -cp {%LIB%} MyProgram, thus brand certain y'all include the JDBC driver jounce into the LIB environs variable. If y'all are direct providing the JAR similar inwards simplest cases purpose :

coffee -cp .;C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc.jar


8) If y'all are running Java programme inwards Eclipse, brand certain y'all add together sqljdbc41.jar (or whatsoever JDBC driver JAR) into create path past times selecting your program, right click, configure create path.



That's all nigh how to solve java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver inwards Java. If y'all are nonetheless non able to solve the occupation thus delight cheque for unlike CLASSPATH related issues equally shown inwards my post service nigh how CLASSPATH industrial plant inwards Java. It's non unlike than solving similar occupation comes piece connecting to Oracle, MySQL, PostgreSQL or whatsoever other relational database. In most cases, it's simply the missing JAR file which causes the problem. In about cases, it's the wrong JAR file too inwards about rare cases, it's those classpath mysteries.


Other classpath related issues which comes when y'all elbow grease to connect database from Java programme :
  • How to cook java.lang.ClassNotFoundException: org.postgresql.Driver mistake inwards Java? [solution]
  • General Guide to solve java.lang.ClassNotFoundException inwards Java [guide]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver inwards Java MySQL? [solution]
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory? [solution]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver inwards Java? [solution]
  • How to solve java.lang.ClassNotFoundException:org.Springframework.Web.Context.ContextLoaderListener [solution]

Further Learning
JSP, Servlets too JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 too 2
Java Platform: Working amongst Databases Using JDBC

Belum ada Komentar untuk "Java.Lang.Classnotfoundexception: Com.Microsoft.Sqlserver.Jdbc.Sqlserverdriver [Solution]"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel