Compartilhar via


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");
}  

Resource Management

Create and manage Azure SQL Database resources in your subscription with the Java resource management libraries.

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

<dependency>
   <groupId>com.azure.resourcemanager</groupId>
   <artifactId>azure-resourcemanager</artifactId>
   <version>2.1.0</version>
</dependency>

For detailed information on how to use the Java resource management libraries, please refer to this doc

Example

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

SqlServer server = azureResourceManager.sqlServers().define(sqlDbName)
        .withRegion(Region.US_EAST)
        .withNewResourceGroup(resourceGroupName)
        .withAdministratorLogin(adminLogin)
        .withAdministratorPassword(adminPass)
        .defineFirewallRule(firewallRuleName)
        .withIpAddressRange("172.16.0.0", "172.31.255.255")
        .attach()
        .create();

Samples

Azure SQL resources

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.