Connect.docx

  • Uploaded by: Atila King
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Connect.docx as PDF for free.

More details

  • Words: 191
  • Pages: 3
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

More Documents from "Atila King"

Connect.docx
November 2019 20
Dest.docx
April 2020 31
Air-blaster-system-cab.pdf
November 2019 38
Petkab Ce H05vv-f
November 2019 17
Flt-0005.pdf
June 2020 19