Advertisements

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
    dependencies{
    
    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.

Connection Class.java

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 = "000.000.000.00";
// 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";

@SuppressLint("NewApi")
public Connection CONN() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection conn = null;
String ConnURL;
try {
Class.forName(classs);
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.

Tips:

  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.

How useful was this post?

Do not forget to rate! this helps us to Improve.

Average rating / 5. Vote count:

Read More Like This..

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Advertisements

Safiya Akhtar

Safiya Akhtar is Android Application Developer in India. She is currently working as a public consultant for career and education too, besides blogging and freelancing. She believes that it is never too late or too early to start what you have always dreamed of.

7 thoughts on “Connect SQL Server With Android Application| JDBC Driver integration

Your Views And Question are Welcome Here!

Advertisements