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

This error comes when you lot sweat to connect to Microsoft SQL Server database from Java programme but the required JDBC driver is non available inward Classpath or driver is available inward CLASSPATH but the class loader is non able to honour it due classpath intricacies. Depending upon your situation, a solution could survive every bit uncomplicated every bit downloading whatsoever of sqljdbc.jar, sqljdbc4.jar or sqljdbc41.jar, based upon the Java version you lot are using in addition to adding them into CLASSPATH every bit set CLASSPATH = %CLASSPATH%; (path to Microsoft JDBC driver) inward Windows. BTW, inward 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 pick but you lot added the JAR into CLASSPATH environs variable or vice-versa.

Similarly, inward Java spider web application JDBC drivers are required to survive nowadays inward WEB-INF/lib directory or if you lot are running on Tomcat in addition to then tomcat/lib directory every bit well. Finding solution becomes slow in 1 trial you lot know how classpath concept plant inward Java, a must know for whatsoever Java developer.

Let's sweat to honour out the existent sweat of java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver in addition to how to solve this nasty mistake which is preventing your Java programme to connect to Microsoft SQL Server 2008, 2012 or fifty-fifty 2014 database.

By the way, if you lot actually desire to sympathise Java database programming from scratch, I propose you lot read the book, Practical database programming amongst Java book past times Ying Bai. This mass explains every of import exceptional of Java database connectivity in addition to JDBC amongst the uncomplicated example.

error comes when you lot sweat 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 gild to connect to whatsoever database including SQL Server from Java program, nosotros operate JDBC (Java Database Connectivity) API. This API is nowadays inward JDK itself thus you lot don't involve whatsoever additional JAR to operate this but because it has to back upward 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 degree is a must for connecting to database. If you lot know the lilliputian fight of history, at that spot are 4 types of JDBC drivers, but forthwith days almost all database back upward type 4, pure Java JDBC driver, which comes every bit JAR file in addition to quite slow to use.


All you lot involve to create is include those JAR e.g. mysql-connector.jar for MySQL, ojdbc6.jar for Oracle 11g database in addition to sqljdbc4.jar for SQL Server into your Java application's classpath.


Sometimes you lot may honour that at that spot 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 inward unlike JREs.

The latest version of JDBC is JDBC 4.1 released inward Java 7, which is supported past times Microsoft inward sqljdbc41.jar, in addition to you lot must operate this JAR if you lot are connecting Microsoft SQL Server 2008 or 2012 from Java 1.7 or Java 1.8. If you lot operate this JAR amongst JRE version less than 1.7, you lot volition acquire exception e.g. SQLFeatureNotSupportedException.

Similarly, if you lot are connecting to SQL Server database from Java 1.6 in addition to then operate sqljdbc4.jar, which supports JDBC 4.0 version, using this JAR inward Java 1.5 volition upshot inward Exception. Similarly, if you lot are connecting to MSSQL database from JRE 1.5 in addition to then operate 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 almost novel JDBC features introduced inward JDBC 3.0 in addition to JDBC 4.0 on JDK 7.




com.microsoft.sqlserver.jdbc.SQLServerDriver

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

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

In heart Java, it could survive your uncomplicated Java programme which is executing this business in addition to inward company Java, it could survive a web server or company server or a framework executing this code on your behalf.

error comes when you lot sweat 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 survive lilliputian different, but this is what you lot should sweat :


1) Check if you lot conduct keep downloaded JDBC Driver for Microsoft SQL Server i.e. whatsoever of sqljdbc.jar, sqljdbc4.jar or sqljdbc41.jar, if non in addition to then delight download it from hither http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11774 in addition to add together into CLASSPATH of your Java program.


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


3) Make certain you lot solely add together 1 of these JAR, non all or them or fifty-fifty 2 of them.


4) If you lot are connecting to Microsoft SQL Server from Tomcat or whatsoever company server e.g. JBoss or Weblogic, in addition to then brand certain you lot include MSSQL JDBC driver JAR inward WEB-INF/lib directory. Sometimes, you lot likewise involve to add together this into tomcat/lib if at that spot are multiple application which is connecting to SQL Server.


5) For heart Java application, connecting to SQL server from Windows environs in addition to using CLASSPATH environs variable, append the place of sqljdbc4.jar (or the JAR you lot are using) into CLASSPATH every bit set CLASSPATH = %CLASSPATH%; {location of driver jar}. Please notice, separator inward 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 degree path every bit export CLASSPATH = ${CLASSPATH} : (directory of driver jar}. Please note, the separator inward Linux is a colon(:). BTW, don't acquire confused betwixt using same JAR file inward Windows in addition to Linux, Java is platform independent in addition to that's why you lot tin operate same JAR file inward all platforms. Also, fifty-fifty though MSSQL solely runs on Windows machine, you lot tin withal connect to it from Linux using Java client.


7) If your Java programme is using -cp or -classpath pick e.g. java -cp {%LIB%} MyProgram, in addition to then brand certain you lot include the JDBC driver jounce into the LIB environs variable. If you lot are straight providing the JAR similar inward simplest cases operate :

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


8) If you lot are running Java programme inward Eclipse, brand certain you lot add together sqljdbc41.jar (or whatsoever JDBC driver JAR) into construct path past times selecting your program, right click, configure construct path.



That's all almost how to solve java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver inward Java. If you lot are withal non able to solve the employment in addition to then delight banking company gibe for unlike CLASSPATH related issues every bit shown inward my shipping almost how CLASSPATH plant inward Java. It's non unlike than solving similar employment comes spell 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 approximately cases, it's the wrong JAR file in addition to inward approximately rare cases, it's those classpath mysteries.


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

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


Sumber https://javarevisited.blogspot.com/

0 Response to "Java.Lang.Classnotfoundexception: Com.Microsoft.Sqlserver.Jdbc.Sqlserverdriver [Solution]"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel