Blog Archive
Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts
Wednesday, February 27, 2013
ClassNotFoundException for com.microsoft.sqlserver.jdbc.SQLServerDriver
Check whether you have correct Jar file included in your project.
The Microsoft JDBC Driver for SQL Server provides sqljdbc.jar and sqljdbc4.jar class library files to be used depending on your preferred Java Runtime Environment (JRE) settings. For more information about which JAR file to choose, see System Requirements for the JDBC Driver.
The sqljdbc.jar class library provides support for JDBC 3.0 and the sqljdbc4.jar class library provides support for JDBC 4.0.
For more information, please see:
System Requirements for the JDBC Driver
http://msdn.microsoft.com/en-us/library/ms378422.aspx
Using the JDBC Driver
http://msdn.microsoft.com/en-us/library/ms378526.aspx
You can download the sqljdbc4.jar, here : Microsoft JDBC Driver for SQL Server
ClassNotFoundException for com.microsoft.jdbc.sqlserver.SQLServerDriver
If you got this error, probably you are using wrong reference.
com.microsoft.jdbc.sqlserver.SQLServerDriver is used for Microsoft 2000 SQL SERVER.
For 2005 SQL SERVER and grester version, you have use "com.microsoft.sqlserver.jdbc.SQLServerDriver".
Note the change: from "microsoft.jdbc.sqlserver" to "microsoft.sqlserver.jdbc"
And also for SQL SERVER 2005, it has different URL prefix, The SQL Server 2000 JDBC driver uses an URL prefix of "jdbc:microsoft:sqlserver://", while the SQL Server 2005 JDBC driver uses an URL prefix of "jdbc:sqlserver://".
Note the removal of "microsoft" from the URL prefix.
For more information about the connection string properties, you can refer : http://msdn2.microsoft.com/en-us/library/ms378428(SQL.90).aspx
JDBC Connection for SQL SERVER
Create two class "JDBCConnector" and "TestConnection" in same package. Replace the values YourServerNameorIp, YourDBInstanceName, YourDBUserName and YourDBPassword with Appropriate values.
This sample code works fine with SQL SERVER 2008,
Kindly let me know if it is not working any of the sql server version.
JDBCConnector.Java
import java.sql.Connection;
import java.sql.DriverManager;
public class JDBCConnector {
public JDBCConnector()
{
MakeConnection("YourServerNameorIp",
"YourDBInstanceName", "YourDBUserName", "YourDBPassword");
}
private void MakeConnection(String strServerIPAddress,
String strDatabaseName, String strUserName, String strPassword)
{
if(openConnection(strServerIPAddress,
strDatabaseName, strUserName, strPassword))
{
System.out.println("Test Connection Successful");
}
else
{
System.out.println("Test Connection not succesful");
}
}
private boolean openConnection(String strServerIPAddress,
String strDatabaseName, String strUserName, String strPassword) {
//String connect to SQL server
String url = "jdbc:sqlserver://" + strServerIPAddress + ":1433" +
";DatabaseName=" + strDatabaseName;
try
{
//Loading the driver...
Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
}
catch( java.lang.ClassNotFoundException e )
{
System.out.println("ClassNotFoundException");
return false;
}
try
{
//Building a Connection
Connection m_Conn = DriverManager.getConnection(url, strUserName, strPassword);
if(m_Conn == null )
return false;
}
catch( java.sql.SQLException e )
{
System.out.println("Connection error");
return false;
}
return true;
}
}
TestConnection.Java
public class TestConnection
{
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
JDBCConnector conn = new JDBCConnector();
}
catch (Exception ex)
{
System.out.println("base class not found");
}
}
}
Subscribe to:
Posts (Atom)