Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.mogli.henabenparekhrajthakkar_comp304_lab4;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by Mogli on 11/30/2017.
- */
- public class SqlHelper extends SQLiteOpenHelper {
- private static final int DATABASE_VERSION = 1;
- private static final String DATABASE_NAME = "hospital.db";
- //nurse table name & columns
- private static final String NURSE_TABLE_NAME = "nurses";
- private static final String NURSE_COLUMN_ID = "nurseId";
- private static final String NURSE_COLUMN_FIRST_NAME = "nurseFirstName";
- private static final String NURSE_COLUMN_LAST_NAME = "nurseLastName";
- private static final String NURSE_COLUMN_DEPARTMENT = "nurseDepartment";
- private static final String NURSE_COLUMN_PASSWORD = "nursePassword";
- //doctor table name & columns
- private static final String DOCTOR_TABLE_NAME = "doctors";
- private static final String DOCTOR_COLUMN_ID = "doctorId";
- private static final String DOCTOR_COLUMN_FIRST_NAME = "doctorFirstName";
- private static final String DOCTOR_COLUMN_LAST_NAME = "doctorLastName";
- private static final String DOCTOR_COLUMN_DEPARTMENT = "doctorDepartment";
- private static final String DOCTOR_COLUMN_PASSWORD = "doctorPassword";
- private static final String NURSE_TABLE_CREATE = "create table if not exists nurses( nurseId text primary key not null, "
- + "nurseFirstName text not null, nurseLastName text not null, nurseDepartment text not null, nursePassword text not null );";
- private static final String DOCTOR_TABLE_CREATE = "create table if not exists doctors( doctorId text primary key not null, "
- + "doctorFirstName text not null, doctorLastName text not null, doctorDepartment text not null, doctorPassword text not null );";
- //insert query
- /*private String insertIntoNurse = "INSERT INTO nurses (nurseId, nurseFirstName, nurseLastName, nurseDepartment, nursePassword)"
- +"VALUES ('john123', 'john', 'doe', 'd1', '123john' ), ('jonahdoe', 'jonah', 'doe', 'd2', '123jonah'), ('hannah123', 'hannah', 'montana', 'd3', 'hm123');";*/
- public SqlHelper(Context context)
- {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase sqLiteDatabase) {
- try {
- sqLiteDatabase.execSQL(NURSE_TABLE_CREATE);
- sqLiteDatabase.execSQL(DOCTOR_TABLE_CREATE);
- }
- catch(Exception e)
- {
- Log.d("myMsg", "Error creating table");
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
- String nurse_query = "DROP TABLE IF EXISTS " + NURSE_TABLE_NAME;
- String doctor_query = "DROP TABLE IF EXISTS " + DOCTOR_TABLE_NAME;
- sqLiteDatabase.execSQL(nurse_query);
- sqLiteDatabase.execSQL(doctor_query);
- onCreate(sqLiteDatabase);
- }
- public boolean checkUser(String email, String password) {
- // array of columns to fetch
- String[] columns = {
- NURSE_COLUMN_ID
- };
- SQLiteDatabase db = this.getReadableDatabase();
- // selection criteria
- String selection = NURSE_COLUMN_ID + " = ?" + " AND " + NURSE_COLUMN_PASSWORD + " = ?";
- // selection arguments
- String[] selectionArgs = {email, password};
- // query user table with conditions
- /**
- * Here query function is used to fetch records from user table this function works like we use sql query.
- * SQL query equivalent to this query function is
- * SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com' AND user_password = 'qwerty';
- */
- Cursor cursor = db.query(NURSE_TABLE_NAME, //Table to query
- columns, //columns to return
- selection, //columns for the WHERE clause
- selectionArgs, //The values for the WHERE clause
- null, //group the rows
- null, //filter by row groups
- null); //The sort order
- int cursorCount = cursor.getCount();
- cursor.close();
- db.close();
- if (cursorCount > 0) {
- return true;
- }
- return false;
- }
- public String searchPass(String NURSE_COLUMN_ID)
- {
- SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
- String query = "select nurseId, nursePassword from "+ NURSE_TABLE_NAME;
- Cursor cursor =sqLiteDatabase.rawQuery(query, null);
- String a, b;
- b = "";
- if(cursor.moveToFirst())
- {
- do
- {
- a = cursor.getString(0);
- b = cursor.getString(1);
- if(a.equals(NURSE_COLUMN_ID))
- {
- b = cursor.getString(1);
- break;
- }
- }
- while(cursor.moveToNext());
- }
- return b;
- }
- public void addNurses(Nurse nurse)
- {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(NURSE_COLUMN_ID, nurse.getNurseId());
- values.put(NURSE_COLUMN_FIRST_NAME, nurse.getNurseFirstName());
- values.put(NURSE_COLUMN_LAST_NAME, nurse.getNurseLastName());
- values.put(NURSE_COLUMN_DEPARTMENT, nurse.getNurseDepartment());
- values.put(NURSE_COLUMN_PASSWORD, nurse.getNursePassword());
- db.insert(NURSE_TABLE_NAME,null,values);
- db.close();
- }
- public List<Nurse> getAllNurses() {
- List<Nurse> nurseList = new ArrayList<Nurse>();
- // Select All Query
- String selectQuery = "SELECT * FROM " + NURSE_TABLE_NAME;
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- Nurse nurse = new Nurse();
- nurse.setNurseId(cursor.getString(0));
- nurse.setNurseFirstName(cursor.getString(1));
- nurse.setNurseLastName(cursor.getString(2));
- nurse.setNurseDepartment(cursor.getString(3));
- nurse.setNursePassword(cursor.getString(4));
- // Adding contact to list
- nurseList.add(nurse);
- } while (cursor.moveToNext());
- }
- // return contact list
- return nurseList;
- }
- }
- package com.example.mogli.henabenparekhrajthakkar_comp304_lab4;
- /**
- * Created by Mogli on 11/30/2017.
- */
- public class Nurse {
- String nurseId, nurse_firstName, nurse_lastName, nurse_department, nurse_password;
- public Nurse()
- {
- }
- public Nurse(String nurseId, String nurse_firstName, String nurse_lastName, String nurse_department, String nurse_password)
- {
- this.nurseId = nurseId;
- this.nurse_firstName = nurse_firstName;
- this.nurse_lastName = nurse_lastName;
- this.nurse_department = nurse_department;
- this.nurse_password = nurse_password;
- }
- public void setNurseId(String nurseId)
- {
- this.nurseId = nurseId;
- }
- public void setNurseFirstName(String nurse_firstName)
- {
- this.nurse_firstName = nurse_firstName;
- }
- public void setNurseLastName(String nurse_lastName)
- {
- this.nurse_lastName = nurse_lastName;
- }
- public void setNurseDepartment(String nurse_department)
- {
- this.nurse_department = nurse_department;
- }
- public void setNursePassword(String nurse_password)
- {
- this.nurse_password = nurse_password;
- }
- public String getNurseId()
- {
- return nurseId;
- }
- public String getNurseFirstName()
- {
- return nurse_firstName;
- }
- public String getNurseLastName()
- {
- return nurse_lastName;
- }
- public String getNurseDepartment()
- {
- return nurse_department;
- }
- public String getNursePassword()
- {
- return nurse_password;
- }
- }
- package com.example.mogli.henabenparekhrajthakkar_comp304_lab4;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.util.List;
- public class login_page extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login_page);
- SqlHelper helper = new SqlHelper(this);
- helper.addNurses(new Nurse("john123","John","Doe","D1","123john"));
- helper.addNurses(new Nurse("mona123","mona","dona","D2","123mona"));
- helper.addNurses(new Nurse("albert123","albert","white","D3","123albert"));
- helper.addNurses(new Nurse("bob123","bob","tob","D4","123bob"));
- // Reading all shops
- Log.d("Reading: ", "Reading all Nurses..");
- List<Nurse> nurses = helper.getAllNurses();
- for (Nurse nurse : nurses) {
- String log = "Id: " + nurse.getNurseId() + " , First Name: " + nurse.getNurseFirstName()
- + " ,Password: " + nurse.getNursePassword();
- // Writing shops to log
- Log.d("Nurse: : ", log);
- }
- }
- public void onLogin(View view)
- {
- SqlHelper helper = new SqlHelper(this);
- EditText a = (EditText) findViewById(R.id.username_tb);
- String username = a.toString();
- EditText b = (EditText) findViewById(R.id.password_tb);
- String password = b.toString();
- boolean getPass = helper.checkUser(username,password);
- String gp = Boolean.toString(getPass);
- Log.d("getPass", gp);
- if(getPass)
- {
- Toast pass = Toast.makeText(login_page.this, "Username and password do match!", Toast.LENGTH_SHORT);
- pass.show();
- }
- /* else
- {
- Toast pass = Toast.makeText(login_page.this, "Username and password do not match!", Toast.LENGTH_SHORT);
- pass.show();
- }*/
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout 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="com.example.mogli.henabenparekhrajthakkar_comp304_lab4.login_page">
- <TableLayout
- android:layout_width="368dp"
- android:layout_height="495dp"
- tools:layout_editor_absoluteX="9dp"
- tools:layout_editor_absoluteY="9dp">
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <EditText
- android:id="@+id/username_tb"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ems="10"
- android:hint="@string/username"
- android:inputType="textPersonName" />
- </TableRow>
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <EditText
- android:id="@+id/password_tb"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ems="10"
- android:hint="@string/password"
- android:inputType="textPassword" />
- </TableRow>
- <TableRow
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="onLogin"
- android:text="@string/login_str" />
- </TableRow>
- </TableLayout>
- </android.support.constraint.ConstraintLayout>
Add Comment
Please, Sign In to add comment