Advertisement
Guest User

Untitled

a guest
Nov 4th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. package com.qistina.myguardian1;
  2.  
  3. import android.os.AsyncTask;
  4. import android.support.v7.app.AppCompatActivity;
  5.  
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11.  
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.ResultSet;
  15. import java.sql.Statement;
  16.  
  17. import static com.qistina.myguardian1.R.id.text;
  18.  
  19. public class MainActivity extends AppCompatActivity {
  20. private EditText useremail, password, name, emailReg, pwReg1, pwReg2, phoneNum;
  21. private String ustr, pstr, namestr, eregstr, pw1, pw2, numreg;
  22. public TextView text;
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28. useremail = (EditText) findViewById(R.id.uemail);
  29. password = (EditText) findViewById(R.id.upw);
  30. text = (TextView) findViewById(R.id.welcomeText);
  31.  
  32.  
  33.  
  34.  
  35. }
  36. public void login (View v){
  37. ustr = useremail.getText().toString();
  38. pstr = password.getText().toString();
  39. Login login = new Login();
  40. login.execute(ustr, pstr);
  41. System.out.println();
  42. //System.out.println(ustr);
  43. }
  44.  
  45. public void register (View v){
  46. namestr = name.getText().toString();
  47. eregstr = emailReg.getText().toString();
  48. numreg = phoneNum.getText().toString();
  49. pw1 = pwReg1.getText().toString();
  50. pw2 = pwReg2.getText().toString();
  51. //name, emailReg, pwReg1, pwReg2, phoneNum
  52. //namestr, eregstr, pw1, pw2, numreg
  53.  
  54. if (pw1.equals(pw2)) {
  55. Register register = new Register();
  56. register.execute(namestr, eregstr, pw1, numreg);
  57. }else{
  58. Toast toast = Toast.makeText(getApplicationContext(),(CharSequence) "Passwords don't match. Try again", Toast.LENGTH_SHORT);
  59. toast.show();
  60. }
  61.  
  62. System.out.println();
  63. //System.out.println(ustr);
  64. }
  65. private class Login extends AsyncTask<String, Void, Integer> {
  66.  
  67. @Override
  68. protected Integer doInBackground(String... users) {
  69. String uname = users[0];
  70. String pw = users[1];
  71. String result = new String();
  72. result = "";
  73. int x=0;
  74. try{
  75. Class.forName("com.mysql.jdbc.Driver");
  76. Connection con = DriverManager.getConnection("jdbc:mysql://192.168.43.17:3306/myguardian","myguardian","fyp123");
  77. Statement stmt = con.createStatement();
  78. ResultSet rs = stmt.executeQuery("select * from userprofile where email = '"+uname+"' and password = '"+pw+"';");
  79. while(rs.next()) {
  80. //result = rs.getString(2)+rs.getString(3);
  81. x++;
  82. }
  83. con.close();
  84. return x;
  85. }catch(Exception e) {
  86. System.out.println(e);
  87. return x;
  88. }
  89. }
  90. @Override
  91. protected void onPostExecute(Integer result){
  92. if (result == 1){
  93. setContentView(R.layout.activity_maps);
  94. // text.setText("Logged in");
  95. }
  96. else {
  97. text.setText("Wrong email/password. Please try again.");
  98. }
  99. }
  100. }
  101. private class Register extends
  102. AsyncTask<String, Void, Integer> {
  103.  
  104. @Override
  105. protected Integer doInBackground(String... userprofile) {
  106. String name = userprofile[0];
  107. String email = userprofile[1];
  108. String pw1 = userprofile[2];
  109. String phoneNum = userprofile[3];
  110. int y=0;
  111. try{
  112. Class.forName("com.mysql.jdbc.Driver");
  113. Connection con = DriverManager.getConnection("jdbc:mysql://192.168.43.17:3306/myguardian","myguardian","fyp123");
  114. Statement stmt = con.createStatement();
  115. Statement stmt2 = con.createStatement();
  116. ResultSet rs = stmt.executeQuery("select * from userprofile where email = '"+email+"';");
  117. while(rs.next()) {
  118. //result = rs.getString(2)+rs.getString(3);
  119. y++;
  120. }
  121. if (y == 1) { //email exists
  122. con.close();
  123. return 0;
  124. }
  125. else {
  126. stmt2.executeUpdate("INSERT INTO userprofile VALUES(name = '"+name+"', email = '"+email+"', password = '"+pw1+"', phonenum = '"+phoneNum+"' );");
  127. con.close();
  128. return 1;
  129. }
  130. }catch(Exception e) {
  131. System.out.println(e);
  132. return y;
  133. }
  134. }
  135. @Override
  136. protected void onPostExecute(Integer result){
  137. if (result == 1){
  138. setContentView(R.layout.home);
  139. // text.setText("Logged in");
  140. }
  141. else {
  142. Toast toast = Toast.makeText(getApplicationContext(),(CharSequence) "Email exists. Please log in or use a different email", Toast.LENGTH_SHORT);
  143. toast.show();
  144. }
  145. }
  146. }
  147. public void onClickRegister(View View) {
  148. setContentView(R.layout.register);
  149. name = (EditText) findViewById(R.id.inputName);
  150. emailReg = (EditText) findViewById(R.id.inputEmail);
  151. pwReg1 = (EditText) findViewById(R.id.inputPW);
  152. pwReg2 = (EditText) findViewById(R.id.conPW);
  153. phoneNum = (EditText) findViewById(R.id.inputNum);
  154. }
  155.  
  156.  
  157. public void onClickLogin(View View) {
  158. setContentView(R.layout.activity_main);
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement