java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

Reply

Join Date: Dec 2004
Posts: 116
Reputation: stackOverflow is an unknown quantity at this point 
Solved Threads: 0
stackOverflow's Avatar
stackOverflow stackOverflow is offline Offline
Junior Poster

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

 
0
  #1
Sep 29th, 2008
Hi All,

I'm trying to use jdbc connection with sqlServer 2000. I'm using j2sdk1.4.2. And i have service pack2.

I've installed the .jar files mssqlserver.jar, msutil.jar, msbase.jar.

I have these jar files in E:\j2sdk1.4.2_04\lib.

I'm trying to connect with the following program.

  1. import java.*;
  2. import java.io.*;
  3.  
  4. public class Connect{
  5. private java.sql.Connection con = null;
  6. private final String url = "jdbc:microsoft:sqlserver://";
  7. private final String serverName= "localhost";
  8. private final String portNumber = "1433";
  9. private final String databaseName= "master";
  10. private final String userName = "Z";
  11. private final String password = "";
  12. // Informs the driver to use server a side-cursor,
  13. // which permits more than one active statement
  14. // on a connection.
  15. private final String selectMethod = "cursor";
  16.  
  17. // Constructor
  18. public Connect(){}
  19.  
  20. private String getConnectionUrl(){
  21. return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
  22. }
  23.  
  24. private java.sql.Connection getConnection(){
  25. try{
  26. // Driver d = (Driver)Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
  27. Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
  28. con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
  29. if(con!=null) System.out.println("Connection Successful!");
  30. }catch(Exception e){
  31. e.printStackTrace();
  32. System.out.println("Error Trace in getConnection() : " + e.getMessage());
  33. }
  34. return con;
  35. }
  36.  
  37. /*
  38.   Display the driver properties, database details
  39.   */
  40.  
  41. public void displayDbProperties(){
  42. java.sql.DatabaseMetaData dm = null;
  43. java.sql.ResultSet rs = null;
  44. try{
  45. con= this.getConnection();
  46. if(con!=null){
  47. dm = con.getMetaData();
  48. System.out.println("Driver Information");
  49. System.out.println("\tDriver Name: "+ dm.getDriverName());
  50. System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
  51. System.out.println("\nDatabase Information ");
  52. System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
  53. System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
  54. System.out.println("Avalilable Catalogs ");
  55. rs = dm.getCatalogs();
  56. while(rs.next()){
  57. System.out.println("\tcatalog: "+ rs.getString(1));
  58. }
  59. rs.close();
  60. rs = null;
  61. closeConnection();
  62. }else System.out.println("Error: No active Connection");
  63. }catch(Exception e){
  64. e.printStackTrace();
  65. }
  66. dm=null;
  67. }
  68.  
  69. private void closeConnection(){
  70. try{
  71. if(con!=null)
  72. con.close();
  73. con=null;
  74. }catch(Exception e){
  75. e.printStackTrace();
  76. }
  77. }
  78. public static void main(String[] args) throws Exception
  79. {
  80. Connect myDbTest = new Connect();
  81. myDbTest.displayDbProperties();
  82. }
  83. }

when i compile i'm not getting any error...
but when i run it .. i'm getting this error...

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Connect.getConnection(Connect.java:28)
at Connect.displayDbProperties(Connect.java:46)
at Connect.main(Connect.java:82)
Error Trace in getConnection() : com.microsoft.jdbc.sqlserver.SQLServerDriver
Error: No active Connection

I'd be greatful if anyone can help me come out of this
Last edited by stackOverflow; Sep 29th, 2008 at 2:08 am.
Imagination was given to man to compensate him for what he is not; A sense of humour to console him for what he is..
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,359
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

 
0
  #2
Sep 29th, 2008
Include the driver jarfile on your classpath.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 116
Reputation: stackOverflow is an unknown quantity at this point 
Solved Threads: 0
stackOverflow's Avatar
stackOverflow stackOverflow is offline Offline
Junior Poster

Re: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

 
0
  #3
Sep 29th, 2008
do u mean setting up of system variable CLASSPATH?? ... if so, i've done it.
Imagination was given to man to compensate him for what he is not; A sense of humour to console him for what he is..
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,359
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

 
0
  #4
Sep 29th, 2008
If so, then not correctly, or you misspelled the class or package name.

A ClassNotFoundException will only occur in one of three instances:

1. The Class is not anywhere on the CLASSPATH
2. The Class name is misspelled (which, essentially, falls back to #1)
3. The package "path" is wrong/misspelled (which also, essentially, falls back to #1)

Edit: Also, do not rely on the System CLASSPATH environment variable, containers won't use it. If running from a container, then include the library either in the common/lib or shared/lib (or equivalent depending on the server) or the WEB-INF/lib directory, or include a reference to it in the Manifest file when running from a jar (using -jar) or use the -cp command line option. (When running from an IDE, add it as an external library/jar in the project properties.)
Last edited by masijade; Sep 29th, 2008 at 5:03 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: ncaissie is an unknown quantity at this point 
Solved Threads: 0
ncaissie ncaissie is offline Offline
Newbie Poster

Re: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

 
0
  #5
Feb 8th, 2009
Hi,

I have my class path setup and i'm still getting this errror.
I am using eclips and jsp.

here is a paste of my class path:
C:\Program Files\Java\jre6\lib;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;.

and my install directory:
C:\Program Files\Microsoft SQL Server 2000 Driver for JDBC\lib

I did copy them into:
C:\tomcat\webapps\Reporting\WebContent\WEB-INF\lib

my java direcories are:
C:\Program Files\Java\jdk
C:\Program Files\Java\jre6

where Reporting is the project name.

any advice would be great.
thanks.

p.s. I'm on a vista laptop.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: ncaissie is an unknown quantity at this point 
Solved Threads: 0
ncaissie ncaissie is offline Offline
Newbie Poster

Re: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

 
0
  #6
Feb 8th, 2009
I have writen a simple test using the java compiler and I get the this result.

import java.util.*;
import java.text.*;
import java.sql.*;
import java.io.*;
import java.*;

class test
{
public static void main(String args[]){
System.out.print("Start");
try
{


Connection conn = null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://DESKTOP;databasename=cssd_reporting_tool","sa","05330921");
Statement command = conn.createStatement();
String fieldSelectStatement = "SELECT * FROM tblUsers where UserName='nxc018'";
ResultSet rs = command.executeQuery(fieldSelectStatement);
rs.next();
System.out.print("OK");
System.out.print(rs.getString(2));

conn.close();
}
catch(SQLException sqle)
{
System.out.print(sqle.getMessage());
System.out.print("Sql error");

}catch(ClassNotFoundException cnfe){
System.out.print(cnfe.getMessage());
System.out.print("class not found error");

}
System.out.print("finnish");

}
}


C:\>java test
Start[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.Sql errorfinnish
C:\>
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,359
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

 
0
  #7
Feb 8th, 2009
That is not the same error.

For that one, make sure that your SQL Server is setup to respond to TCP IP connection attempts (per default, it is, AFAIK, not).
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: ncaissie is an unknown quantity at this point 
Solved Threads: 0
ncaissie ncaissie is offline Offline
Newbie Poster

Re: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

 
0
  #8
Feb 8th, 2009
i know.
thanks
but i need to figure out the first one.
I believe it has something to do with Eclips.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,359
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

 
0
  #9
Feb 8th, 2009
Reread the last sentence (the one in parenthesis) of reply #4.

Also, reread the part of that same post that says not to rely on the system CLASSPATH environment variable as containers (of which all IDEs are) will not use it.
Last edited by masijade; Feb 8th, 2009 at 7:38 pm.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: ncaissie is an unknown quantity at this point 
Solved Threads: 0
ncaissie ncaissie is offline Offline
Newbie Poster

Re: java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver

 
0
  #10
Feb 8th, 2009
I did, and I put the jar files in the Web-info directory but it didn't work.
that's why i stated i was using eclips.
I was hoping someone knew if there was something i can set in there that would help.

there are many web-inf folders in the tomcat directory for some reason.

thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC