azure-docs-sdk-java/docs-ref-services/sql.md

3.3 KiB

title description keywords author ms.author manager ms.date ms.topic ms.prod ms.technology ms.devlang ms.service
Azure SQL Database libraries for Java Connect to Azure SQL database using the JDBC driver or management Azure SQL database instances with the management API. Azure, Java, SDK, API, SQL, database , JDBC rloutlaw routlaw douge 07/05/2017 reference azure azure java sql-database

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>

[!div class="nextstepaction"] Explore the Management APIs

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

[!INCLUDE java-sql-samples]

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