0% found this document useful (0 votes)
8 views6 pages

RDBMS

MS SQL Server is a relational database management system developed by Microsoft, supporting various applications and offering features like T-SQL, ACID compliance, and data security. To connect MS SQL to an Android app, a middleware API must be created using languages like Java or PHP, which communicates with the database and the Android application. The process involves setting up the SQL Server, creating the API, and using libraries like Retrofit or Volley in Android Studio to handle network requests and data exchange.

Uploaded by

caogialinh02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

RDBMS

MS SQL Server is a relational database management system developed by Microsoft, supporting various applications and offering features like T-SQL, ACID compliance, and data security. To connect MS SQL to an Android app, a middleware API must be created using languages like Java or PHP, which communicates with the database and the Android application. The process involves setting up the SQL Server, creating the API, and using libraries like Retrofit or Volley in Android Studio to handle network requests and data exchange.

Uploaded by

caogialinh02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

RDBMS - MS SQL trong Android Studio

MS SQL (Microsoft SQL Server)

Definition:

MS SQL, or Microsoft SQL Server, is a relational database management system (RDBMS) developed by
Microsoft. It is used for storing, retrieving, and managing data in a structured format. MS SQL Server
supports a wide range of applications, from small single-machine applications to large web-based or
enterprise-level applications.

Key Features of MS SQL:

Relational Database: MS SQL Server is based on relational database architecture, meaning it uses tables
to store data with predefined relationships.

T-SQL (Transact-SQL): MS SQL supports Transact-SQL, an extension of SQL (Structured Query Language)
used for writing queries, procedures, triggers, and more.

ACID Compliance: MS SQL supports ACID (Atomicity, Consistency, Isolation, Durability) principles,
ensuring reliable transaction processing.

Scalability: It supports small to large-scale applications with massive data volumes.

Data Security: MS SQL has advanced encryption techniques, data masking, and authentication options to
ensure data security.

Integration Services (SSIS): A platform for building enterprise-level data integration and transformation
solutions.

Reporting Services (SSRS): A reporting platform for creating and delivering interactive and paginated
reports.

Analytics (SSAS): It provides advanced data analytics and reporting through SQL Server Analysis Services.

Backup & Recovery: It provides robust backup and recovery mechanisms.

Concurrency Control: It supports multiple users accessing the database simultaneously with minimal
conflict through locking, isolation levels, and versioning.

MS SQL Editions:

Express Edition: Free and limited version for small-scale applications.

Standard Edition: Mid-level edition with essential features.

Enterprise Edition: Full version with advanced features for large-scale applications.

Developer Edition: Full features but meant for development and testing.

How to Connect MS SQL to Android Studio


Connecting MS SQL Server to an Android application typically involves setting up a middleware such as a
REST API using Java, PHP, or Node.js to communicate between your Android app and the SQL Server
database. Android itself doesn't directly connect to MS SQL Server, but you can interact with the
database using APIs and network communication. Here's how you can set it up:

Steps to Connect MS SQL to Android Studio:

Create a Backend API: Since Android cannot directly connect to MS SQL, you need to create a backend
API using a language like Java, PHP, or ASP.NET that will communicate with MS SQL and your Android
app.

Java (Spring Boot): You can create a RESTful API using Java with Spring Boot or any other framework. This
API will handle HTTP requests from the Android app and communicate with MS SQL.

Add dependencies for JDBC in your pom.xml file (Maven) or build.gradle (Gradle) file for SQL Server
connection.

Use the JDBC Driver for SQL Server to connect from Java to MS SQL.

Example connection string in Java using JDBC:

java

Copy code

String connectionUrl = "jdbc:sqlserver://localhost:1433;"

+ "databaseName=yourDatabaseName;"

+ "user=yourUsername;"

+ "password=yourPassword;";

Connection con = DriverManager.getConnection(connectionUrl);

PHP (Slim/Laravel): You can also use PHP to create an API (RESTful or otherwise) that interacts with MS
SQL. The API can be called from Android to fetch or store data.

Example using PHP:

php

Copy code

$serverName = "localhost"; // MS SQL server name or IP address


$connectionInfo = array( "Database"=>"yourDatabase", "UID"=>"yourUsername",
"PWD"=>"yourPassword");

$conn = sqlsrv_connect( $serverName, $connectionInfo);

Install JDBC Driver for MS SQL: If you are using Java as your middleware, download and install the
Microsoft JDBC Driver for SQL Server. Add the driver to your project dependencies. If you're using Spring
Boot, you can include it in your pom.xml file as:

xml

Copy code

<dependency>

<groupId>com.microsoft.sqlserver</groupId>

<artifactId>mssql-jdbc</artifactId>

<version>9.2.0.jre8</version>

</dependency>

Create REST API Endpoints: The backend service should expose API endpoints (like /login, /getData,
/postData) that the Android app can communicate with.

Example in Java Spring Boot:

java

Copy code

@RestController

public class YourController {

@GetMapping("/getData")

public ResponseEntity<List<YourDataModel>> getData() {

List<YourDataModel> data = yourService.getDataFromDatabase();

return new ResponseEntity<>(data, HttpStatus.OK);

}
Enable SQL Server for Remote Connections: Make sure that SQL Server is configured to accept remote
connections, especially if your Android app is not on the same machine. Configure SQL Server by:

Opening SQL Server Configuration Manager.

Enabling TCP/IP under SQL Server Network Configuration.

Allowing the required port (default: 1433) through the firewall.

Allow CORS in API (Cross-Origin Resource Sharing): In the API server, ensure that CORS is enabled so the
Android app can make requests to the server.

Android Network Request (Retrofit/Volley/HttpURLConnection):

In Android Studio, you can use libraries like Retrofit or Volley to send HTTP requests to the API and
retrieve the data.

Example using Retrofit:

java

Copy code

public interface ApiService {

@GET("/getData")

Call<List<YourDataModel>> getData();

Retrofit retrofit = new Retrofit.Builder()

.baseUrl("http://your-backend-url.com/")

.addConverterFactory(GsonConverterFactory.create())

.build();

ApiService apiService = retrofit.create(ApiService.class);

Call<List<YourDataModel>> call = apiService.getData();


Handle JSON Response: The API typically sends data back in JSON format, which can then be parsed on
the Android side.

Example of parsing JSON:

java

Copy code

Retrofit retrofit = new Retrofit.Builder()

.baseUrl("http://your-backend-url.com/")

.addConverterFactory(GsonConverterFactory.create())

.build();

ApiService apiService = retrofit.create(ApiService.class);

Call<YourDataModel> call = apiService.getData();

call.enqueue(new Callback<YourDataModel>() {

@Override

public void onResponse(Call<YourDataModel> call, Response<YourDataModel> response) {

if (response.isSuccessful()) {

YourDataModel data = response.body();

// Handle the response data

@Override

public void onFailure(Call<YourDataModel> call, Throwable t) {

// Handle failure

});
Summary of Steps:

Set up MS SQL Server (Enable remote connections, configure firewall).

Create a backend API (using Java, PHP, Node.js, etc.).

Use JDBC or ODBC to connect your backend with MS SQL.

Expose API endpoints for Android app communication.

Use Retrofit/Volley/HttpURLConnection in Android Studio to connect to the API.

Send HTTP requests to retrieve or send data between the Android app and the backend API connected
to MS SQL.

This approach uses a backend server as a middleware to handle communication between MS SQL Server
and the Android app.

You might also like