JDBC MS-SQL
Example Execution
java -cp .;ojdbc7.jar;mssql-jdbc-6.2.2.jre8.jar jdbctestMSSQL3
Example Code
Uses Windows Authentication...
sqljdbc_auth.dll must be in a location specified in your PATH
CLASSPATH must include mssql-jdbc-6.2.2.jre8.jar (or later)
//=====================================================================
//
// File: connectURL.java
// Summary: This Microsoft JDBC Driver for SQL Server sample application
// demonstrates how to connect to a SQL Server database by using
// a connection URL. It also demonstrates how to retrieve data
// from a SQL Server database by using an SQL statement.
//
//---------------------------------------------------------------------
//
// This file is part of the Microsoft JDBC Driver for SQL Server Code Samples.
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
//=====================================================================
import java.sql.*;
public class jdbctestMSSQL3 {
public static void main(String[] args) {
// Create a variable for the connection string.
String connectionUrl = "jdbc:sqlserver://myserver:1433;" +
"databaseName=myDB;integratedSecurity=true;";
// Declare the JDBC objects.
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// Establish the connection.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT TOP 10 * FROM dbo.InstanceStartup_dF0j";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2));
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
finally {
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (stmt != null) try { stmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
}
Bibliography
https://docs.microsoft.com/en-us/sql/connect/jdbc/microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15https://docs.microsoft.com/en-us/sql/connect/sql-connection-libraries?view=sql-server-ver15#anchor-20-drivers-relational-accesshttps://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15https://docs.microsoft.com/en-us/sql/connect/jdbc/building-the-connection-url?view=sql-server-ver15#Connectingintegratedhttps://thusithamabotuwana.wordpress.com/2012/07/19/connecting-to-sql-server-from-java/https://stackoverflow.com/questions/2451892/how-do-i-connect-to-a-sql-server-2008-database-using-jdbc