Guest User

Untitled

a guest
Aug 27th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3.  
  4.  
  5. ConnectionClass connectionClass;
  6. EditText edtuserid,edtpass;
  7. Button btnlogin;
  8. ProgressBar pbbar;
  9.  
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15.  
  16. connectionClass = new ConnectionClass();
  17. edtuserid = (EditText) findViewById(R.id.edtuserid);
  18. edtpass = (EditText) findViewById(R.id.edtpass);
  19. btnlogin = (Button) findViewById(R.id.btnlogin);
  20. pbbar = (ProgressBar) findViewById(R.id.pbbar);
  21. pbbar.setVisibility(View.GONE);
  22.  
  23. btnlogin.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. DoLogin doLogin = new DoLogin();
  27. doLogin.execute("");
  28.  
  29. }
  30. });
  31.  
  32. }
  33.  
  34. public class DoLogin extends AsyncTask<String,String,String>
  35. {
  36. String z = "";
  37. Boolean isSuccess = false;
  38.  
  39.  
  40. String userid = edtuserid.getText().toString();
  41. String password = edtpass.getText().toString();
  42.  
  43.  
  44. @Override
  45. protected void onPreExecute() {
  46. pbbar.setVisibility(View.VISIBLE);
  47. }
  48.  
  49. @Override
  50. protected void onPostExecute(String r) {
  51. pbbar.setVisibility(View.GONE);
  52. Toast.makeText(MainActivity.this,r,Toast.LENGTH_SHORT).show();
  53.  
  54. if(isSuccess) {
  55. Intent i = new Intent(MainActivity.this, attendance.class);
  56. startActivity(i);
  57. finish();
  58. }
  59.  
  60. }
  61.  
  62. @Override
  63. protected String doInBackground(String... params) {
  64. if(userid.trim().equals("")|| password.trim().equals(""))
  65. z = "Please enter User Id and Password";
  66. else
  67. {
  68. try {
  69. Connection con = connectionClass.CONN();
  70. if (con == null) {
  71. z = "Error in connection with The server";
  72. } else {
  73. String query = "select * from tablename where id='" + userid + "' and Password='" + password + "'";
  74. Statement stmt = con.createStatement();
  75. ResultSet rs = stmt.executeQuery(query);
  76.  
  77. if(rs.next())
  78. {
  79.  
  80. z = "Login successfull ";
  81. isSuccess=true;
  82. }
  83. else
  84. {
  85. z = "Invalid Credentials";
  86. isSuccess = false;
  87. }
  88.  
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. isSuccess = false;
  94. z = "Exceptions";
  95. }
  96. }
  97. return z;
  98. }
  99. }
  100.  
  101. public class ConnectionClass {
  102. String ip = "192.168.....etc";
  103. String classs = "net.sourceforge.jtds.jdbc.Driver";
  104. String db = "dbname";
  105. String un = "sql server login username";
  106. String password = "*****";
  107.  
  108. @SuppressLint("NewApi")
  109. public Connection CONN() {
  110. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
  111. .permitAll().build();
  112. StrictMode.setThreadPolicy(policy);
  113. Connection conn = null;
  114. String ConnURL = null;
  115. try {
  116.  
  117. Class.forName(classs);
  118. ConnURL = "jdbc:jtds:sqlserver://" + ip + ";"
  119. + "databaseName=" + db + ";user=" + un + ";password="
  120. + password + ";";
  121. conn = DriverManager.getConnection(ConnURL);
  122. } catch (SQLException se) {
  123. Log.e("ERRO", se.getMessage());
  124. } catch (ClassNotFoundException e) {
  125. Log.e("ERRO", e.getMessage());
  126. } catch (Exception e) {
  127. Log.e("ERRO", e.getMessage());
  128. }
  129. return conn;
  130. }
  131.  
  132. <uses-permission android:name="android.permission.INTERNET" />
  133. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  134. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  135. <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
  136. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
Add Comment
Please, Sign In to add comment