Skip to content

Instantly share code, notes, and snippets.

@brunopk
Created March 2, 2020 17:34
Show Gist options
  • Save brunopk/ca89a4b572b5169a62849a24bfd503e0 to your computer and use it in GitHub Desktop.
Save brunopk/ca89a4b572b5169a62849a24bfd503e0 to your computer and use it in GitHub Desktop.
SQL Server connection with Java 8
import java.sql.*;
public class Main {
public static void main(String[] args) {
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://10.64.36.137:1433;user=sigma;password=Password33";
try {
Connection con = DriverManager.getConnection(connectionUrl); Statement stmt = con.createStatement();
String SQL = "SELECT TOP 10 * FROM Visa";
ResultSet rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString("Nombre") + " " + rs.getString("Apellido"));
}
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment