All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Android Studio Connect To SQL Server Database Example

Last Updated : Mar 11, 2024

Android Studio Connect To SQL Server Database Example

In this article we will show you the solution of android studio connect to sql server database example, anything that repeats or is structured, such contact information, should be saved to a database.

This page walks you through the process of setting up SQLite databases on Android, assuming you are already familiar with SQL databases in general.

The android.database.sqlite package contains the same APIs you'll require for using a database on Android.

A formal declaration of the database's organizational structure, the schema is one of the fundamental concepts of SQL databases.

The SQL statements you use to build your database are represented in the schema.

Create a companion class, also referred to as the contract class, that specifically outlines the organization of your schema in a methodical and self-documenting manner.

Constants that specify names for URIs,tables, and columns are stored in contract classes.

You may utilize identical constants across every one of the classes within a single package if you use the contract class.

This enables you to modify a column name once and have it take effect throughout your code.

Include definitions that are universal to your entire database inside the root level of a class as this is an excellent method to structure a contract class.

For each table, construct an inner class next. The columns of each inner class are listed for the appropriate table.

Android stores this database inside the private folder for your app, just like when you save files to the device's internal storage.

Your data is protected because this section is by default inaccessible to users or other programmes.

Step By Step Guide On Android Studio Connect To SQL Server Database Example :-

package com.example.dbtestsql;
import android.Manifest;
import android.os.StrictMode;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
    private static String ip = "";
    private static String port = "1433";
    private static String Classes = "net.sourceforge.jtds.jdbc.Driver";
    private static String database = "CustomerCareSystem";// the data base name
    private static String username = "natydb";// the user name
    private static String password = "1234";// the password
    private static String url = "jdbc:jtds:sqlserver://"+ip+":"+port+"/"+database; // the connection url string
    private Connection connection = null;
    protected void onCreate(Bundle savedInstanceState) {
    public void start(View view) {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.INTERNET}, PackageManager.PERMISSION_GRANTED);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        try {
            connection = DriverManager.getConnection(url, username,password);
            Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
        } catch (ClassNotFoundException e) {
            Toast.makeText(this, "Class fail", Toast.LENGTH_SHORT).show();
        } catch (SQLException e) {
            Toast.makeText(this, "Connected no", Toast.LENGTH_SHORT).show();
  1. First we create an import function.
  2. Then we create a class as mainactivity.
  3. Then we declare host as this is the host ip that your database exists on. You can use for the local host found on your pc. use if config for windows to find the ip if the database exists on your pc.
  4. Then we see the port sql server runs on.
  5. After that the driver that is required for this connection is used.
  6. Then we declare username and password.

Conclusion :-

A helpful collection of APIs for controlling your database may be found in the SQLiteOpenHelper class.

The system only executes the potentially time-consuming tasks of establishing and updating the database when necessary, not when the programme is launched, when you use this class to gain references to your database.

Calling getWritableDatabase() or getReadableDatabase is all that is required().

I hope this article on android studio connect to sql server database example helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪