Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. package me.martinwiesner.cpad;
  2.  
  3. import android.os.AsyncTask;
  4. import android.support.annotation.Nullable;
  5. import android.util.Log;
  6.  
  7. import java.sql.*;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.concurrent.ExecutionException;
  11.  
  12. public class UserDatabaseHandler {
  13. private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  14. private static final String DB_URL = "sql7.freemysqlhosting.net";
  15.  
  16. private static final String USER = "**********";
  17. private static final String PASS = "**********";
  18.  
  19. private static Get get;
  20. private static Set set;
  21.  
  22. public static void onCreate() {
  23. get = new Get();
  24. set = new Set();
  25. }
  26.  
  27. static List<User> getAllUsers() {
  28. return get.doInBackground(new String[]{""});
  29. }
  30.  
  31. @Nullable
  32. static User getUser(String email) {
  33. for (User user : getAllUsers()) {
  34. if (user.getEmail().equals(email))
  35. return user;
  36. }
  37. return null;
  38. }
  39.  
  40. @Nullable
  41. static User getUser(int id) {
  42. for (User user : getAllUsers()) {
  43. if (user.getId() == id)
  44. return user;
  45. }
  46. return null;
  47. }
  48.  
  49. static void setAllUsers(List<User> users) {
  50. set.doInBackground(users);
  51. }
  52.  
  53. static void addUser(User user) {
  54. List<User> users = getAllUsers();
  55. users.add(user);
  56. setAllUsers(users);
  57. }
  58.  
  59. static User addUser(String name, String email, String password, boolean isAdmin) {
  60. Log.e("UDH", "Adding User");
  61. int id = 0;
  62. for (User user : getAllUsers()) {
  63. if (user.getId() != id) {
  64. break;
  65. } else {
  66. id++;
  67. }
  68. }
  69. User user = new User(id, name, email, password, isAdmin);
  70. addUser(user);
  71. return user;
  72. }
  73.  
  74. private static class Get extends AsyncTask<String, Void, List<User>> {
  75.  
  76. @Override
  77. protected List<User> doInBackground(String... strings) {
  78. Log.e("UDH", "Getting in Background");
  79. Connection connection = null;
  80. Statement statement = null;
  81. List<User> allUsers = new ArrayList<>();
  82. try {
  83. Log.e("UDH", "Getting in Background - Trying");
  84. Class.forName(JDBC_DRIVER);
  85.  
  86. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  87.  
  88. statement = connection.createStatement();
  89. String sql = "SELECT * FROM users";
  90. ResultSet rs = statement.executeQuery(sql);
  91.  
  92. while (rs.next()) {
  93. int id = rs.getInt("ID");
  94. String name = rs.getString("NAME");
  95. String email = rs.getString("EMAIL");
  96. String password = rs.getString("PASSWORD");
  97. boolean isAdmin = rs.getBoolean("ISADMIN");
  98.  
  99. Log.e("Getting", id + "");
  100. Log.e("Getting", name);
  101. Log.e("Getting", email);
  102. Log.e("Getting", password);
  103. Log.e("Getting", isAdmin + "");
  104.  
  105. User user = new User(id, name, email, password, isAdmin);
  106. allUsers.add(user);
  107. }
  108. rs.close();
  109. statement.close();
  110. connection.close();
  111. } catch (SQLException se) {
  112. se.printStackTrace();
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. } finally {
  116. try {
  117. if (statement != null)
  118. statement.close();
  119. } catch (SQLException se2) {
  120. }
  121. try {
  122. if (connection != null)
  123. connection.close();
  124. } catch (SQLException se) {
  125. se.printStackTrace();
  126. }
  127. }
  128. return allUsers;
  129. }
  130. }
  131.  
  132. private static class Set extends AsyncTask<List<User>, Void, String> {
  133.  
  134. @Override
  135. protected String doInBackground(List<User>... users) {
  136. Log.e("UDH", "Setting in Background");
  137. Connection connection = null;
  138. Statement statement = null;
  139.  
  140. for (User user : users[0]) {
  141. try {
  142. connection = DriverManager.getConnection(DB_URL, USER, PASS);
  143. statement = connection.createStatement();
  144.  
  145. String query = "INSERT INTO `users`(`ID`, `NAME`, `EMAIL`, `PASSWORD`, `ISADMIN`) VALUES ("
  146. + user.getId() + "," + user.getName() + "," + user.getEmail() + "," + user.getPassword()
  147. + "," + user.isAdmin() + ")";
  148.  
  149. ResultSet rs = statement.executeQuery(query);
  150.  
  151. rs.close();
  152. connection.close();
  153. statement.close();
  154. } catch (SQLException e) {
  155. e.printStackTrace();
  156. } finally {
  157. try {
  158. if (statement != null)
  159. statement.close();
  160. } catch (SQLException se2) {
  161. }
  162. try {
  163. if (connection != null)
  164. connection.close();
  165. } catch (SQLException se) {
  166. se.printStackTrace();
  167. }
  168. }
  169. }
  170. return "";
  171. }
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement