0% found this document useful (0 votes)
193 views

Connect To Microsoft SQL Server Via JDBC

This document describes how to connect to a Microsoft SQL Server database from a Java application using JDBC. It explains how to download the Microsoft JDBC driver, set up the connection URL with parameters like server name and port, register the driver, and provide examples to connect using Windows or SQL authentication. The examples demonstrate getting database metadata like the driver name, version, and product information by connecting to a local SQL Server Express instance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views

Connect To Microsoft SQL Server Via JDBC

This document describes how to connect to a Microsoft SQL Server database from a Java application using JDBC. It explains how to download the Microsoft JDBC driver, set up the connection URL with parameters like server name and port, register the driver, and provide examples to connect using Windows or SQL authentication. The examples demonstrate getting database metadata like the driver name, version, and product information by connecting to a local SQL Server Express instance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Connect to Microsoft SQL Server via JDBC https://www.codejava.net/java-se/jdbc/connect-to-microsoft-sql-server-via...

Connect to Microsoft SQL Server via JDBC

Details
Last Updated on 07 August 2017 | Print Email

Table of content:

1. 1. Download Microsoft JDBC driver


2. JDBC database URL for SQL Server
3. Register JDBC driver for SQL Server and establish connection
4. Example program

This article describes how to get JDBC driver and write code for making database connection to Microsoft SQL Server
from a Java client. Suppose you have a light weight version of SQL Server installed, such as Microsoft SQL Server 2012
Express.Click here to download Microsoft JDBC Driver 4.0 for SQL Server which supports:

SQL Server versions: 2005, 2008, 2008 R2, and 2012.


JDK version: 5.0 and 6.0.

Run the downloaded program sqljdbc_<version>_<language>.exe. It will extract the files into a specified directory
(default is Microsoft JDBC Driver 4.0 for SQL Server). You will find two jar files sqljdbc.jar (for JDBC 3.0) and
sqljdbc4.jar (for JDBC 4.0), plus some .dll files and HTML help files.Place the sqljdbc.jarfile under your application’s
classpath if you are using JDK 5.0 or sqljdbc4.jar file if you are using JDK 6.0 or later. Recommended Book: Introducing
Microsoft SQL Server 2012The syntax of database URL for SQL Server is as
follows:jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]Where:

serverName: host name or IP address of the machine on which SQL server is running.
instanceName: name of the instance to connect to on serverName. The default instance is used if this
parameter is not specified.
portNumber: port number of SQL server, default is 1433. If this parameter is missing, the default port is used.
property=value: specify one or more additional connection properties. To see the properties specific to SQL
server, visit Setting the Connection Properties.

NOTE:SQL Server has two authentication modes:

Windows authentication: using current Windows user account to log on SQL Server. This mode is for the case
both the client and the SQL server are running on the same machine. We specify this mode by adding the
property integratedSecurity=true to the URL.
SQL Server authentication: using a SQL Server account to authenticate. We have to specify username and
password explicitly for this mode.

Following are some examples:

- Connect to default instance of SQL server running on the same machine as the JDBC client, using Windows
authentication:

jdbc:sqlserver://localhost;integratedSecurity=true;

- Connect to an instance named sqlexpress on the host dbServer, using SQL Server authentication:

jdbc:sqlserver://dbHost\sqlexpress;user=sa;password=secret

- Connect to a named database testdb on localhost using Windows authentication:

jdbc:sqlserver://localhost:1433;databaseName=testdb;integratedSecurity=true;

Recommended Book: Microsoft SQL Server 2012 T-SQL Fundamentals

The JDBC driver class of SQL Server is com.microsoft.sqlserver.jdbc.SQLServerDriver, so to register this driver, use the
following statement:
DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());

Or:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

However, that is not required since JDBC 4.0 (JDK 6.0) because the driver manager can detect and load the driver class
automatically as long as the sqljdbc4.jar is present in the classpath.To make a connection, call the method getConnection()
of the DriverManager class. Here is a code snippet that connects the user sa with password secret to the instance
sqlexpress on localhost:
String dbURL = "jdbc:sqlserver://localhost\\sqlexpress;user=sa;password=secret";
Connection conn = DriverManager.getConnection(dbURL);
if (conn != null) {
System.out.println("Connected");

1 of 3 4/18/19, 10:05 AM
Connect to Microsoft SQL Server via JDBC https://www.codejava.net/java-se/jdbc/connect-to-microsoft-sql-server-via...

The following code passes username and password as arguments to the method getConnection():
String dbURL = "jdbc:sqlserver://localhost\\sqlexpress";
String user = "sa";
String pass = "secret";
conn = DriverManager.getConnection(dbURL, user, pass);

We can also use a java.util.Properties object to store connection properties, as in the following example:
String dbURL = "jdbc:sqlserver://localhost\\sqlexpress";
Properties properties = new Properties();
properties.put("user", "sa");
properties.put("password", "secret");
conn = DriverManager.getConnection(dbURL, properties);

NOTE: if you want to use Windows authentication mode (integratedSecurity=true), you must have the sqljdbc_auth.dll
in the classpath. To demonstrate, we create a small program that connects to an SQL Server instance on localhost and
print out some database information as follows:
package net.codejava.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
* This program demonstrates how to establish database connection to Microsoft
* SQL Server.
* @author www.codejava.net
*
*/
public class JdbcSQLServerConnection {

public static void main(String[] args) {

Connection conn = null;

try {

String dbURL = "jdbc:sqlserver://localhost\\sqlexpress";


String user = "sa";
String pass = "secret";
conn = DriverManager.getConnection(dbURL, user, pass);
if (conn != null) {
DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
System.out.println("Driver name: " + dm.getDriverName());
System.out.println("Driver version: " + dm.getDriverVersion());
System.out.println("Product name: " + dm.getDatabaseProductName());
System.out.println("Product version: " + dm.getDatabaseProductVersion());
}

} catch (SQLException ex) {


ex.printStackTrace();
} finally {
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}

The program would produce the following output:

Driver name: Microsoft JDBC Driver 4.0 for SQL Server

Driver version: 4.0.2206.100

Product name: Microsoft SQL Server

Product version: 11.00.2100

Related Tutorial:

Connect to a database with JDBC

You may be also interested in:

Connect to MySQL database via JDBC


Connect to Oracle database via JDBC
Connect to PostgreSQL database server via JDBC
Connect to Apache Derby (Java DB) via JDBC
Connect to SQLite via JDBC

2 of 3 4/18/19, 10:05 AM
Connect to Microsoft SQL Server via JDBC https://www.codejava.net/java-se/jdbc/connect-to-microsoft-sql-server-via...

Java connecting to MongoDB database examples

Recommended: Complete Java Masterclass

3 of 3 4/18/19, 10:05 AM

You might also like