You need to find an appropriate JDBC driver to be able to connect to Microsoft SQL Server using JDBC. Following are the preferred drivers for SQL Server:

  • jTDSis an open source JDBC 3.0 driver for Microsoft SQL Server (6.5, 7, 2000 and 2005). Place jar file into your application classpath. java.sql package along with above driver helps connecting to database.
  • Microsoft SQL Server 2000 Driver for JDBC is a Type 4 JDBC driver. You need to place the jar files in your CLASSPATH variable.

The example below shows how to make a connection to Microsoft SQL Server 2000 using jTDS driver:

 import java.sql.*;

public class testConnection
{
    public static void main(String[] args) 
    {
        DB db = new DB();
        db.dbConnect(
     "jdbc:jtds:sqlserver://localhost:1433/tempdb","sa","");
    }
}

class DB
{
    public DB() {}

    public voidn dbConnect(String db_connect_string, 
  String db_userid, String db_password)
    {
        try
        {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
    db_connect_string, db_userid, db_password);
            System.out.println("connected");
            
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
};