Playback starts YouTube’s embedded player (privacy-enhanced mode).
How to connect to MS SQL Server database from your Android App? - complete steps
ChannelProgrammer WorldDescription from the creator
This video shows the detailed steps to install and configure the MS SQL Server and Microsoft SQL Server Management Studio on your machine.
It shows the steps of how to boot up the (start) SQL services and then enable it (in SQL Server Configuration) to listen on a tcp/ip port. It uses local ip address and default port to connect to the SQL Server.
Later it shows how to use MS SQL Server Management Studio (SSMS) to create a new login ID (and password). It uses the new credentials to create new database and table and insert values in it.
Then it switches to Android Studio to show in simple steps of how to develop an App to interact with the MS SQL Server and database. It implements the required driver in the App's gradle file of the project. It defines the required INTERNET permission in the manifest file. And then in java code it creates a Connection which is used for quering to the database.
The complete source code of this project is available at the below url:
programmerworld.co/android/how-to-connect-to-ms…
For any questions, suggestions or appreciations, please reach out to us at:
programmerworld.co/contact/ or write to us at: [email protected]
Java code is placed below also for reference:
package com.example.mymssqlserverdatabaseconnection;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private static String ip = "192.168.1.101";
private static String port = "1433";
private static String Classes = "net.sourceforge.jtds.jdbc.Driver";
private static String database = "testDatabase";
private static String username = "test";
private static String password = "test";
private static String url = "jdbc:jtds:sqlserver://"+ip+":"+port+"/"+database;
private Connection connection = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.INTERNET}, PackageManager.PERMISSION_GRANTED);
textView = findViewById(R.id.textView);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
Class.forName(Classes);
connection = DriverManager.getConnection(url, username,password);
textView.setText("SUCCESS");
} catch (ClassNotFoundException e) {
e.printStackTrace();
textView.setText("ERROR");
} catch (SQLException e) {
e.printStackTrace();
textView.setText("FAILURE");
}
}
public void sqlButton(View view){
if (connection!=null){
Statement statement = null;
try {
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("Select * from TEST_TABLE;");
while (resultSet.next()){
textView.setText(resultSet.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
else {
textView.setText("Connection is null");
}
}
}
-
It shows the steps of how to boot up the (start) SQL services and then enable it (in SQL Server Configuration) to listen on a tcp/ip port. It uses local ip address and default port to connect to the SQL Server.
Later it shows how to use MS SQL Server Management Studio (SSMS) to create a new login ID (and password). It uses the new credentials to create new database and table and insert values in it.
Then it switches to Android Studio to show in simple steps of how to develop an App to interact with the MS SQL Server and database. It implements the required driver in the App's gradle file of the project. It defines the required INTERNET permission in the manifest file. And then in java code it creates a Connection which is used for quering to the database.
The complete source code of this project is available at the below url:
programmerworld.co/android/how-to-connect-to-ms…
For any questions, suggestions or appreciations, please reach out to us at:
programmerworld.co/contact/ or write to us at: [email protected]
Java code is placed below also for reference:
package com.example.mymssqlserverdatabaseconnection;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.TextView;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private static String ip = "192.168.1.101";
private static String port = "1433";
private static String Classes = "net.sourceforge.jtds.jdbc.Driver";
private static String database = "testDatabase";
private static String username = "test";
private static String password = "test";
private static String url = "jdbc:jtds:sqlserver://"+ip+":"+port+"/"+database;
private Connection connection = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.INTERNET}, PackageManager.PERMISSION_GRANTED);
textView = findViewById(R.id.textView);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
Class.forName(Classes);
connection = DriverManager.getConnection(url, username,password);
textView.setText("SUCCESS");
} catch (ClassNotFoundException e) {
e.printStackTrace();
textView.setText("ERROR");
} catch (SQLException e) {
e.printStackTrace();
textView.setText("FAILURE");
}
}
public void sqlButton(View view){
if (connection!=null){
Statement statement = null;
try {
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("Select * from TEST_TABLE;");
while (resultSet.next()){
textView.setText(resultSet.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
else {
textView.setText("Connection is null");
}
}
}
-
About this tutorial
Programmer World published this 27:28 video tutorial on April 9, 2020. It has been watched 75,294 times on YouTube.
- Channel
- Programmer World
- Category
- Education
- Duration
- 27:28
- Published
- April 9, 2020
- Views
- 75,294
- Likes
- 844
Share
This tutorial in other languages
- FRComment se connecter à la base de données MS SQL Server à partir de votre application Android? - suivez les étapes
- ESCómo conectarse a la base de datos MS SQL Server desde la aplicación de Android? - completar los pasos
- PTComo se conectar ao banco de dados MS SQL Server do seu aplicativo Android? - etapas completas
- ITCome connettersi a un database MS SQL Server dalla tua app Android? - completare i passaggi
- DEWie zu MS SQL Server-Datenbank von Ihrem Android-App verbinden? - vollständige Schritte
- NLHoe u verbinding met MS SQL Server-database vanaf je Android-app? - compleet stappen
- RUКак подключиться к базе данных MS SQL Server с вашего Android App? - выполните шаги
- PLJak połączyć się z bazą danych MS SQL Server z aplikacji na Androida? - kompletne kroki
- JAあなたのAndroidアプリからMS SQL Serverデータベースに接続するには? - 完全な手順
- ARكيفية الاتصال بقاعدة البيانات MS SQL Server من الخاص بك الروبوت التطبيق؟ - أكمل الخطوات
- HIकैसे अपने Android App से एमएस एसक्यूएल सर्वर डेटाबेस से कनेक्ट करने के लिए? - पूरा चरणों
- IDBagaimana menghubungkan ke database MS SQL Server dari App Android Anda? - langkah lengkap