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();
}
}
Start Free Trial