Sunday, July 5, 2015

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver [Solution]

This error comes when you try to connect to Microsoft SQL Server database from Java program but the required JDBC driver is not available in Classpath or driver is available in CLASSPATH but class loader is not able to find it due classpath intricacies. Depending upon your situation, solution could be as simple as downloading any of sqljdbc.jar, sqljdbc4.jar or sqljdbc41.jar, based upon the Java version you are using and adding them into CLASSPATH as set CLASSPATH = %CLASSPATH%; (path to Microfsoft JDBC driver) in Windows. BTW, in most of the cases "java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver" comes because of classpath intricacies. For example, if your Java program is using -cp or -classpath option but you added the JAR into CLASSPATH environment variable or vice-versa. Similarly, in Java web application JDBC drivers are required to be present in WEB-INF/lib directory or if you are running on Tomcat then tomcat/lib directory as well. Finding solution becomes easy once you know how CLASSPATH concept works in Java, a must know for any Java developer. Let's try to find out the real cause of java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver and how to solve this nasty error which is preventing your Java program to connect to Microsoft SQL Server 2008, 2012 or even 2014 database. By the way, if you really wants to understand Java database programming from scratch, I suggest you to read the book, Practical database programming with Java book by Ying Bai. This book explains every important detail of Java database connectivity and JDBC with simple example.

Friday, July 3, 2015

How to Convert Fahrenheit to Celsius in Java with Example

In this Java tutorial, you will learn how to write a program to convert Fahrenheit to Celsius in Java. Fahrenheit is a thermodynamic temperature scale, where the freezing point of water is 32 degrees Fahrenheit (°F) and the boiling point of water is 212°F (at standard atmospheric pressure). This puts the boiling and freezing points of water exactly 180 degrees apart. Therefore, a degree on the Fahrenheit scale is 1/180 of the interval between the freezing point and the boiling point of water. Absolute zero is defined as -459.67°F. If you know, in Celsius scale, freezing point of water is at 0ºC and boiling point of water is at 100ºC. By using these fact, you can easily deduce a formula to convert Fahrenheit temperature into Celsius. To be frank, if you have been a Science student you already know about that formula, nothing new about it. What is more important is to learn how to convert such formula into a computer program using Java programming language. This is what you will learn in this tutorial. BTW, if you an absolute Java beginner and learning Java, I suggest you to take a look at couple of introductory books about Java programming language e.g. Java: A Beginner's guide by Herbert Schildt, its one of the up-to-date book in Java and covers even latest Java release, Java 8. If you are one who like to try couple of books before settling it for one then you can also check here for couple of more recommended books for Java beginners.

Thursday, July 2, 2015

How to solve java.lang.UnsatisfiedLinkError: no ocijdbc11 in Java

"java.lang.unsatisfiedlinkerror no ocijdbc11 in java.library.path" error comes when you try to connect to Oracle 11g database using OCI (thick) driver by using tns name, but ocijdbc11.dll file is not available in PATH or java.library.path environment variable. ocijdbc11.dll is a native library and if you know Java searches native library in PATH or a location specified by java.library.path system property, if it doesn't find the dll, then it throws java.lang.unsatisfiedlinkerror no ocijdbc11 in java.library.path error. This dll is usually find in C:\Programs\Oracle\ora11g\bin\ocijdbc11.dll, but it could vary depending upon your Oracle installation. First step to solve this error is search for this dll in your machine, if you are using windows, just search in your C drive. If the dll is not there then you need to download that from Oracle's website. If its already present then just add its location into PATH variable. If you are explicitly providing java.library.path then also provide it there e.g.
-Djava.library.path=C:\Programs\Oracle\ora11g\bin\ocijdbc11.dll . By the way, if you truly wants to learn Java database programming from scratch, I suggest you to take a look at Practical database programming with Java book by Ying Bai. This book explains every important detail of Java database connectivity.