Mad Practical 28
Mad Practical 28
Mad Practical 28
• Activity_main.xml
• MainActivity.java
package com.example.practical28;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
MySqliteDemo myDatabaseDemo ;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edt1 = findViewById(R.id.edt1);
EditText edt2 = findViewById(R.id.edt2);
Button btn1 = findViewById(R.id.btn1);
Button btn2 = findViewById(R.id.btn2);
Intent intent = new Intent(getApplicationContext(),secondActivity.class);
myDatabaseDemo = new MySqliteDemo(this);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(count == 3){
Toast.makeText(MainActivity.this,"Cannot login your unsccessfull login is out of
limit",Toast.LENGTH_SHORT).show();
}
else if (edt1.getText().toString().equals(" ") && edt2.getText().toString().equals("
") ){
Toast.makeText(MainActivity.this,"Username and password cannot be
empty",Toast.LENGTH_SHORT).show();
count ++;
}
else if (edt1.getText().toString().length()<=0 &&
edt2.getText().toString().length()<=0){
Toast.makeText(MainActivity.this,"Username and password cannot be
empty",Toast.LENGTH_SHORT).show();
count++;
}
else {
boolean res = myDatabaseDemo.read(edt1.getText().toString(),
edt2.getText().toString());
if (res) {
Toast.makeText(MainActivity.this, "Login Successfully ",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Login UnSuccessfully",
Toast.LENGTH_SHORT).show();
}
}
}
});
btn2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view ){
startActivity(intent);
}
});
}
}
• MySqliteDemo.java
package com.example.practical28;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqliteDemo extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "LoginPage";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "LoginInfo";
public static final String USER_NAME = "username";
public static final String PASSWORD = "password";
public MySqliteDemo(Context
context){super(context,DATABASE_NAME,null,DATABASE_VERSION);}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+"( id INTEGER PRIMARY KEY
AUTOINCREMENT ,"+USER_NAME+" TEXT,"+PASSWORD+" TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db , int oldVersion,int newVersion){
}
public boolean insert(String username,String password){
SQLiteDatabase db = getWritableDatabase();
ContentValues con = new ContentValues();
con.put(USER_NAME,username);
con.put(PASSWORD,password);
long val = db.insert(TABLE_NAME,null,con);
if(val>0){
return true;
}
else{
return false;
}
}
public boolean read(String username,String password){
SQLiteDatabase db = getWritableDatabase();
try {
String query = "SELECT * FROM " + TABLE_NAME + " WHERE " + USER_NAME + "=?"
+ username + "’ AND " + PASSWORD + "=?’" + password + "’;";
Cursor cur = db.rawQuery(query, null);
if(cur.getCount()>0){
return true;
}
else{
return false;
}
}
catch(Exception e){
return false;
}
}
}
• Second_Activity.xml
• SecondActivity.java
package com.example.practical28;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Pattern;
public class secondActivity extends AppCompatActivity {
MySqliteDemo myDatabase ;
private static final Pattern patter = Pattern.compile("^(?=.*[a-z])(?=.*[AZ])(?=.*[\\W_]).{6,20}$");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
myDatabase = new MySqliteDemo(this);
EditText edt1 = findViewById(R.id.edt1);
EditText edt2 = findViewById(R.id.edt2);
EditText edt3 = findViewById(R.id.edt3);
Button btn = findViewById(R.id.btn1);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(patter.matcher(edt2.getText().toString()).matches())
{
if(edt2.getText().toString().equals(edt3.getText().toString()))
{
boolean res =
myDatabase.insert(edt1.getText().toString(),edt2.getText().toString());
if(res){
Toast.makeText(secondActivity.this,"Sign In
Successfully",Toast.LENGTH_SHORT).show();
startActivity(intent);
secondActivity.super.finish();
}
else{
Toast.makeText(secondActivity.this,"Sgin in not
Successfully",Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(secondActivity.this,"Please Check your
password",Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(secondActivity.this,"Please Enter Strong Password
",Toast.LENGTH_SHORT).show();
}
}
});
}
}
Output:-