Azure SQL Database libraries for Java

Overview

Azure SQL Database is a relational database service using the Microsoft SQL Server engine that supports table, JSON, spatial, and XML data.

To get started with Azure SQL Database, see Azure SQL Database: Use Java to connect and query data.

Client JDBC driver

Connect to Azure SQL Database from your applications using the SQL Database JDBC driver. You can use the Java JDBC API to directly connect with the database or use data access frameworks that interact with the database through JDBC such as Hibernate.

Add a dependency to your Maven pom.xml file to use the client JDBC driver in your project.

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>6.2.1.jre8</version>
</dependency>

Example

Connect to SQL database and select all records in a table using JDBC.

String connectionString = "jdbc:sqlserver://fabrikam.database.windows.net:1433;database=fiber;user=raisa;password=testpass;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
try {
    Connection conn = DriverManager.getConnection(connectionString);
    Statement statement = conn.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT * FROM SALES");
}  

Management API

Create and manage Azure SQL Database resources in your subscription with the management API.

Add a dependency to your Maven pom.xml file to use the management API in your project.

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-mgmt-sql</artifactId>
    <version>1.3.0</version>
</dependency>

Example

Create a SQL Database resource and restrict access to a range of IP addresses using a firewall rule.

SqlServer sqlServer = azure.sqlServers().define(sqlDbName)
                    .withRegion(Region.US_EAST)
                    .withNewResourceGroup(resourceGroupName)
                    .withAdministratorLogin(administratorLogin)
                    .withAdministratorPassword(administratorPassword)
                    .withNewFirewallRule("172.16.0.0", "172.31.255.255")
                    .create();

Samples

Configure Azure SQL Database with the management API
Deploy WordPress powered by SQL Database
Scale SQL Database across multiple Azure regions
Manage shared SQL database workloads with elastic pools

Explore more sample Java code for Azure SQL Database you can use in your apps.