Подія
31 бер., 23 - 2 квіт., 23
Найбільший навчальний захід SQL, Fabric і Power BI. 31 березня – 2 квітня. Щоб заощадити 400 грн, скористайтеся кодом FABINSIDER.
Реєструйтеся сьогодніЦей браузер більше не підтримується.
Замініть його на Microsoft Edge, щоб користуватися перевагами найновіших функцій, оновлень безпеки та технічної підтримки.
This Microsoft JDBC Driver for SQL Server sample application demonstrates how to connect to a SQL Server database by using a data source object. It also demonstrates how to retrieve data from a SQL Server database by using a stored procedure.
The code file for this sample is named ConnectDataSource.java, and it can be found in the following location:
\<installation directory>\sqljdbc_<version>\<language>\samples\connections
To run this sample application, you must set the classpath to include the mssql-jdbc jar file. You'll also need access to the AdventureWorks2022 sample database. For more information about how to set the classpath, see Using the JDBC Driver.
Примітка
The Microsoft JDBC Driver for SQL Server provides mssql-jdbc class library files to be used depending on your preferred Java Runtime Environment (JRE) settings. For more information about which JAR file to choose, see System Requirements for the JDBC Driver.
In the following example, the sample code sets various connection properties by using setter methods of the SQLServerDataSource object, and then calls the getConnection method of the SQLServerDataSource object to return a SQLServerConnection object.
Next, the sample code uses the prepareCall method of the SQLServerConnection object to create a SQLServerCallableStatement object, and then the executeQuery method is called to execute the stored procedure.
Finally, the sample uses the SQLServerResultSet object returned from the executeQuery method to iterate through the results returned by the stored procedure.
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
public class ConnectDataSource {
public static void main(String[] args) {
// Create datasource.
SQLServerDataSource ds = new SQLServerDataSource();
ds.setUser("<user>");
ds.setPassword("<password>");
ds.setServerName("<server>");
ds.setPortNumber(<port>);
ds.setDatabaseName("AdventureWorks");
try (Connection con = ds.getConnection();
CallableStatement cstmt = con.prepareCall("{call dbo.uspGetEmployeeManagers(?)}");) {
// Execute a stored procedure that returns some data.
cstmt.setInt(1, 50);
ResultSet rs = cstmt.executeQuery();
// Iterate through the data in the result set and display it.
while (rs.next()) {
System.out.println("EMPLOYEE: " + rs.getString("LastName") + ", " + rs.getString("FirstName"));
System.out.println("MANAGER: " + rs.getString("ManagerLastName") + ", " + rs.getString("ManagerFirstName"));
System.out.println();
}
}
// Handle any errors that may have occurred.
catch (SQLException e) {
e.printStackTrace();
}
}
}
Подія
31 бер., 23 - 2 квіт., 23
Найбільший навчальний захід SQL, Fabric і Power BI. 31 березня – 2 квітня. Щоб заощадити 400 грн, скористайтеся кодом FABINSIDER.
Реєструйтеся сьогодніНавчання
Модуль
Manage Data Sources with External Data Stores in Finance and Operations apps - Training
Administrators often need access to their data from finance and operations apps, to perform analytics that will help ensure that certain parameters or business metrics are met.
Сертифікація
Microsoft Certified: Azure Database Administrator Associate - Certifications
Адмініструйте інфраструктуру баз даних SQL Server для хмарних, локальних і гібридних реляційних баз даних за допомогою пропозицій реляційних баз даних Microsoft PaaS.