53GB

How to connect to MS SQL Server database from your Android App? – complete steps



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:
https://programmerworld.co/android/how-to-connect-to-ms-sql-server-database-from-your-android-app-complete-steps/

For any questions, suggestions or appreciations, please reach out to us at:
https://programmerworld.co/contact/ or write to us at: programmerworld1990@gmail.com

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”);
}
}
}

Exit mobile version