Guest User

Untitled

a guest
Apr 10th, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import android.content.Context;
  2. import android.database.Cursor;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.support.v7.app.AlertDialog;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11.  
  12. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  13. EditText username;
  14. EditText password;
  15. TextView attempts;
  16. Button lbtn;
  17. // int count = 5;
  18. SQLiteDatabase db;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. username = (EditText)findViewById(R.id.edtuname);
  24. password = (EditText)findViewById(R.id.edtpsw);
  25. attempts = (TextView)findViewById(R.id.txtcount);
  26. lbtn = (Button)findViewById(R.id.lbtn);
  27.  
  28. db=openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
  29. db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
  30. db.execSQL("INSERT INTO student VALUES('121','sujith','68');");
  31. lbtn=(Button)findViewById(R.id.lbtn);
  32. lbtn.setOnClickListener(this);
  33. }
  34. @Override
  35. public void onClick(View v)
  36. {
  37. if(username.getText().toString().trim().length()==0||
  38. password.getText().toString().trim().length()==0)
  39. {
  40. showMessage("Error", "Please enter all values");
  41. return;
  42. }
  43. else
  44. {
  45. Cursor c=db.rawQuery("SELECT name FROM student WHERE rollno='"+password.getText()+"'", null);
  46. if(c.moveToFirst())
  47. {
  48. if(username.getText().toString().equals(c.getString(0)))
  49. attempts.setText(c.getString(0)+" Login Successful" );
  50. else
  51. attempts.setText(c.getString(0)+" Login Failed" );
  52. }
  53.  
  54. }
  55.  
  56. }
  57. public void showMessage(String title,String message)
  58. {
  59. AlertDialog.Builder builder=new AlertDialog.Builder(this);
  60. builder.setCancelable(true);
  61. builder.setTitle(title);
  62. builder.setMessage(message);
  63. builder.show();
  64. }
  65. }
Add Comment
Please, Sign In to add comment