A Java JDBC SQL Server Driver class and URL example

Here's a quick post to help anyone that needs a quick JDBC Driver and URL reference when using Microsoft SQL Server with Java and JDBC.

The basic SQL Server JDBC Driver and URL information you need is shown here:

SQL Server URL String:  
jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE

SQL Server JDBC Driver String:
com.microsoft.jdbc.sqlserver.SQLServerDriver

A SQL Server JDBC Driver and URL connection example

It may also help to see this SQL Server JDBC information used in a very simple Java application. To that end, here's a simple Java JDBC SQL Server example that shows how to use the SQL Server JDBC Driver and URL to establish a Java database connection:

public class JdbcSQLServerDriverUrlExample
{
  public static void main(String[] args)
  {
    Connection connection = null;
    try
    {
      // the sql server driver string
      Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    
      // the sql server url
      String url = "jdbc:microsoft:sqlserver://HOST:1433;DatabaseName=DATABASE";
      
      // get the sql server database connection
      connection = DriverManager.getConnection(url,"THE_USER", "THE_PASSWORD");
      
      // now do whatever you want to do with the connection
      // ...
      
    }
    catch (ClassNotFoundException e)
    {
      e.printStackTrace();
      System.exit(1);
    }
    catch (SQLException e)
    {
      e.printStackTrace();
      System.exit(2);
    }
  }
}

I hope this simple SQL Server JDBC reference is helpful. There are also many other Java, JDBC, and SQL Server tutorials on this site. Just use our search form to find many other examples.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.