Database connection, check data, login page
I'm doing Major Project on my final year and I'm very new to Android plus I;m not good at codings. I need help with my login page. I've created something like a database connection java file which is this:
package one.two;
import java.util.List;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
public class UserDB
{
public String KEY_USERNAME = "Username";
public String KEY_PASSWORD = "Password";
public String KEY_LNAME = "LastName";
public String KEY_FNAME = "FirstName";
private static final String DATABASE_NAME = "Users";
private static final String DATABASE_TABLE = "User";
private static final int DATABASE_VERSION = 1;
private static Context context;
private static DatabaseHelper DBHelper;
private static SQLiteDatabase d开发者_Go百科b;
public UserDB(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, "Users", null, 1);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
@Override
public void onCreate(SQLiteDatabase db)
{
}
}//end DatabaseHelper
// ---opens the database---
public UserDB open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close()
{
DBHelper.close();
}
}
I've already created a database for users using SQLite. The database name is Users and the table is called User. The records inside the table are Username, Password, LastName, FirstName. I've inserted one user's info into the database. The problem is I do not know whether my UserDB.java is correct.
And I've also created login.java. Hardcoded Login page:
package one.two;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
public class Login extends Activity implements OnClickListener{
UserDB db = new UserDB(this);
/** Called when the activity is first created. */
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
//private Button btnRegister;
private TextView lblResult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// Get the EditText and Button References
etUsername = (EditText)findViewById(R.id.usernametxt);
etPassword = (EditText)findViewById(R.id.passwordtxt);
btnLogin = (Button)findViewById(R.id.btnLogin);
//btnRegister = (Button)findViewById(R.id.btnRegister);
lblResult = (TextView)findViewById(R.id.msglbl);
//Cursor c = (Cursor) db.getAllTitles();
Button btnArrival = (Button) findViewById(R.id.btnRegister);
btnArrival.setOnClickListener(this);
// Set Click Listener
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Check Login
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
if(username.equals("guest") && password.equals("guest")){
lblResult.setText("Login successful.");
} else {
lblResult.setText("Login failed. Username and/or password doesn't match.");
}
}
});
}
public void onClick(View v)
{
Intent intent = new Intent(this, DatabaseActivity.class);
startActivity(intent);
}
}
So I want to know how I should apply the database connection on the login.java. Should I insert database connection something like db.Open();? I studied ASP.Net a few months back and I kind of forget most of what I've learnt.
So how should I open the database connection on login.java and how to check with database whether the user enters the right username and password?
You gotta search online for more database examples.
A good website would be anddev.org
精彩评论