Skip to content
Unknown's avatar

Ketul Patel

  • Home
  • Contact

Tag: login page in android studio using sql

Login Page in Android Studio using MS-SQL.

This blog will guide how to make a login page in android studio and fetch the value from MS-SQL database by following below simple steps :

(1) Step – 1 : Create a Login Layout in activity_login.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity"
    android:orientation="vertical"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="Email"
        android:id="@+id/emaillogin"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="Password"
        android:id="@+id/passwordlogin"/>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="30dp"
        android:layout_gravity="center"
        android:gravity="center"
        >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/loginbtn"
        android:textSize="25dp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register"
        android:id="@+id/regbtn"
        android:textSize="25dp"
        />
</LinearLayout>

</LinearLayout>

(2) Step – 2 : Create a Connection Class that will be in Connection Package and name the java class as ConnectionClass.java :

public class ConnectionClass {
public static String ip =””; // SQL Server IP Address
public static String un = “”; // SQL Server User name
public static String pass = “”; // SQL Server Password
public static String db = “”; // SQL Server Database
}

(3) Step – 3 : Add following permission to AndroidManifest.xml

<uses-permission android-name=”android.permission.INTERNET />

(4) Step – 4 : Now write the business logic for LoginActivity.java :

public class LoginActivity extends AppCompatActivity {
EditText emaillogin,passwordlogin;
Button loginbtn,regbtn;

Connection con;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    emaillogin = (EditText)findViewById(R.id.emaillogin);
    passwordlogin = (EditText)findViewById(R.id.passwordlogin);
    loginbtn = (Button)findViewById(R.id.loginbtn);
    regbtn = (Button)findViewById(R.id.regbtn);

    loginbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new LoginActivity.checkLogin().execute("");
        }
    });

    regbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
            startActivity(intent);
            finish();
        }
    });
}

 public class checkLogin extends AsyncTask<String, String, String>{

        String z = null;
        Boolean isSuccess = false;


        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onPostExecute(String s) {

        }

        @Override
        protected String doInBackground(String... strings) {
            con = connectionClass(ConnectionClass.un.toString(),ConnectionClass.pass.toString(),ConnectionClass.db.toString(),ConnectionClass.ip.toString());
            if(con == null){
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(LoginActivity.this,"Check Internet Connection",Toast.LENGTH_LONG).show();
                    }
                });
                z = "On Internet Connection";
            }
            else {
                try {
                    String sql = "SELECT * FROM register WHERE email = '" + emaillogin.getText() + "' AND password = '" + passwordlogin.getText() + "' ";
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(sql);

                    if (rs.next()) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(LoginActivity.this, "Login Success", Toast.LENGTH_LONG).show();
                            }
                        });
                        z = "Success";

                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(LoginActivity.this, "Check email or password", Toast.LENGTH_LONG).show();
                            }
                        });

                        emaillogin.setText("");
                        passwordlogin.setText("");
                    }
                } catch (Exception e) {
                    isSuccess = false;
                    Log.e("SQL Error : ", e.getMessage());
                }
            }
            return z;
        }
    }

    @SuppressLint("NewApi")
    public Connection connectionClass(String user, String password, String database, String server){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection connection = null;
        String connectionURL = null;
        try{
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            connectionURL = "jdbc:jtds:sqlserver://" + server+"/" + database + ";user=" + user + ";password=" + password + ";";
            connection = DriverManager.getConnection(connectionURL);
        }catch (Exception e){
            Log.e("SQL Connection Error : ", e.getMessage());
        }

        return connection;
    }
}

(5) Step – 5 : To watch the full tutorial you can watch following video.

KetulPatel YouTube Leave a comment April 6, 2020April 6, 2020 3 Minutes
Blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Subscribe Subscribed
    • Ketul Patel
    • Already have a WordPress.com account? Log in now.
    • Ketul Patel
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
Design a site like this with WordPress.com
Get started