Android应用程序| MySQL Logging无法正常工作

时间:2016-09-04 07:01:29

标签: java android mysql

我正在使用MySQL制作一个Android登录应用程序。我创建了一个DatabaseHandler类,它有两个扩展AsyncTasc的私有类。

代码:

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 "";
        }
    }
}

为了找出它无法正常工作的原因,我添加了以Log.e开头的行。 它每次写Log.e时都会记录,但它不起作用。

问题

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

URL应该是错误的:“jdbc:mysql:// server:port / database”