Connect SQL Server With Android Application| JDBC Driver integration

SQL server is a very widely used database management system and is really useful and easy to use than others! So after spending hours, I found some way to integrate my android application with SQL server

First, download the JTDS driver for your OS.

Now How to add this driver to SQL server …. very easy but you must remember the way! watch my video

To add jar files to your android studio:

  1. Right click on app > New > Module.
  2. A window will open where you can find jar files when you scroll down.
  3. Select the widget another window will open, find the location of your jar and select.
  4. Finish. That is it you can start creating projects with SQL server after the build is finished.
  5. Now after adding JTDS you have to Add This Line In Your App Gradle File
    implementation project(':jtds-1.2.7')

Now we are going to make

a java class to connect SQL server and android studio. Remember this class is the heart of your every android project with SQL server.

Use Imports Correctly.


import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionClass {
// Your IP address must be static otherwise this will not work. You //can get your Ip address
//From Network and security in Windows.
String ip = "";
// This is default if you are using JTDS driver.
String classs = "net.sourceforge.jtds.jdbc.Driver";
// Name Of your database.
String db = "MyDB"; 
// Userame and password are required for security. 
so Go to sql server and add username and password for your database.
String un = "username";
String password = "passw0rd";

public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
Connection conn = null;
String ConnURL;
try {
ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
         + "databaseName=" + db + ";user=" + un + ";password="
              + password + ";";
  conn = DriverManager.getConnection(ConnURL);
 catch (SQLException se) 
Log.e("safiya", se.getMessage());
catch (ClassNotFoundException e) {
catch (Exception e) {
Log.e("error", e.getMessage());
return conn;

You can now use connectionclass object to make connection with your database and perform queries on your table.

You are all set, now test your application by making a simple login screen.

Strict Mode:

StrictMode is a developer tool which detects things you might be doing by accident and brings them to your attention so you can fix them.

StrictMode is most commonly used to catch accidental disk or network access on the application’s main thread, where UI operations are received and animations take place. Keeping disk and network operations off the main thread makes for much smoother, more responsive applications.


  1. Always keep your connection class separate. So that if you change your server address, you just need to modify one file.
  2. Whenever you are using a SQL query. Do not run it on main thread otherwise, you will get errors like:  Main Thread id having load skipped 144 thread.
  3. You can Async Task to run your query on the background while the screen shows Loading or progress dialog.
  4. You can also use Runnable and threads to move the execution of SQL query from the main UI thread to Created thread.
  5. Use Logcat To display errors you face during your execution.
  6. Create a package for all the different purpose java classes.

[amazon_link asins=’B071ZB5H4J,B072FKL28L,B07F2MN17P,B07JLFPYPH,B07BGBRK2R,B07C5JD34Y|B00XU590US,B00LISQ52A,B078MXCL76,B01CNC2QOA,B07GD6226K,B078X64KSV,B07C52YPXZ|B07GZQRLTZ,B07BTGN56B,B000PW7GLE,B07GN9CQ9X,B07GSNKR7J,B01J2CA1FA,B00LISQ52A|B07F37Q881,B00LISQ52A,B01CNC2QOA,B072N8NZY6,B0716T7F9L,B07H2TVW4K,B07H27W18M’ template=’ProductCarousel’ store=’lifenews525-21|lifenews-21|lifenews0b-21|lifenews06-21′ marketplace=’IN|UK|IT|FR’ link_id=’ 3e9787ba-d6b6-11e8-bd67-bb743672657e’]



Your Views And Question are Welcome Here!