Connect URL

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import java.sql.

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

public class ConnectURL {


public static void main(String[] args) {

// Create a variable for the connection string.


String connectionUrl =
"jdbc:sqlserver://<server>:<port>;databaseName=AdventureWorks;user=<user>;password=
<password>";

try (Connection con = DriverManager.getConnection(connectionUrl); Statement


stmt = con.createStatement();) {
String SQL = "SELECT TOP 10 * FROM Person.Contact";
ResultSet rs = stmt.executeQuery(SQL);

// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString("FirstName") + " " +
rs.getString("LastName"));
}
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
}
}
}

You might also like