From 4b002df7b3f4e905bc3af6e6e0c81946efe95711 Mon Sep 17 00:00:00 2001 From: Manfred Riem Date: Fri, 17 Jan 2025 10:24:21 -0600 Subject: [PATCH] Fixes #516 - Fix 'Create a SQL command line' example (#531) --- sql/{get-country => cli}/README.md | 29 ++++-- sql/{get-country => cli}/pom.xml | 43 ++++----- .../java/examples/azure/mssql/cli/Cli.java | 95 +++++++++++++++++++ sql/create/README.md | 4 +- .../azure/mssql/getcountry/GetCountry.java | 25 ----- .../README.md | 2 +- sql/pom.xml | 2 +- 7 files changed, 139 insertions(+), 61 deletions(-) rename sql/{get-country => cli}/README.md (58%) rename sql/{get-country => cli}/pom.xml (63%) create mode 100644 sql/cli/src/main/java/examples/azure/mssql/cli/Cli.java delete mode 100644 sql/get-country/src/main/java/examples/azure/mssql/getcountry/GetCountry.java diff --git a/sql/get-country/README.md b/sql/cli/README.md similarity index 58% rename from sql/get-country/README.md rename to sql/cli/README.md index 1acb5d4c..c9ffba58 100644 --- a/sql/get-country/README.md +++ b/sql/cli/README.md @@ -1,12 +1,12 @@ -# Get country information (JDBC client) +# SQL Database JDBC CLI [![sql/get-country/README.md](https://github.com/Azure-Samples/java-on-azure-examples/actions/workflows/sql_get-country_README_md.yml/badge.svg)](https://github.com/Azure-Samples/java-on-azure-examples/actions/workflows/sql_get-country_README_md.yml) ## Prerequisites - + This example assumes you have previously completed the following examples: @@ -14,16 +14,14 @@ This example assumes you have previously completed the following examples: 1. [Create an Azure SQL Database](../create/README.md) 1. [Install curl](https://curl.haxx.se/download.html) 1. [Open Azure SQL server firewall to your IP address](../open-firewall-to-your-ip/README.md) -1. [Install mssql-cli client](https://docs.microsoft.com/en-us/sql/tools/mssql-cli?view=sql-server-ver15) -1. [Load your Azure SQL database with your data](../load-your-mssql-database-with-data/README.md) -## Get country information +## SQL Database JDBC CLI -This example will get country information from the database. +This example will send a SQL statement to the database. @@ -33,11 +31,22 @@ First lets build the example. mvn package ``` -The command line below will get the country information for the country with -the abbreviation 'USA'. +Setup the correct environment variables. ```shell - java -jar target/get-country.jar "jdbc:sqlserver://$MSSQL_DNS_NAME:1433;databaseName=demo;encrypt=true;trustServerCertificate=true" $MSSQL_CLIENT_USERNAME $MSSQL_PASSWORD USA +export MSSQL_DNS_NAME=`az sql server show \ + --resource-group $RESOURCE_GROUP \ + --name $MSSQL_NAME \ + --query fullyQualifiedDomainName \ + --output tsv` + + export MSSQL_CLIENT_USERNAME="$MSSQL_USERNAME@$MSSQL_NAME" +``` + +The command line below will send the "SELECT 1" statement to the database. + +```shell + java -jar target/cli.jar "jdbc:sqlserver://$MSSQL_DNS_NAME:1433;encrypt=true;trustServerCertificate=true" $MSSQL_CLIENT_USERNAME $MSSQL_PASSWORD "SELECT 1" ``` 12.6.1.jre11 @@ -19,7 +19,7 @@ 3.3.0 - get-country + cli org.apache.maven.plugins @@ -31,35 +31,34 @@ org.apache.maven.plugins - maven-dependency-plugin - ${maven-dependency-plugin.version} + maven-shade-plugin + 3.2.4 - copy-dependencies package - copy-dependencies + shade - ${project.build.directory}/lib + + + examples.azure.mssql.cli.Cli + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + - - org.apache.maven.plugins - maven-jar-plugin - ${maven-jar-plugin.version} - - - - true - lib/ - examples.azure.mssql.getcountry.GetCountry - - - - diff --git a/sql/cli/src/main/java/examples/azure/mssql/cli/Cli.java b/sql/cli/src/main/java/examples/azure/mssql/cli/Cli.java new file mode 100644 index 00000000..a2187ef9 --- /dev/null +++ b/sql/cli/src/main/java/examples/azure/mssql/cli/Cli.java @@ -0,0 +1,95 @@ +package examples.azure.mssql.cli; + +import java.io.InputStreamReader; +import java.io.LineNumberReader; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class Cli { + + private Connection connection; + private String[] arguments; + + public Cli(String[] arguments) throws SQLException { + try { + Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); + } catch (ClassNotFoundException e) { + throw new SQLException("SQLServerDriver not found", e); + } + String url = arguments[0]; + if (!url.startsWith("jdbc:")) { + url = "jdbc:sqlserver://" + url; + } + this.connection = DriverManager.getConnection(url, arguments[1], arguments[2]); + this.arguments = arguments; + } + + public static void main(String[] arguments) throws Exception { + Cli cli = new Cli(arguments); + cli.run(); + } + + public void run() throws Exception { + if (arguments.length > 3) { + executeSql(arguments[3]); + } else { + boolean interactive = true; + if (interactive) { + LineNumberReader reader = new LineNumberReader(new InputStreamReader(System.in)); + boolean exit = false; + do { + System.out.print(">> "); + String sql = reader.readLine(); + if ("/exit".equalsIgnoreCase(sql.trim())) { + exit = true; + continue; + } + executeSql(sql); + } while (!exit); + } + } + } + + private void executeSql(String sql) { + try { + Statement statement = connection.createStatement(); + boolean hasResultSet = statement.execute(sql); + if (hasResultSet) { + ResultSet resultSet = statement.getResultSet(); + if (resultSet != null) { + int columnCount = resultSet.getMetaData().getColumnCount(); + for (int i = 1; i <= columnCount; i++) { + System.out.print(resultSet.getMetaData().getColumnName(i) + "\t"); + } + System.out.println(); + + boolean hasResults = false; + while(resultSet.next()) { + hasResults = true; + for (int i = 1; i <= columnCount; i++) { + System.out.print(resultSet.getString(i) + "\t"); + } + System.out.println(); + } + if (!hasResults) { + System.out.println("no results"); + } + } else { + System.out.println("no results"); + } + } else { + int updateCount = statement.getUpdateCount(); + if (updateCount == -1) { + System.out.println("Statement executed successfully."); + } else { + System.out.println("Statement executed successfully, " + updateCount + " rows affected."); + } + } + } catch (SQLException e) { + System.out.println("Error executing statement: " + e.getMessage()); + } + } +} diff --git a/sql/create/README.md b/sql/create/README.md index 315b4ac3..4eea0eb8 100644 --- a/sql/create/README.md +++ b/sql/create/README.md @@ -26,14 +26,14 @@ To create the Azure SQL Database setup the environment variables: ```shell - export MSSQL_NAME=mssql-$RANDOM + export MSSQL_NAME=joazmssql$RANDOM export MSSQL_USERNAME=mssql export MSSQL_PASSWORD=p#ssw0rd-$RANDOM ```