Read data from Azure SQL Server using Java

Here’s a sample code snippet that demonstrates how to read data from an Azure SQL Server database using Java and JDBC (Java Database Connectivity):

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class AzureSQLDataReader {
    public static void main(String[] args) {
        // Database credentials
        String url = "jdbc:sqlserver://your-server.database.windows.net:1433;databaseName=your-database";
        String username = "your-username";
        String password = "your-password";

        // SQL query
        String query = "SELECT * FROM your-table";

        try {
            // Register JDBC driver
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            // Open a connection
            Connection conn = DriverManager.getConnection(url, username, password);

            // Create a statement
            Statement stmt = conn.createStatement();

            // Execute the query
            ResultSet rs = stmt.executeQuery(query);

            // Process the result set
            while (rs.next()) {
                // Retrieve the data by column name
                String id = rs.getString("id");
                String name = rs.getString("name");
                String email = rs.getString("email");

                // Do something with the data
                System.out.println("ID: " + id);
                System.out.println("Name: " + name);
                System.out.println("Email: " + email);
                System.out.println("--------------------------");
            }

            // Close the resources
            rs.close();
            stmt.close();
            conn.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Make sure to replace the url, username, password, your-database, your-table, and column names with your actual Azure SQL Server connection details and query.

This code snippet assumes you have the Azure SQL Server JDBC driver (e.g., mssql-jdbc.jar) included in your classpath. If not, you can download it from the official Microsoft website or include it as a Maven/Gradle dependency.

The code connects to the Azure SQL Server database, creates a statement, executes the SQL query using executeQuery(), and processes the result set using next() to iterate over the rows and getString() (or appropriate getter methods) to retrieve the column values. Finally, it closes the resources to free up memory.

Remember to handle exceptions appropriately in your production code and consider using try-with-resources or a similar mechanism to automatically close the resources.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech