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

This error comes when you lot endeavor 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 you lot are using in addition to 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 selection but you lot 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 you lot are running on Tomcat hence tomcat/lib directory equally well. Finding solution becomes tardily 1 time you lot know how classpath concept industrial plant inwards Java, a must know for whatsoever Java developer.

Let's endeavor to notice out the existent campaign of java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver in addition to how to solve this nasty fault 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 empathise 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 special of Java database connectivity in addition to JDBC amongst the uncomplicated example.

error comes when you lot endeavor 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 social club 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 hence you lot don't involve whatsoever additional JAR to purpose this but because it has to back upwardly dissimilar 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 you lot know the lilliputian fight of history, in that place are 4 types of JDBC drivers, but straightaway days almost all database back upwardly type 4, pure Java JDBC driver, which comes equally JAR file in addition to quite tardily to use.


All you lot involve to produce 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 notice that in that place 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 dissimilar JDBC version available inwards dissimilar JREs.

The latest version of JDBC is JDBC 4.1 released inwards Java 7, which is supported past times Microsoft inwards sqljdbc41.jar, in addition to you lot must purpose this JAR if you lot are connecting Microsoft SQL Server 2008 or 2012 from Java 1.7 or Java 1.8. If you lot purpose 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 hence purpose sqljdbc4.jar, which supports JDBC 4.0 version, using this JAR inwards Java 1.5 volition upshot inwards Exception. Similarly, if you lot are connecting to MSSQL database from JRE 1.5 hence purpose sqljdbc.jar, which uses JDBC 3.0.

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




com.microsoft.sqlserver.jdbc.SQLServerDriver

In social club to charge the JDBC driver from sqljdbc.jar, Java uses Class.forName() method. This method takes the bring upwardly 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 upshot of this method telephone vociferation upwardly JVM  (class loader) volition hold off for a flat com.microsoft.sqlserver.jdbc.SQLServerDriver inwards your Java program's classpath. If it doesn't flora the flat it throws "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver".

In inwardness Java, it could live on your uncomplicated Java programme which is executing this occupation in addition to inwards firm Java, it could live on a web server or firm server or a framework executing this code on your behalf.

error comes when you lot endeavor to connect to Microsoft SQL Server database from Java programme but java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver [Solution]


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

As I said, depending upon private province of affairs solution of this fault could live on lilliputian different, but this is what you lot should endeavor :


1) Check if you lot receive got downloaded JDBC Driver for Microsoft SQL Server i.e. whatsoever of sqljdbc.jar, sqljdbc4.jar or sqljdbc41.jar, if non hence 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 firm server e.g. JBoss or Weblogic, hence brand certain you lot include MSSQL JDBC driver JAR inwards WEB-INF/lib directory. Sometimes, you lot equally good involve to add together this into tomcat/lib if in that place are multiple application which is connecting to SQL Server.


5) For inwardness 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 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 in addition to Linux, Java is platform independent in addition to that's why you lot tin purpose same JAR file inwards all platforms. Also, fifty-fifty though MSSQL solely runs on Windows machine, you lot tin nevertheless connect to it from Linux using Java client.


7) If your Java programme is using -cp or -classpath selection e.g. java -cp {%LIB%} MyProgram, hence brand certain you lot include the JDBC driver jounce into the LIB environs variable. If you lot are straight 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 you lot are running Java programme inwards 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 well-nigh how to solve java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver inwards Java. If you lot are nevertheless non able to solve the occupation hence delight cheque for dissimilar CLASSPATH related issues equally shown inwards my ship well-nigh how CLASSPATH industrial plant inwards Java. It's non dissimilar than solving similar occupation 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 about cases, it's the wrong JAR file in addition to inwards about rare cases, it's those classpath mysteries.


Other classpath related issues which comes when you lot endeavor to connect database from Java programme :
  • How to gear upwardly java.lang.ClassNotFoundException: org.postgresql.Driver fault 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 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

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