salmanabila

mainactivity.java

Jun 6th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. package info.androidhive.introslider;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.support.design.widget.TextInputLayout;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.support.v7.widget.Toolbar;
  9. import android.text.Editable;
  10. import android.text.TextWatcher;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.view.WindowManager;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17. import android.os.StrictMode;
  18. import android.os.AsyncTask;
  19. import android.widget.ProgressBar;
  20. import java.sql.Connection;
  21. import java.sql.DriverManager;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25.  
  26. public class MainActivity extends AppCompatActivity {
  27.  
  28. Toolbar toolbar;
  29. EditText username, password;
  30. TextInputLayout inputLayoutEmail, inputLayoutPassword;
  31. Button login;
  32. ProgressBar progressBar;
  33. Connection con;
  34. String un, pass, db, ip;
  35.  
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.activity_main);
  40. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  41. setSupportActionBar(toolbar);
  42.  
  43. inputLayoutEmail = (TextInputLayout) findViewById(R.id.input_layout_email);
  44. inputLayoutPassword = (TextInputLayout) findViewById(R.id.input_layout_password);
  45. login = (Button) findViewById(R.id.button);
  46. username = (EditText) findViewById(R.id.editText);
  47. password = (EditText) findViewById(R.id.editText2);
  48. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  49.  
  50. username.addTextChangedListener(new MyTextWatcher(username));
  51. password.addTextChangedListener(new MyTextWatcher(password));
  52.  
  53. un = "";
  54. pass = "";
  55. db = "";
  56. ip = "182.255.0.151";
  57.  
  58.  
  59. login.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View arg0) {
  62. String usernam = username.getText().toString();
  63. String passwordd = password.getText().toString();
  64. CheckLogin checkLogin = new CheckLogin();// this is the Asynctask, which is used to process in background to reduce load on app process
  65. checkLogin.execute(usernam, passwordd);
  66. }
  67. });
  68.  
  69. }
  70.  
  71. private class MyTextWatcher implements TextWatcher {
  72.  
  73. private View view;
  74.  
  75. private MyTextWatcher(View view) {
  76. this.view = view;
  77. }
  78.  
  79. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  80. }
  81.  
  82. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  83. }
  84.  
  85. public void afterTextChanged(Editable editable) {
  86. switch (view.getId()) {
  87. case R.id.editText:
  88. validateUsername();
  89. break;
  90. case R.id.editText2:
  91. validatePassword();
  92. break;
  93. }
  94. }
  95. }
  96.  
  97. private boolean validateUsername() {
  98. if (username.getText().toString().trim().isEmpty()) {
  99. inputLayoutEmail.setError("Enter valid username !");
  100. requestFocus(username);
  101. return false;
  102. } else {
  103. inputLayoutEmail.setErrorEnabled(false);
  104. }
  105. return true;
  106. }
  107.  
  108. private boolean validatePassword() {
  109. if (password.getText().toString().trim().isEmpty()) {
  110. inputLayoutPassword.setError("Enter valid password !");
  111. requestFocus(password);
  112. return false;
  113. } else {
  114. inputLayoutPassword.setErrorEnabled(false);
  115. }
  116.  
  117. return true;
  118. }
  119.  
  120. public class CheckLogin extends AsyncTask<String, String, String> {
  121. String z = "";
  122. Boolean isSuccess = false;
  123.  
  124. @Override
  125. protected void onPreExecute() {
  126. progressBar.setVisibility(View.VISIBLE);
  127. }
  128.  
  129. @Override
  130. protected void onPostExecute(String r) {
  131. progressBar.setVisibility(View.GONE);
  132. Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
  133. if (isSuccess) {
  134. Toast.makeText(MainActivity.this, "Login Successfull", Toast.LENGTH_LONG).show();
  135. //finish();
  136. }
  137. }
  138.  
  139.  
  140. @Override
  141. protected String doInBackground(String... args) {
  142.  
  143.  
  144.  
  145. String usernam = args[0];
  146. String passwordd = args[1];
  147. if (usernam.trim().equals("")){
  148. validateUsername();
  149. }
  150. else if(passwordd.trim().equals("")){
  151. validatePassword();
  152. }
  153. /*(usernam.trim().equals("") || passwordd.trim().equals("")) {
  154. z = "Please enter Username and Password";*/
  155. else {
  156. try {
  157. con = connectionclass(un, pass, db, ip);// Connect to database
  158. if (con == null) {
  159. z = "Check Your Internet Access!";
  160. } else {
  161. String query = "select * from tbllogin where username= '" + usernam.toString() + "' and password = '" + passwordd.toString() + "' ";
  162. Statement stmt = con.createStatement();
  163. ResultSet rs = stmt.executeQuery(query);
  164. if (rs.next()) {
  165. z = "Login successful";
  166. isSuccess = true;
  167. con.close();
  168. } else {
  169. z = "Invalid Credentials!";
  170. isSuccess = false;
  171. }
  172. }
  173. } catch (Exception ex) {
  174. isSuccess = false;
  175. z = ex.getMessage();
  176. }
  177. }
  178. return z;
  179. }
  180. }
  181.  
  182. private void requestFocus(View view) {
  183. if (view.requestFocus()) {
  184. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
  185. }
  186. }
  187.  
  188.  
  189.  
  190. @SuppressLint("NewApi")
  191. public Connection connectionclass(String user, String password, String database, String server) {
  192. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  193. StrictMode.setThreadPolicy(policy);
  194. Connection connection = null;
  195. String ConnectionURL = null;
  196. try {
  197. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  198. ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";" + "databaseName=" + database + ";user=" + user + ";password=" + password + ";";
  199. connection = DriverManager.getConnection(ConnectionURL);
  200. } catch (SQLException se) {
  201. Log.e("error here 1 : ", se.getMessage());
  202. } catch (ClassNotFoundException e) {
  203. Log.e("error here 2 : ", e.getMessage());
  204. } catch (Exception e) {
  205. Log.e("error here 3 : ", e.getMessage());
  206. }
  207. return connection;
  208. }
  209.  
  210. }
Add Comment
Please, Sign In to add comment