himanshu jain
himanshu jain

Reputation: 3

Attempt to connect to SQL Server causes ClassNotFoundException

import java.sql.*;

public class MysqlConnect {
    public static void main(String[] args) {
        System.out.println("MySQL Connect Example.");
        Connection conn = null;
        String url = "\\host/context/";
        String dbName = "theDatabaseName";
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver ";
        String userName = "theUserName";
        String password = "thePassword";
        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url + dbName, userName, password);
            System.out.println("Connected to the database");
            conn.close();
            System.out.println("Disconnected from database");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I am facing problem while the running this code.

I have already downloaded the SQL Server driver (sqljdbc), set it in the class path and copied it into the java/lib directory, but still I am getting the same result: ClassNotFoundException.

Can anybody help me?

Upvotes: 0

Views: 1469

Answers (3)

Nishan
Nishan

Reputation: 2871

You don't have 'com.microsoft.sqlserver.jdbc.SQLServerDriver' class in your classpath.

Make sure you have following jars in your CLASSPATH: Msbase.jar, Msutil.jar, Mssqlserver.jar

More details here: http://support.microsoft.com/kb/313100

Upvotes: 0

Santosh
Santosh

Reputation: 17893

Please check the JDBC URL. The one mentioned by you \\xeon-s5/LDF RAID (G)/ does not seem to be correct URL. A Typical URL will look like jdbc:sqlserver://neptune.acme.com:1433. Check out this further. Here how to connect to SQL server from java.

Upvotes: 1

mcfinnigan
mcfinnigan

Reputation: 11638

You have a trailing space at the end of your class name.

"com.microsoft.sqlserver.jdbc.SQLServerDriver "

should most likely be

"com.microsoft.sqlserver.jdbc.SQLServerDriver"

Upvotes: 0

Related Questions