java - How do I check that login and password entered is valid -
i trying create login funcitonality in android studio in verify entered password , login exist in database , go together(based on information in login database) should happen when user clicks "button check login" if info accurate should take user welcome screen.
i struggling how check information according database. please help!
please @ following :
databasehelper.java
package com.example.login; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; import android.util.log; public class databasehelper extends sqliteopenhelper { public databasehelper(context context, string name,cursorfactory factory, int version) { super(context, name, factory, version); } // called when no database exists in disk , helper class needs // create new one. @override public void oncreate(sqlitedatabase _db) { _db.execsql(logindatabaseadapter.database_create); } // called when there database version mismatch meaning version // of database on disk needs upgraded current version. @override public void onupgrade(sqlitedatabase _db, int _oldversion, int _newversion) { // log version upgrade. log.w("taskdbadapter", "upgrading version " +_oldversion + " " +_newversion + ", destroy old data"); // upgrade existing database conform new version. multiple // previous versions can handled comparing _oldversion , _newversion // values. // simplest case drop old table , create new one. _db.execsql("drop table if exists " + "template"); // create new one. oncreate(_db); } }
logindatabaseadapter.java
public class logindatabaseadapter { static final string database_name = "login.db"; static final int database_version = 1; public static final int name_column = 1; // todo: create public field each column in table. // sql statement create new database. static final string database_create = "create table "+"login"+ "( " +"id"+" integer primary key autoincrement,"+ "username text,password text); "; // variable hold database instance public sqlitedatabase db; // context of application using database. private final context context; // database open/upgrade helper private databasehelper dbhelper; public logindatabaseadapter(context _context) { context = _context; dbhelper = new databasehelper(context, database_name, null, database_version); } // method openthe database public logindatabaseadapter open() throws sqlexception { db = dbhelper.getwritabledatabase(); return this; } // method close database public void close() { db.close(); } // method returns instance of database public sqlitedatabase getdatabaseinstance() { return db; } // method insert record in table public void insertentry(string username,string password) { contentvalues newvalues = new contentvalues(); // assign values each column. newvalues.put("username", username); newvalues.put("password",password); // insert row table db.insert("login", null, newvalues); toast.maketext(context, "user info saved", toast.length_long).show(); } // method delete record of username public int deleteentry(string username) { string where="username=?"; int numberofentriesdeleted= db.delete("login", where, new string[]{username}) ; toast.maketext(context, "number fo entry deleted : "+numberofentriesdeleted, toast.length_long).show(); return numberofentriesdeleted; } // method password of username public string getsinlgeentry(string username) { cursor cursor=db.query("login", null, " username=?", new string[]{username}, null, null, null); if(cursor.getcount()<1) // username not exist return "not exist"; cursor.movetofirst(); string password= cursor.getstring(cursor.getcolumnindex("password")); return password; } // method update existing record public void updateentry(string username,string password) { // create object of contentvalues contentvalues updatedvalues = new contentvalues(); // assign values each column. updatedvalues.put("username", username); updatedvalues.put("password",password); string where="username = ?"; db.update("login",updatedvalues, where, new string[]{username}); } }
signupactivity.java
public class signupactivity extends activity { edittext edittextusername,edittextpassword,edittextconfirmpassword; button btncreateaccount; logindatabaseadapter logindatabaseadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.signup); // instance of database adapter logindatabaseadapter=new logindatabaseadapter(this); logindatabaseadapter=logindatabaseadapter.open(); // refferences of views edittextusername=(edittext)findviewbyid(r.id.edittextusername); edittextpassword=(edittext)findviewbyid(r.id.edittextpassword); edittextconfirmpassword=(edittext)findviewbyid(r.id.edittextconfirmpassword); btncreateaccount=(button)findviewbyid(r.id.buttoncreateaccount); btncreateaccount.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // todo auto-generated method stub string username=edittextusername.gettext().tostring(); string password=edittextpassword.gettext().tostring(); string confirmpassword=edittextconfirmpassword.gettext().tostring(); // check if of fields vaccant if(username.equals("")||password.equals("")||confirmpassword.equals("")) { toast.maketext(getapplicationcontext(), "field vaccant", toast.length_long).show(); return; } // check if both password matches if(!password.equals(confirmpassword)) { toast.maketext(getapplicationcontext(), "password not matches", toast.length_long).show(); return; } else { // save data in database logindatabaseadapter.insertentry(username, password); toast.maketext(getapplicationcontext(), "account created ", toast.length_long).show(); } } }); } @override protected void ondestroy() { // todo auto-generated method stub super.ondestroy(); logindatabaseadapter.close(); } }
Comments
Post a Comment