In this tutorial, I am gonna explain all the steps and the program flow of the Android Online Shopping Application to help you understand a little about the program before trying to run the App.
The App is Divided into Two Parts

1. For Shoppers

is the app used to shop from the items available in the store from the sellers.

2. For Sellers

is the app used to list items for sale.
Before trying to run the app first we need to create a database to store our user information and Items information For that, I have created a shopapp.sql file that has all the databases and tables we will be using for this project.

CREATE DATABASE shoppay;
USE shoppay;

CREATE TABLE `payment` (
  `userID` int(11) NOT NULL,
  `userAmount` double NOT NULL,
  `userName` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `shopitemslist` (
  `itemID` int(11) NOT NULL,
  `itemTitle` varchar(30) NOT NULL,
  `itemIcon` longblob NOT NULL,
  `itemPrice` double NOT NULL,
  `itemCategory` varchar(20) NOT NULL,
  `itemLeft` int(11) NOT NULL,
  `itemProvider` varchar(20) NOT NULL,
  `adminName` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `shoppay` (
  `userName` varchar(20) NOT NULL,
  `shopID` int(11) NOT NULL,
  `paymentPIN` varchar(8) NOT NULL,
  `paymentState` varchar(20) NOT NULL,
  `paymentAmount` double NOT NULL,
  `payedAdmin` varchar(20) NOT NULL,
  `orderID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

TE TABLE `useraccount` (
  `userName` varchar(50) NOT NULL,
  `userEmail` varchar(50) NOT NULL,
  `userPass` varchar(30) NOT NULL,
  `userFullName` varchar(50) NOT NULL,
  `phoneNumber` varchar(20) NOT NULL,
  `streetAddress` varchar(50) NOT NULL,
  `homeNumber` varchar(10) NOT NULL,
  `userKebele` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


ALTER TABLE `payment`
  ADD PRIMARY KEY (`userID`);

ALTER TABLE `shopitemslist`
  ADD PRIMARY KEY (`itemID`);

ALTER TABLE `shoppay`
  ADD PRIMARY KEY (`shopID`);

ALTER TABLE `shopitemslist`
  MODIFY `itemID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=28;

ALTER TABLE `shoppay`
  MODIFY `shopID` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;

After we have created our database the next thing we will do is to load the database driver for java so that we can access the database we have created in the above step. in my case I have the MySQL connector for java.

MySQL-connector-java-5.1.12-bin.jar

In Android Studio follow the below steps.


Goto File -> New -> Import Module
Source Directory -> Browse the project path.
Specify the Module Name – which is mysql-connector-java-5.1.12-bin.jar in my case.

We have now configured MySQL to work with java so that we can execute a query inside our java file.

This does not me we have completely configured MySQL the last thing we need to do is to create a Java class which will be used to initiate the connection with MySQL.
The below code will load our MySQL driver we have setup earlier and then try to initiate a Connection with the database with the help of getConnection();

In line 17 we have 3 different components.

1. URL(jdbc:mysql://192.168.43.101/shopapp)

this is the database URL we have created in the above step. This URL only works when you database is port is 3306 if your port number is different then you can configure like this (jdbc:mysql://ip-address:port/database-name) for example jdbc:mysql://192.168.43.101:3521/shopapp.

2. Database User Name(shopapp)

This one is the user name that have full access over the database we have created above

3. Database Password(shopapp)

The password for the user name we set above.
So finally we will get this structure like this jdbc:mysql://ip-address:port(if changed)/database-name","user-name","password".

At the end of this if everything configured correctly we will have a database connection ready to be used.

package com.example.razor_studios.shopapp;

import android.os.StrictMode;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnect {
    public static Connection getConnection(){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://192.168.43.101/shopapp","shopapp","shopapp");
        } catch (ClassNotFoundException | SQLException ex) {
        }
        return con;
    }
}

After we configure everything correctly our next step is to create an SQLite DB Helper which will be used to store data locally in the device like(user information, shopping history, and payment histories) so our user doesn't need to enter their information whenever they open the app.
For now, we only need to store user info and their selected language for that we have the below code.

This code will be used to create the SQLiteDB and our user info table, adding new user info whenever they logout and login again, update their data whenever they update their information and also update selected languages.

package com.example.razor_studios.shopapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteDBHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "shopappuser";
    public static final String TABLE_NAME = "useraccount";
    public static final int DB_VERSION = 1;
    public static String USER_NAME="userName", USER_EMAIL="userEmail", USER_PASS="userPass", USER_FULLNAME="userFullName",PHONE_NUMBER="phoneNumber", USER_LANG="userLang";
    public static String CREATE_TABLE = "CREATE TABLE useraccount (userName text, userEmail text, userPass text, userFullName text, phoneNumber text, userLang text)";
    public static final String DROP_TABLE = "DROP TABLE IF EXIST useraccount";
    public static final String TRUNCATE_TABLE = "DELETE FROM useraccount";

    public SQLiteDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(CREATE_TABLE);
    }
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int j) {
        sqLiteDatabase.execSQL(DROP_TABLE);
        onCreate(sqLiteDatabase);
    }
    public Cursor readAccount(SQLiteDatabase sqLiteDatabase) {
        String[] values = {USER_NAME, USER_EMAIL, USER_PASS, USER_FULLNAME, PHONE_NUMBER, USER_LANG};
        Cursor cursor = sqLiteDatabase.query(TABLE_NAME, values, null,null, null, null, null);
        return cursor;
    }
    public void addAccount(String uName, String uEmail, String uPass, String uFullName, String pNumber, String uLang, SQLiteDatabase sqLiteDatabase) {
        ContentValues cValues = new ContentValues();
        cValues.put(USER_NAME, uName);
        cValues.put(USER_EMAIL, uEmail);
        cValues.put(USER_PASS, uPass);
        cValues.put(USER_FULLNAME, uFullName);
        cValues.put(PHONE_NUMBER, pNumber);
        cValues.put(USER_LANG, uLang);

        sqLiteDatabase.insert(TABLE_NAME, null, cValues);
    }
    public void removeAccount(String uName, SQLiteDatabase sqLiteDatabase) {
        String selection = USER_NAME + " = '" + uName + "'";
        sqLiteDatabase.delete(TABLE_NAME, selection,null);
    }
    public void updateUserLanguage(String uLang, String uName, SQLiteDatabase sqLiteDatabase) {
        ContentValues cValues = new ContentValues();
        cValues.put(USER_LANG, uLang);

        String selection = USER_NAME + " = '" + uName + "'";
        sqLiteDatabase.update(TABLE_NAME, cValues, selection,null);
    }
}