Reputation: 149
I am trying to connect Microsoft SQL server 2005 in JAVA using JDBC but getting following Exception. My project is not running in Eclipse it is saying "Requested resource not found" (Done all server configurations and also adding and removing project from server) and when I run it from console it shows me "Class not found exception" I've put JARs by using build path and also put in WEB-INF/lib. but not getting connection following is my code.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
/**
* @author www.javaworkspace.com
*
*/
public class SMSdao {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager
.getConnection(
"jdbc:sqlserver://192.128.10.17:1433;databaseName=TEK_R;selectMethod=cursor",
"sa", "sai@123");
System.out.println("DATABASE NAME IS:"
+ connection.getMetaData().getDatabaseProductName());
Statement statement = connection.createStatement();
ResultSet resultSet = statement
.executeQuery("SELECT * FROM dbo.SMS_OUT");
while (resultSet.next()) {
System.out.println("Message:"
+ resultSet.getString("MessageIn"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
AND the stack trcae is:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at SMSdao.main(SMSdao.java:13)
Tried Everything and done all the things whatever i found on web. I am using jdk 7 and tomcat 6 on windows 7.
Upvotes: 0
Views: 3169
Reputation: 2071
I would also suggest using jTDS instead: http://jtds.sourceforge.net/
Historically microsoft's JDBC drivers have failed to fully implement important and useful parts of the JDBC spec. jTDS should be more fully featured and is open source.
Upvotes: 0
Reputation: 1902
You need to download the SQLServerDriver JDBC from microsoft. The Class.forName is searching for the Class name you specified and it can't find it in your classpath.
http://msdn.microsoft.com/en-us/sqlserver/aa937724
Just put the jar in your project classpath (tomcat's shared lib is better).
Upvotes: 2