hi im stuck again
this is my code. can anyone kindly teach me where do i insert my data into database so that when i click on the login button it will check the username and password typed by the user with the database. if its correct it will move on the menu page.
my code below
XML
this is my code. can anyone kindly teach me where do i insert my data into database so that when i click on the login button it will check the username and password typed by the user with the database. if its correct it will move on the menu page.
my code below
Code:
package com.logintesting;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LogintestingActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText txtUserName = (EditText)findViewById(R.id.txtUsername);
final EditText txtPassword = (EditText)findViewById(R.id.txtPassword);
Button btnLogin = (Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String username = txtUserName.getText().toString();
String password = txtPassword.getText().toString();
try{
if(username.length() > 0 && password.length() >0)
{
db dbUser = new db(LogintestingActivity.this);
dbUser.open();
if(dbUser.Login(username, password))
{
Toast.makeText(LogintestingActivity.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(LogintestingActivity.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
}
dbUser.close();
}
}catch(Exception e)
{
Toast.makeText(LogintestingActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
}
}
Code:
package com.logintesting;
import java.sql.SQLException;
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;
public class db {
public static final String KEY_ROWID = "_id";
public static final String KEY_USERNAME= "username";
public static final String KEY_PASSWORD = "password";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "usersdb";
private static final String DATABASE_TABLE = "users";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table users (_id integer primary key autoincrement, "
+ "username text not null, "
+ "password text not null);";
private Context context = null;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public db(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
}
public void open() throws SQLException
{
db = DBHelper.getWritableDatabase();
}
public void close()
{
DBHelper.close();
}
public long AddUser(String username, String password)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_USERNAME, username);
initialValues.put(KEY_PASSWORD, password);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean Login(String username, String password) throws SQLException
{
Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
if (mCursor != null) {
if(mCursor.getCount() > 0)
{
return true;
}
}
return false;
}
}
XML
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="10dip">
<TextView
android:id="@+id/lblUsername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp" android:text="Username:"
android:textStyle="bold"
android:layout_marginBottom="8dip"
/>
<EditText
android:id="@+id/txtUsername"
android:layout_below="@id/lblUsername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginBottom="8dip"
/>
<TextView
android:id="@+id/lblPassword"
android:layout_below="@id/txtUsername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp" android:text="Password:"
android:textStyle="bold"
android:layout_marginBottom="8dip"
/>
<EditText
android:id="@+id/txtPassword"
android:layout_below="@id/lblPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginBottom="8dip"
android:password="true"
/>
<Button
android:id="@+id/btnLogin"
android:layout_below="@id/txtPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Login"
/>
</RelativeLayout>