Connect import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class SQLDatabaseConnection { // Connect to your database. // Replace server name, username, and password with your credentials public static void main(String[] args) { String connectionUrl = "jdbc:sqlserver://yourserver.database.windows.net:1433;" + "database=AdventureWorks;" + "user=yourusername@yourserver;" + "password=yourpassword;" + "encrypt=true;" + "trustServerCertificate=false;" + "loginTimeout=30;"; try (Connection connection = DriverManager.getConnection(connectionUrl);) { // Code here. } // Handle any errors that may have occurred. catch (SQLException e) { e.printStackTrace(); } } }
Execute a query import import import import import
java.sql.Connection; java.sql.DriverManager; java.sql.ResultSet; java.sql.SQLException; java.sql.Statement;
public class SQLDatabaseConnection { // Connect to your database. // Replace server name, username, and password with your credentials public static void main(String[] args) { String connectionUrl = "jdbc:sqlserver://yourserver.database.windows.net:1433;" + "database=AdventureWorks;" + "user=yourusername@yourserver;" + "password=yourpassword;" + "encrypt=true;" + "trustServerCertificate=false;" + "loginTimeout=30;"; ResultSet resultSet = null; try (Connection connection = DriverManager.getConnection(connectionUrl); Statement statement = connection.createStatement();) { // Create and execute a SELECT SQL statement. String selectSql = "SELECT TOP 10 Title, FirstName, LastName from SalesLT.Customer"; resultSet = statement.executeQuery(selectSql); // Print results from select statement while (resultSet.next()) { System.out.println(resultSet.getString(2) + " " + resultSet.getString(3)); } } catch (SQLException e) { e.printStackTrace(); } } }
https://docs.microsoft.com/en-us/sql/connect/jdbc/step-3-proof-of-concept-connecting-to-sqlusing-java?view=sql-server-2017
https://www.youtube.com/watch?v=_vtWXfKaHH8