Guest User

Untitled

a guest
Dec 4th, 2017
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.10 KB | None | 0 0
  1. package com.example.mogli.henabenparekhrajthakkar_comp304_lab4;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import android.util.Log;
  9. import android.widget.Toast;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. /**
  15. * Created by Mogli on 11/30/2017.
  16. */
  17.  
  18. public class SqlHelper extends SQLiteOpenHelper {
  19. private static final int DATABASE_VERSION = 1;
  20. private static final String DATABASE_NAME = "hospital.db";
  21. //nurse table name & columns
  22. private static final String NURSE_TABLE_NAME = "nurses";
  23. private static final String NURSE_COLUMN_ID = "nurseId";
  24. private static final String NURSE_COLUMN_FIRST_NAME = "nurseFirstName";
  25. private static final String NURSE_COLUMN_LAST_NAME = "nurseLastName";
  26. private static final String NURSE_COLUMN_DEPARTMENT = "nurseDepartment";
  27. private static final String NURSE_COLUMN_PASSWORD = "nursePassword";
  28. //doctor table name & columns
  29. private static final String DOCTOR_TABLE_NAME = "doctors";
  30. private static final String DOCTOR_COLUMN_ID = "doctorId";
  31. private static final String DOCTOR_COLUMN_FIRST_NAME = "doctorFirstName";
  32. private static final String DOCTOR_COLUMN_LAST_NAME = "doctorLastName";
  33. private static final String DOCTOR_COLUMN_DEPARTMENT = "doctorDepartment";
  34. private static final String DOCTOR_COLUMN_PASSWORD = "doctorPassword";
  35.  
  36. private static final String NURSE_TABLE_CREATE = "create table if not exists nurses( nurseId text primary key not null, "
  37. + "nurseFirstName text not null, nurseLastName text not null, nurseDepartment text not null, nursePassword text not null );";
  38.  
  39. private static final String DOCTOR_TABLE_CREATE = "create table if not exists doctors( doctorId text primary key not null, "
  40. + "doctorFirstName text not null, doctorLastName text not null, doctorDepartment text not null, doctorPassword text not null );";
  41.  
  42. //insert query
  43. /*private String insertIntoNurse = "INSERT INTO nurses (nurseId, nurseFirstName, nurseLastName, nurseDepartment, nursePassword)"
  44. +"VALUES ('john123', 'john', 'doe', 'd1', '123john' ), ('jonahdoe', 'jonah', 'doe', 'd2', '123jonah'), ('hannah123', 'hannah', 'montana', 'd3', 'hm123');";*/
  45.  
  46. public SqlHelper(Context context)
  47. {
  48. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  49. }
  50.  
  51. @Override
  52. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  53. try {
  54. sqLiteDatabase.execSQL(NURSE_TABLE_CREATE);
  55. sqLiteDatabase.execSQL(DOCTOR_TABLE_CREATE);
  56. }
  57. catch(Exception e)
  58. {
  59. Log.d("myMsg", "Error creating table");
  60. }
  61. }
  62. @Override
  63. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  64. String nurse_query = "DROP TABLE IF EXISTS " + NURSE_TABLE_NAME;
  65. String doctor_query = "DROP TABLE IF EXISTS " + DOCTOR_TABLE_NAME;
  66. sqLiteDatabase.execSQL(nurse_query);
  67. sqLiteDatabase.execSQL(doctor_query);
  68. onCreate(sqLiteDatabase);
  69. }
  70. public boolean checkUser(String email, String password) {
  71.  
  72. // array of columns to fetch
  73. String[] columns = {
  74. NURSE_COLUMN_ID
  75. };
  76. SQLiteDatabase db = this.getReadableDatabase();
  77. // selection criteria
  78. String selection = NURSE_COLUMN_ID + " = ?" + " AND " + NURSE_COLUMN_PASSWORD + " = ?";
  79.  
  80. // selection arguments
  81. String[] selectionArgs = {email, password};
  82.  
  83. // query user table with conditions
  84. /**
  85. * Here query function is used to fetch records from user table this function works like we use sql query.
  86. * SQL query equivalent to this query function is
  87. * SELECT user_id FROM user WHERE user_email = 'jack@androidtutorialshub.com' AND user_password = 'qwerty';
  88. */
  89. Cursor cursor = db.query(NURSE_TABLE_NAME, //Table to query
  90. columns, //columns to return
  91. selection, //columns for the WHERE clause
  92. selectionArgs, //The values for the WHERE clause
  93. null, //group the rows
  94. null, //filter by row groups
  95. null); //The sort order
  96.  
  97. int cursorCount = cursor.getCount();
  98.  
  99. cursor.close();
  100. db.close();
  101. if (cursorCount > 0) {
  102. return true;
  103. }
  104.  
  105. return false;
  106. }
  107. public String searchPass(String NURSE_COLUMN_ID)
  108. {
  109. SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
  110. String query = "select nurseId, nursePassword from "+ NURSE_TABLE_NAME;
  111. Cursor cursor =sqLiteDatabase.rawQuery(query, null);
  112. String a, b;
  113. b = "";
  114. if(cursor.moveToFirst())
  115. {
  116. do
  117. {
  118. a = cursor.getString(0);
  119. b = cursor.getString(1);
  120. if(a.equals(NURSE_COLUMN_ID))
  121. {
  122. b = cursor.getString(1);
  123. break;
  124. }
  125. }
  126. while(cursor.moveToNext());
  127. }
  128. return b;
  129. }
  130. public void addNurses(Nurse nurse)
  131. {
  132. SQLiteDatabase db = this.getWritableDatabase();
  133.  
  134. ContentValues values = new ContentValues();
  135. values.put(NURSE_COLUMN_ID, nurse.getNurseId());
  136. values.put(NURSE_COLUMN_FIRST_NAME, nurse.getNurseFirstName());
  137. values.put(NURSE_COLUMN_LAST_NAME, nurse.getNurseLastName());
  138. values.put(NURSE_COLUMN_DEPARTMENT, nurse.getNurseDepartment());
  139. values.put(NURSE_COLUMN_PASSWORD, nurse.getNursePassword());
  140. db.insert(NURSE_TABLE_NAME,null,values);
  141. db.close();
  142. }
  143. public List<Nurse> getAllNurses() {
  144. List<Nurse> nurseList = new ArrayList<Nurse>();
  145. // Select All Query
  146. String selectQuery = "SELECT * FROM " + NURSE_TABLE_NAME;
  147.  
  148. SQLiteDatabase db = this.getWritableDatabase();
  149. Cursor cursor = db.rawQuery(selectQuery, null);
  150.  
  151. // looping through all rows and adding to list
  152. if (cursor.moveToFirst()) {
  153. do {
  154. Nurse nurse = new Nurse();
  155. nurse.setNurseId(cursor.getString(0));
  156. nurse.setNurseFirstName(cursor.getString(1));
  157. nurse.setNurseLastName(cursor.getString(2));
  158. nurse.setNurseDepartment(cursor.getString(3));
  159. nurse.setNursePassword(cursor.getString(4));
  160. // Adding contact to list
  161. nurseList.add(nurse);
  162. } while (cursor.moveToNext());
  163. }
  164.  
  165. // return contact list
  166. return nurseList;
  167. }
  168. }
  169.  
  170. package com.example.mogli.henabenparekhrajthakkar_comp304_lab4;
  171.  
  172. /**
  173. * Created by Mogli on 11/30/2017.
  174. */
  175.  
  176. public class Nurse {
  177. String nurseId, nurse_firstName, nurse_lastName, nurse_department, nurse_password;
  178. public Nurse()
  179. {
  180.  
  181. }
  182. public Nurse(String nurseId, String nurse_firstName, String nurse_lastName, String nurse_department, String nurse_password)
  183. {
  184. this.nurseId = nurseId;
  185. this.nurse_firstName = nurse_firstName;
  186. this.nurse_lastName = nurse_lastName;
  187. this.nurse_department = nurse_department;
  188. this.nurse_password = nurse_password;
  189. }
  190. public void setNurseId(String nurseId)
  191. {
  192. this.nurseId = nurseId;
  193. }
  194. public void setNurseFirstName(String nurse_firstName)
  195. {
  196. this.nurse_firstName = nurse_firstName;
  197. }
  198. public void setNurseLastName(String nurse_lastName)
  199. {
  200. this.nurse_lastName = nurse_lastName;
  201. }
  202. public void setNurseDepartment(String nurse_department)
  203. {
  204. this.nurse_department = nurse_department;
  205. }
  206. public void setNursePassword(String nurse_password)
  207. {
  208. this.nurse_password = nurse_password;
  209. }
  210. public String getNurseId()
  211. {
  212. return nurseId;
  213. }
  214. public String getNurseFirstName()
  215. {
  216. return nurse_firstName;
  217.  
  218. }
  219. public String getNurseLastName()
  220. {
  221. return nurse_lastName;
  222. }
  223. public String getNurseDepartment()
  224. {
  225. return nurse_department;
  226. }
  227. public String getNursePassword()
  228. {
  229. return nurse_password;
  230. }
  231. }
  232.  
  233. package com.example.mogli.henabenparekhrajthakkar_comp304_lab4;
  234.  
  235. import android.support.v7.app.AppCompatActivity;
  236. import android.os.Bundle;
  237. import android.util.Log;
  238. import android.view.View;
  239. import android.widget.EditText;
  240. import android.widget.Toast;
  241.  
  242. import java.util.List;
  243.  
  244. public class login_page extends AppCompatActivity {
  245.  
  246.  
  247. @Override
  248. protected void onCreate(Bundle savedInstanceState) {
  249. super.onCreate(savedInstanceState);
  250. setContentView(R.layout.activity_login_page);
  251.  
  252. SqlHelper helper = new SqlHelper(this);
  253. helper.addNurses(new Nurse("john123","John","Doe","D1","123john"));
  254. helper.addNurses(new Nurse("mona123","mona","dona","D2","123mona"));
  255. helper.addNurses(new Nurse("albert123","albert","white","D3","123albert"));
  256. helper.addNurses(new Nurse("bob123","bob","tob","D4","123bob"));
  257. // Reading all shops
  258. Log.d("Reading: ", "Reading all Nurses..");
  259. List<Nurse> nurses = helper.getAllNurses();
  260.  
  261. for (Nurse nurse : nurses) {
  262. String log = "Id: " + nurse.getNurseId() + " , First Name: " + nurse.getNurseFirstName()
  263. + " ,Password: " + nurse.getNursePassword();
  264. // Writing shops to log
  265. Log.d("Nurse: : ", log);
  266. }
  267. }
  268. public void onLogin(View view)
  269. {
  270. SqlHelper helper = new SqlHelper(this);
  271. EditText a = (EditText) findViewById(R.id.username_tb);
  272. String username = a.toString();
  273. EditText b = (EditText) findViewById(R.id.password_tb);
  274. String password = b.toString();
  275.  
  276. boolean getPass = helper.checkUser(username,password);
  277. String gp = Boolean.toString(getPass);
  278. Log.d("getPass", gp);
  279. if(getPass)
  280. {
  281. Toast pass = Toast.makeText(login_page.this, "Username and password do match!", Toast.LENGTH_SHORT);
  282. pass.show();
  283. }
  284. /* else
  285. {
  286. Toast pass = Toast.makeText(login_page.this, "Username and password do not match!", Toast.LENGTH_SHORT);
  287. pass.show();
  288. }*/
  289. }
  290. }
  291.  
  292. <?xml version="1.0" encoding="utf-8"?>
  293. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  294. xmlns:app="http://schemas.android.com/apk/res-auto"
  295. xmlns:tools="http://schemas.android.com/tools"
  296. android:layout_width="match_parent"
  297. android:layout_height="match_parent"
  298. tools:context="com.example.mogli.henabenparekhrajthakkar_comp304_lab4.login_page">
  299.  
  300. <TableLayout
  301. android:layout_width="368dp"
  302. android:layout_height="495dp"
  303. tools:layout_editor_absoluteX="9dp"
  304. tools:layout_editor_absoluteY="9dp">
  305.  
  306. <TableRow
  307. android:layout_width="match_parent"
  308. android:layout_height="match_parent">
  309.  
  310. <EditText
  311. android:id="@+id/username_tb"
  312. android:layout_width="wrap_content"
  313. android:layout_height="wrap_content"
  314. android:ems="10"
  315. android:hint="@string/username"
  316. android:inputType="textPersonName" />
  317. </TableRow>
  318.  
  319. <TableRow
  320. android:layout_width="match_parent"
  321. android:layout_height="match_parent">
  322.  
  323. <EditText
  324. android:id="@+id/password_tb"
  325. android:layout_width="wrap_content"
  326. android:layout_height="wrap_content"
  327. android:ems="10"
  328. android:hint="@string/password"
  329. android:inputType="textPassword" />
  330. </TableRow>
  331.  
  332. <TableRow
  333. android:layout_width="match_parent"
  334. android:layout_height="match_parent">
  335.  
  336. <Button
  337. android:id="@+id/button"
  338. android:layout_width="wrap_content"
  339. android:layout_height="wrap_content"
  340. android:onClick="onLogin"
  341. android:text="@string/login_str" />
  342. </TableRow>
  343. </TableLayout>
  344.  
  345. </android.support.constraint.ConstraintLayout>
Add Comment
Please, Sign In to add comment