Advertisement
Guest User

Untitled

a guest
Jul 4th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. public class DBConnect extends AsyncTask<String, Void, Connection>
  2. {
  3. private Context ctx;
  4. SharedPreferences prefs;
  5. Connection conn;
  6. String ipValue;
  7. public DBConnect(Context ctx)
  8. {
  9. this.ctx=ctx;
  10. }
  11. @Override
  12. protected void onPreExecute() {
  13. super.onPreExecute();
  14. //get sharedPreferences here
  15. prefs = ctx.getSharedPreferences("DoorSystem_SHPREF",
  16. Context.MODE_PRIVATE);
  17. ipValue=prefs.getString("IP_VALUE",null);
  18. }
  19. @Override
  20. protected Connection doInBackground(String... params) {
  21.  
  22.  
  23. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
  24. .permitAll().build();
  25. StrictMode.setThreadPolicy(policy);
  26. String ConnURL = null;
  27. try {
  28.  
  29. Class.forName("net.sourceforge.jtds.jdbc.Driver");
  30. ConnURL = "jdbc:jtds:sqlserver://" + ipValue + ":1433/doors;user=sam123;password=111";
  31. conn = DriverManager.getConnection(ConnURL);
  32. } catch (Exception se) {
  33. se.printStackTrace();
  34. }
  35.  
  36. return conn;
  37. }
  38. }
  39.  
  40. public void logIn(View view)
  41. {
  42. Toast.makeText(getApplicationContext(),ipValue,Toast.LENGTH_LONG).show();
  43. try {
  44. DBConnect dbConnect = new DBConnect(getApplicationContext());
  45. Connection conn=dbConnect.execute().get();
  46. LogInStatement="select count(*) from users where username=? AND password=?";
  47. preparedStatement=conn.prepareStatement(LogInStatement);
  48. preparedStatement.setString(1,userNameEditText.getText().toString());
  49. preparedStatement.setString(2,passwordEditText.getText().toString());
  50. ResultSet rs = preparedStatement.executeQuery();
  51. while (rs.next()) {
  52. count = rs.getInt(1);
  53. }
  54. if (count != 0) {
  55. Intent userScreenIntent = new Intent(this, UserScreen.class);
  56. startActivity(userScreenIntent);
  57. } else {
  58. Toast.makeText(getApplicationContext(), "بيانات الدخول غير صحيحة", Toast.LENGTH_LONG).show();
  59. }
  60. }
  61. catch(Exception e)
  62. {
  63. e.printStackTrace();
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement