Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.martinwiesner.cpad;
- import android.os.AsyncTask;
- import android.support.annotation.Nullable;
- import android.util.Log;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ExecutionException;
- public class UserDatabaseHandler {
- private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- private static final String DB_URL = "sql7.freemysqlhosting.net";
- private static final String USER = "**********";
- private static final String PASS = "**********";
- private static Get get;
- private static Set set;
- public static void onCreate() {
- get = new Get();
- set = new Set();
- }
- static List<User> getAllUsers() {
- return get.doInBackground(new String[]{""});
- }
- @Nullable
- static User getUser(String email) {
- for (User user : getAllUsers()) {
- if (user.getEmail().equals(email))
- return user;
- }
- return null;
- }
- @Nullable
- static User getUser(int id) {
- for (User user : getAllUsers()) {
- if (user.getId() == id)
- return user;
- }
- return null;
- }
- static void setAllUsers(List<User> users) {
- set.doInBackground(users);
- }
- static void addUser(User user) {
- List<User> users = getAllUsers();
- users.add(user);
- setAllUsers(users);
- }
- static User addUser(String name, String email, String password, boolean isAdmin) {
- Log.e("UDH", "Adding User");
- int id = 0;
- for (User user : getAllUsers()) {
- if (user.getId() != id) {
- break;
- } else {
- id++;
- }
- }
- User user = new User(id, name, email, password, isAdmin);
- addUser(user);
- return user;
- }
- private static class Get extends AsyncTask<String, Void, List<User>> {
- @Override
- protected List<User> doInBackground(String... strings) {
- Log.e("UDH", "Getting in Background");
- Connection connection = null;
- Statement statement = null;
- List<User> allUsers = new ArrayList<>();
- try {
- Log.e("UDH", "Getting in Background - Trying");
- Class.forName(JDBC_DRIVER);
- connection = DriverManager.getConnection(DB_URL, USER, PASS);
- statement = connection.createStatement();
- String sql = "SELECT * FROM users";
- ResultSet rs = statement.executeQuery(sql);
- while (rs.next()) {
- int id = rs.getInt("ID");
- String name = rs.getString("NAME");
- String email = rs.getString("EMAIL");
- String password = rs.getString("PASSWORD");
- boolean isAdmin = rs.getBoolean("ISADMIN");
- Log.e("Getting", id + "");
- Log.e("Getting", name);
- Log.e("Getting", email);
- Log.e("Getting", password);
- Log.e("Getting", isAdmin + "");
- User user = new User(id, name, email, password, isAdmin);
- allUsers.add(user);
- }
- rs.close();
- statement.close();
- connection.close();
- } catch (SQLException se) {
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (statement != null)
- statement.close();
- } catch (SQLException se2) {
- }
- try {
- if (connection != null)
- connection.close();
- } catch (SQLException se) {
- se.printStackTrace();
- }
- }
- return allUsers;
- }
- }
- private static class Set extends AsyncTask<List<User>, Void, String> {
- @Override
- protected String doInBackground(List<User>... users) {
- Log.e("UDH", "Setting in Background");
- Connection connection = null;
- Statement statement = null;
- for (User user : users[0]) {
- try {
- connection = DriverManager.getConnection(DB_URL, USER, PASS);
- statement = connection.createStatement();
- String query = "INSERT INTO `users`(`ID`, `NAME`, `EMAIL`, `PASSWORD`, `ISADMIN`) VALUES ("
- + user.getId() + "," + user.getName() + "," + user.getEmail() + "," + user.getPassword()
- + "," + user.isAdmin() + ")";
- ResultSet rs = statement.executeQuery(query);
- rs.close();
- connection.close();
- statement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (statement != null)
- statement.close();
- } catch (SQLException se2) {
- }
- try {
- if (connection != null)
- connection.close();
- } catch (SQLException se) {
- se.printStackTrace();
- }
- }
- }
- return "";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement