Guest User

Untitled

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