Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.qistina.myguardian1;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- import static com.qistina.myguardian1.R.id.text;
- public class MainActivity extends AppCompatActivity {
- private EditText useremail, password, name, emailReg, pwReg1, pwReg2, phoneNum;
- private String ustr, pstr, namestr, eregstr, pw1, pw2, numreg;
- public TextView text;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- useremail = (EditText) findViewById(R.id.uemail);
- password = (EditText) findViewById(R.id.upw);
- text = (TextView) findViewById(R.id.welcomeText);
- }
- public void login (View v){
- ustr = useremail.getText().toString();
- pstr = password.getText().toString();
- Login login = new Login();
- login.execute(ustr, pstr);
- System.out.println();
- //System.out.println(ustr);
- }
- public void register (View v){
- namestr = name.getText().toString();
- eregstr = emailReg.getText().toString();
- numreg = phoneNum.getText().toString();
- pw1 = pwReg1.getText().toString();
- pw2 = pwReg2.getText().toString();
- //name, emailReg, pwReg1, pwReg2, phoneNum
- //namestr, eregstr, pw1, pw2, numreg
- if (pw1.equals(pw2)) {
- Register register = new Register();
- register.execute(namestr, eregstr, pw1, numreg);
- }else{
- Toast toast = Toast.makeText(getApplicationContext(),(CharSequence) "Passwords don't match. Try again", Toast.LENGTH_SHORT);
- toast.show();
- }
- System.out.println();
- //System.out.println(ustr);
- }
- private class Login extends AsyncTask<String, Void, Integer> {
- @Override
- protected Integer doInBackground(String... users) {
- String uname = users[0];
- String pw = users[1];
- String result = new String();
- result = "";
- int x=0;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://192.168.43.17:3306/myguardian","myguardian","fyp123");
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select * from userprofile where email = '"+uname+"' and password = '"+pw+"';");
- while(rs.next()) {
- //result = rs.getString(2)+rs.getString(3);
- x++;
- }
- con.close();
- return x;
- }catch(Exception e) {
- System.out.println(e);
- return x;
- }
- }
- @Override
- protected void onPostExecute(Integer result){
- if (result == 1){
- setContentView(R.layout.activity_maps);
- // text.setText("Logged in");
- }
- else {
- text.setText("Wrong email/password. Please try again.");
- }
- }
- }
- private class Register extends
- AsyncTask<String, Void, Integer> {
- @Override
- protected Integer doInBackground(String... userprofile) {
- String name = userprofile[0];
- String email = userprofile[1];
- String pw1 = userprofile[2];
- String phoneNum = userprofile[3];
- int y=0;
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://192.168.43.17:3306/myguardian","myguardian","fyp123");
- Statement stmt = con.createStatement();
- Statement stmt2 = con.createStatement();
- ResultSet rs = stmt.executeQuery("select * from userprofile where email = '"+email+"';");
- while(rs.next()) {
- //result = rs.getString(2)+rs.getString(3);
- y++;
- }
- if (y == 1) { //email exists
- con.close();
- return 0;
- }
- else {
- stmt2.executeUpdate("INSERT INTO userprofile VALUES(name = '"+name+"', email = '"+email+"', password = '"+pw1+"', phonenum = '"+phoneNum+"' );");
- con.close();
- return 1;
- }
- }catch(Exception e) {
- System.out.println(e);
- return y;
- }
- }
- @Override
- protected void onPostExecute(Integer result){
- if (result == 1){
- setContentView(R.layout.home);
- // text.setText("Logged in");
- }
- else {
- Toast toast = Toast.makeText(getApplicationContext(),(CharSequence) "Email exists. Please log in or use a different email", Toast.LENGTH_SHORT);
- toast.show();
- }
- }
- }
- public void onClickRegister(View View) {
- setContentView(R.layout.register);
- name = (EditText) findViewById(R.id.inputName);
- emailReg = (EditText) findViewById(R.id.inputEmail);
- pwReg1 = (EditText) findViewById(R.id.inputPW);
- pwReg2 = (EditText) findViewById(R.id.conPW);
- phoneNum = (EditText) findViewById(R.id.inputNum);
- }
- public void onClickLogin(View View) {
- setContentView(R.layout.activity_main);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement