fholmx
asked on
java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
I am trying to connect to a DB in java using MSSQL server. I have followed the steps; my CLASSPATH environment variable is currently set to:
CLASSPATH=.;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;c:\pro gram files\Microsoft SQL Server 2000 Driver for JDBC\lib\msutil.jar;c:\pro gram files\Microsoft SQL Server 2000 Driver for JDBC\lib\mssqlserver.jar
To verify that I have set the variable correctly I went to the command prompt and typed:
javap com.microsoft.jdbc.sqlserv er.SQLServ erDriver
and got:
No sourcepublic class com.microsoft.jdbc.sqlserv er.SQLServ erDriver extends com.microsoft.jdbc.base.Ba seDriver {
static {};
public com.microsoft.jdbc.sqlserv er.SQLServ erDriver() ;
public static void main(java.lang.String[]);
}
as a response which should indicate that my CLASSPATH is set up correctly. In spite of this I am still getting a:
java.lang.ClassNotFoundExc eption: com.microsoft.jdbc.sqlserv er.SQLServ erDriver
at java.net.URLClassLoader$1. run(URLCla ssLoader.j ava:198)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at java.net.URLClassLoader.fi ndClass(UR LClassLoad er.java:18 6)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:299)
at sun.misc.Launcher$AppClass Loader.loa dClass(Lau ncher.java :265)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:255)
at java.lang.ClassLoader.load ClassInter nal(ClassL oader.java :315)
at java.lang.Class.forName0(N ative Method)
at java.lang.Class.forName(Cl ass.java:1 40)
at DBConnect.getConnection(DB Connect.ja va:30)
at DBConnect.displayDbPropert ies(DBConn ect.java:4 8)
at DBConnect.main(DBConnect.j ava:85)
I have tried copying the mssqlserver.jar, msutil.jar and msbase.jar in the lib/ directory of my JDK installation folder but that did not help either. Suggestions anyone? Here is my source code:
import java.*;
public class DBConnect
{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver: //";
private final String serverName= "localhost";
private final String portNumber = "1433";
private final String databaseName= "avato";
private final String userName = "sa";
private final String password = "";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";
// Constructor
public DBConnect(){}
private String getConnectionUrl()
{
return url+serverName+":"+portNum ber+";data baseName=" +databaseN ame+";sele ctMethod=" +selectMet hod+";";
}
private java.sql.Connection getConnection()
{
try{
Class.forName("com.microso ft.jdbc.sq lserver.SQ LServerDri ver");
con = java.sql.DriverManager.get Connection (getConnec tionUrl(), userName,p assword);
if(con!=null) System.out.println("Connec tion Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
/*
Display the driver properties, database details
*/
public void displayDbProperties(){
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try{
con= this.getConnection();
if(con!=null){
dm = con.getMetaData();
System.out.println("Driver Information");
System.out.println("\tDriv er Name: "+ dm.getDriverName());
System.out.println("\tDriv er Version: "+ dm.getDriverVersion ());
System.out.println("\nData base Information ");
System.out.println("\tData base Name: "+ dm.getDatabaseProductName( ));
System.out.println("\tData base Version: "+ dm.getDatabaseProductVersi on());
System.out.println("Avalil able Catalogs ");
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcata log: "+ rs.getString(1));
}
rs.close();
rs = null;
closeConnection();
}else System.out.println("Error: No active Connection");
}catch(Exception e)
{
e.printStackTrace();
}
dm = null;
}
private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
DBConnect myDbTest = new DBConnect();
myDbTest.displayDbProperti es();
}
}
CLASSPATH=.;c:\program files\Microsoft SQL Server 2000 Driver for JDBC\lib\msbase.jar;c:\pro
To verify that I have set the variable correctly I went to the command prompt and typed:
javap com.microsoft.jdbc.sqlserv
and got:
No sourcepublic class com.microsoft.jdbc.sqlserv
static {};
public com.microsoft.jdbc.sqlserv
public static void main(java.lang.String[]);
}
as a response which should indicate that my CLASSPATH is set up correctly. In spite of this I am still getting a:
java.lang.ClassNotFoundExc
at java.net.URLClassLoader$1.
at java.security.AccessContro
at java.net.URLClassLoader.fi
at java.lang.ClassLoader.load
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.ClassLoader.load
at java.lang.Class.forName0(N
at java.lang.Class.forName(Cl
at DBConnect.getConnection(DB
at DBConnect.displayDbPropert
at DBConnect.main(DBConnect.j
I have tried copying the mssqlserver.jar, msutil.jar and msbase.jar in the lib/ directory of my JDK installation folder but that did not help either. Suggestions anyone? Here is my source code:
import java.*;
public class DBConnect
{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver:
private final String serverName= "localhost";
private final String portNumber = "1433";
private final String databaseName= "avato";
private final String userName = "sa";
private final String password = "";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";
// Constructor
public DBConnect(){}
private String getConnectionUrl()
{
return url+serverName+":"+portNum
}
private java.sql.Connection getConnection()
{
try{
Class.forName("com.microso
con = java.sql.DriverManager.get
if(con!=null) System.out.println("Connec
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
/*
Display the driver properties, database details
*/
public void displayDbProperties(){
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try{
con= this.getConnection();
if(con!=null){
dm = con.getMetaData();
System.out.println("Driver
System.out.println("\tDriv
System.out.println("\tDriv
System.out.println("\nData
System.out.println("\tData
System.out.println("\tData
System.out.println("Avalil
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcata
}
rs.close();
rs = null;
closeConnection();
}else System.out.println("Error:
}catch(Exception e)
{
e.printStackTrace();
}
dm = null;
}
private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
DBConnect myDbTest = new DBConnect();
myDbTest.displayDbProperti
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Put the jars in *all* the lib\ext directories
CEHJ: Which other lib\ext directories do you mean?
I can only see one other on my system and I don't think it would be an appropriate one.
Just asking ;-)
I can only see one other on my system and I don't think it would be an appropriate one.
Just asking ;-)
ASKER
TimYates,
I tried the path thing but I could not find the ^%$! dos equivilant directory for my SQL JDBC driver installation. Either way I don't think the CLASSPATH was the problem as my javap command executed successfully.
I tried putting the .JAR files in the jre/lib/ext and that solved the problem. What is astounding is that I followed microsofts instructions, line by line, but there was no mention AT ALL with regard to displacing the .JAR files into the jre/lib/ext.
Now its on to the next bug as I am getting a lovely:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
Let me know if any of you know anything about connecting to a DB using MS SQL and I'll officially post this question for some easy points. Thanks for the help guys!
I tried the path thing but I could not find the ^%$! dos equivilant directory for my SQL JDBC driver installation. Either way I don't think the CLASSPATH was the problem as my javap command executed successfully.
I tried putting the .JAR files in the jre/lib/ext and that solved the problem. What is astounding is that I followed microsofts instructions, line by line, but there was no mention AT ALL with regard to displacing the .JAR files into the jre/lib/ext.
Now its on to the next bug as I am getting a lovely:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
Let me know if any of you know anything about connecting to a DB using MS SQL and I'll officially post this question for some easy points. Thanks for the help guys!
I am do the exact programming to test my Ms Sql server with JDBC connection, and I am having the following errors:
at sun.misc.Launcher$AppClass Loader.loa dClass(Lau ncher.java :274)
at java.lang.ClassLoader.load Class(Clas sLoader.ja va:235)
at java.lang.ClassLoader.load ClassInter nal(ClassL oader.java :302)
at java.lang.Class.forName0(N ative Method)
at java.lang.Class.forName(Cl ass.java:1 41)
at examples.Connect.getConnec tion(Conne ct.java:42 )
at examples.Connect.displayDb Properties (Connect.j ava:60)
at examples.Connect.main(Conn ect.java:9 6)
Error Trace in getConnection() : com.microsoft.jdbc.sqlserv er.SQLServ erDriver
Error: No active Connection
Here is my code, I need to get working on project that involves reading from Sql Server.
package examples;
import java.sql.*;
import java.*;
import java.util.*;
import javax.sql.*;
public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver/ /";
private final String serverName= "local";
private final String portNumber = "1433";
private final String databaseName= "university";
private final String userName = "joe";
private final String password = "abbey";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";
// Constructor
public Connect(){}
private String getConnectionUrl(){
return url+serverName+":"+portNum ber+";data baseName=" +databaseN ame+";sele ctMethod=" +selectMet hod+";";
}
private java.sql.Connection getConnection(){
try{
Class.forName("com.microso ft.jdbc.sq lserver.SQ LServerDri ver");
con = java.sql.DriverManager.get Connection (getConnec tionUrl(), userName,p assword);
if(con!=null) System.out.println("Connec tion Successful!");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
/*
Display the driver properties, database details
*/
public void displayDbProperties(){
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try{
con= this.getConnection();
if(con!=null){
dm = con.getMetaData();
System.out.println("Driver Information");
System.out.println("\tDriv er Name: "+ dm.getDriverName());
System.out.println("\tDriv er Version: "+ dm.getDriverVersion ());
System.out.println("\nData base Information ");
System.out.println("\tData base Name: "+ dm.getDatabaseProductName( ));
System.out.println("\tData base Version: "+ dm.getDatabaseProductVersi on());
System.out.println("Avalil able Catalogs ");
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcata log: "+ rs.getString(1));
}
rs.close();
rs = null;
closeConnection();
}else System.out.println("Error: No active Connection");
}catch(Exception e){
e.printStackTrace();
}
dm=null;
}
private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
myDbTest.displayDbProperti es();
}
}
at sun.misc.Launcher$AppClass
at java.lang.ClassLoader.load
at java.lang.ClassLoader.load
at java.lang.Class.forName0(N
at java.lang.Class.forName(Cl
at examples.Connect.getConnec
at examples.Connect.displayDb
at examples.Connect.main(Conn
Error Trace in getConnection() : com.microsoft.jdbc.sqlserv
Error: No active Connection
Here is my code, I need to get working on project that involves reading from Sql Server.
package examples;
import java.sql.*;
import java.*;
import java.util.*;
import javax.sql.*;
public class Connect{
private java.sql.Connection con = null;
private final String url = "jdbc:microsoft:sqlserver/
private final String serverName= "local";
private final String portNumber = "1433";
private final String databaseName= "university";
private final String userName = "joe";
private final String password = "abbey";
// Informs the driver to use server a side-cursor,
// which permits more than one active statement
// on a connection.
private final String selectMethod = "cursor";
// Constructor
public Connect(){}
private String getConnectionUrl(){
return url+serverName+":"+portNum
}
private java.sql.Connection getConnection(){
try{
Class.forName("com.microso
con = java.sql.DriverManager.get
if(con!=null) System.out.println("Connec
}catch(Exception e){
e.printStackTrace();
System.out.println("Error Trace in getConnection() : " + e.getMessage());
}
return con;
}
/*
Display the driver properties, database details
*/
public void displayDbProperties(){
java.sql.DatabaseMetaData dm = null;
java.sql.ResultSet rs = null;
try{
con= this.getConnection();
if(con!=null){
dm = con.getMetaData();
System.out.println("Driver
System.out.println("\tDriv
System.out.println("\tDriv
System.out.println("\nData
System.out.println("\tData
System.out.println("\tData
System.out.println("Avalil
rs = dm.getCatalogs();
while(rs.next()){
System.out.println("\tcata
}
rs.close();
rs = null;
closeConnection();
}else System.out.println("Error:
}catch(Exception e){
e.printStackTrace();
}
dm=null;
}
private void closeConnection(){
try{
if(con!=null)
con.close();
con=null;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception
{
Connect myDbTest = new Connect();
myDbTest.displayDbProperti
}
}
You are probably beset putting this in a seperate question rather than the bottom of this one :-)
Good luck!
Tim
Good luck!
Tim
beset == best
:-/
:-/
I don't think java likes having spaces on the classpath :-/
can you try:
CLASSPATH=.;c:\progra~1\Mi
assuming
c:\progra~1\Micros~1
does get you to the right directory...