Guest User

Untitled

a guest
Feb 6th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. package mdad.project;
  2.  
  3. import com.example.manandhowproject.R;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.os.Bundle;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17.  
  18. public class Login extends Activity {
  19. SQLiteDatabase db;
  20. Button btnLogin, btnSignUp;
  21. EditText etUsername, etPassword;
  22. SQLiteOpenHelper dbhelper;
  23. Cursor cursor;
  24.  
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.login);
  29. etUsername = (EditText)findViewById(R.id.etUsername);
  30. etPassword = (EditText)findViewById(R.id.etPassword);
  31. Button button = (Button)findViewById(R.id.btnLogin);
  32.  
  33. button.setOnClickListener(new OnClickListener(){
  34. public void onClick (View V){
  35. if(login(etUsername.getText().toString(), etPassword.getText().toString()) == 1){
  36. Toast.makeText(getApplicationContext(), "Log In Success ! ", Toast.LENGTH_LONG).show();
  37. Intent msg1 = new Intent(Login.this, Userreg.class);
  38. startActivity(msg1);
  39. }else{
  40. Toast.makeText(getApplicationContext(), "Error Logging In, Please Try Again", Toast.LENGTH_LONG).show();
  41. }
  42.  
  43. }
  44. });
  45.  
  46. Button btnSignUp = (Button) findViewById (R.id.btnSignUp);
  47. btnSignUp.setOnClickListener(new OnClickListener (){
  48. public void onClick (View V) {
  49. Toast.makeText(getApplicationContext(), "Sign Up Success ! ", Toast.LENGTH_LONG).show();
  50.  
  51. String username= etUsername.getText().toString();
  52. String password= etPassword.getText().toString();
  53. String sql = "insert into Registration (Username ,Password) values( '"+username+"','"+password+"')";
  54. String result = updateTable(sql);
  55. etUsername.setText("");
  56. etPassword.setText("");
  57. }});
  58.  
  59. String sql="create table if not exists Registration (recld integer PRIMARY KEY autoincrement, Username text, Password text)";
  60. String result = createDatabase(sql, "Reg.db");
  61.  
  62. }
  63.  
  64. public int login(String username, String password){
  65.  
  66. String[] selectionArgs = new String[]{username, password};
  67. try
  68. {
  69. int i = 0;
  70. Cursor c = null;
  71. c = db.rawQuery("select * from Registration where username=? and password=?", selectionArgs);
  72. c.moveToFirst();
  73. i = c.getCount();
  74. c.close();
  75. return i;
  76. }
  77. catch(Exception e)
  78. {
  79. e.printStackTrace();
  80. }
  81. return 0;
  82. }
  83. String createDatabase(String sql, String dbName) {
  84. try{
  85. System.out.println(sql);
  86. db = SQLiteDatabase.openOrCreateDatabase("sdcard/" + dbName,null);
  87. db.beginTransaction();
  88. db.execSQL(sql);
  89. db.setTransactionSuccessful();
  90. db.endTransaction();
  91. }
  92.  
  93. catch (Exception e){
  94. Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
  95. System.out.println(e.toString());
  96. return ("error open DB");
  97. }
  98.  
  99. return "";
  100. }
  101.  
  102. String updateTable(String sql)
  103. {
  104. try{
  105. System.out.println(sql);
  106. db.beginTransaction();
  107. db.execSQL(sql);
  108. db.setTransactionSuccessful();
  109. db.endTransaction();
  110.  
  111. }catch (Exception e) { System.out.println(e.toString());
  112. return ("Error updating DB");
  113. }
  114. return ("DB updated");
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121. @Override
  122. public boolean onCreateOptionsMenu(Menu menu) {
  123. // Inflate the menu; this adds items to the action bar if it is present.
  124. getMenuInflater().inflate(R.menu.main, menu);
  125. return true;
  126. }
  127.  
  128. @Override
  129. public boolean onOptionsItemSelected(MenuItem item) {
  130. // Handle action bar item clicks here. The action bar will
  131. // automatically handle clicks on the Home/Up button, so long
  132. // as you specify a parent activity in AndroidManifest.xml.
  133. int id = item.getItemId();
  134. if (id == R.id.action_settings) {
  135. return true;
  136. }
  137. return super.onOptionsItemSelected(item);
  138. }
Add Comment
Please, Sign In to add comment