Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package info.androidhive.introslider;
- import android.annotation.SuppressLint;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.design.widget.TextInputLayout;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.text.Editable;
- import android.text.TextWatcher;
- import android.util.Log;
- import android.view.View;
- import android.view.WindowManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import android.os.StrictMode;
- import android.os.AsyncTask;
- import android.widget.ProgressBar;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class MainActivity extends AppCompatActivity {
- Toolbar toolbar;
- EditText username, password;
- TextInputLayout inputLayoutEmail, inputLayoutPassword;
- Button login;
- ProgressBar progressBar;
- Connection con;
- String un, pass, db, ip;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- inputLayoutEmail = (TextInputLayout) findViewById(R.id.input_layout_email);
- inputLayoutPassword = (TextInputLayout) findViewById(R.id.input_layout_password);
- login = (Button) findViewById(R.id.button);
- username = (EditText) findViewById(R.id.editText);
- password = (EditText) findViewById(R.id.editText2);
- progressBar = (ProgressBar) findViewById(R.id.progressBar);
- username.addTextChangedListener(new MyTextWatcher(username));
- password.addTextChangedListener(new MyTextWatcher(password));
- un = "";
- pass = "";
- db = "";
- ip = "182.255.0.151";
- login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- String usernam = username.getText().toString();
- String passwordd = password.getText().toString();
- CheckLogin checkLogin = new CheckLogin();// this is the Asynctask, which is used to process in background to reduce load on app process
- checkLogin.execute(usernam, passwordd);
- }
- });
- }
- private class MyTextWatcher implements TextWatcher {
- private View view;
- private MyTextWatcher(View view) {
- this.view = view;
- }
- public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
- }
- public void afterTextChanged(Editable editable) {
- switch (view.getId()) {
- case R.id.editText:
- validateUsername();
- break;
- case R.id.editText2:
- validatePassword();
- break;
- }
- }
- }
- private boolean validateUsername() {
- if (username.getText().toString().trim().isEmpty()) {
- inputLayoutEmail.setError("Enter valid username !");
- requestFocus(username);
- return false;
- } else {
- inputLayoutEmail.setErrorEnabled(false);
- }
- return true;
- }
- private boolean validatePassword() {
- if (password.getText().toString().trim().isEmpty()) {
- inputLayoutPassword.setError("Enter valid password !");
- requestFocus(password);
- return false;
- } else {
- inputLayoutPassword.setErrorEnabled(false);
- }
- return true;
- }
- public class CheckLogin extends AsyncTask<String, String, String> {
- String z = "";
- Boolean isSuccess = false;
- @Override
- protected void onPreExecute() {
- progressBar.setVisibility(View.VISIBLE);
- }
- @Override
- protected void onPostExecute(String r) {
- progressBar.setVisibility(View.GONE);
- Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
- if (isSuccess) {
- Toast.makeText(MainActivity.this, "Login Successfull", Toast.LENGTH_LONG).show();
- //finish();
- }
- }
- @Override
- protected String doInBackground(String... args) {
- String usernam = args[0];
- String passwordd = args[1];
- if (usernam.trim().equals("")){
- validateUsername();
- }
- else if(passwordd.trim().equals("")){
- validatePassword();
- }
- /*(usernam.trim().equals("") || passwordd.trim().equals("")) {
- z = "Please enter Username and Password";*/
- else {
- try {
- con = connectionclass(un, pass, db, ip);// Connect to database
- if (con == null) {
- z = "Check Your Internet Access!";
- } else {
- String query = "select * from tbllogin where username= '" + usernam.toString() + "' and password = '" + passwordd.toString() + "' ";
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query);
- if (rs.next()) {
- z = "Login successful";
- isSuccess = true;
- con.close();
- } else {
- z = "Invalid Credentials!";
- isSuccess = false;
- }
- }
- } catch (Exception ex) {
- isSuccess = false;
- z = ex.getMessage();
- }
- }
- return z;
- }
- }
- private void requestFocus(View view) {
- if (view.requestFocus()) {
- getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
- }
- }
- @SuppressLint("NewApi")
- public Connection connectionclass(String user, String password, String database, String server) {
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- Connection connection = null;
- String ConnectionURL = null;
- try {
- Class.forName("net.sourceforge.jtds.jdbc.Driver");
- ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";" + "databaseName=" + database + ";user=" + user + ";password=" + password + ";";
- connection = DriverManager.getConnection(ConnectionURL);
- } catch (SQLException se) {
- Log.e("error here 1 : ", se.getMessage());
- } catch (ClassNotFoundException e) {
- Log.e("error here 2 : ", e.getMessage());
- } catch (Exception e) {
- Log.e("error here 3 : ", e.getMessage());
- }
- return connection;
- }
- }
Add Comment
Please, Sign In to add comment