Advertisement
Guest User

su

a guest
Oct 24th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package com.musyrif.loginmyguardian;
  2.  
  3. import android.os.AsyncTask;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.TextView;
  9.  
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.Statement;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16.     private EditText username, password;
  17.     private TextView text;
  18.     private String ustr, pstr;
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.         username = (EditText) findViewById(R.id.username);
  24.         password = (EditText) findViewById(R.id.password);
  25.         text = (TextView) findViewById(R.id.textView);
  26.  
  27.         System.out.println(ustr);
  28.  
  29.     }
  30.     public void click (View v){
  31.         ustr = username.getText().toString();
  32.         pstr = password.getText().toString();
  33.         new get().execute(ustr, pstr);
  34.     }
  35.     private class get extends AsyncTask<String, Void, Integer>{
  36.  
  37.         @Override
  38.         protected Integer doInBackground(String... users) {
  39.             String uname= users[0];
  40.             String pw = users[1];
  41.             String result = new String();
  42.             result = "";
  43.             int x=0;
  44.             try{
  45.                 Class.forName("com.mysql.jdbc.Driver");
  46.                 Connection con = DriverManager.getConnection("jdbc:mysql://192.168.43.121:3306/myguardian","mg","mg123");
  47.                 Statement stmt = con.createStatement();
  48.                 ResultSet rs = stmt.executeQuery("select * from users where username = '"+uname+"' and password = '"+pw+"';");
  49.                 while(rs.next()) {
  50.                     //result = rs.getString(2)+rs.getString(3);
  51.                     x++;
  52.                 }
  53.                 con.close();
  54.                 return x;
  55.             }catch(Exception e) {
  56.                 System.out.println(e);
  57.                 return x;
  58.             }
  59.         }
  60.         @Override
  61.         protected void onPostExecute(Integer result){
  62.             if (result == 1){
  63.                 text.setText("Logged in");
  64.                 setContentView(R.layout.loggedin);
  65.             }
  66.             else {
  67.                 text.setText("Wrong credentials");
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement