【问题标题】:Code in Eclipse works, but doesn't work on AndroidEclipse 中的代码有效,但在 Android 上无效
【发布时间】:2016-03-06 19:42:09
【问题描述】:

我有这个问题大约 2 天,我找不到任何解决方案。事情是我正在使用 MySQL-Connection-Java 来操作基于 Android 的 MySQL。尽管 Eclipse Java 中的代码可以正常工作,但在调试时它无法与此基础连接。我浏览了互联网上的许多论坛讨论,但似乎没有有效的解决方案,或者我可能是盲目的。您可以在下面看到我正在使用的类代码。我不知道为什么在 Eclipse 中与基础连接,但在 Android 设备上却不想。

错误代码:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法创建与数据库服务器的连接。尝试重新连接 3 次。放弃。

    import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.Properties;

    public class JDBC {
        // init database constants
        private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
        private stat

ic final String DATABASE_URL = "jdbc:mysql://sql7.freemysqlhosting.net/sql7109625?autoReconnect=true";
        private static final String USERNAME = "REMOVED";
        private static final String PASSWORD = "REMOVED";
        private static final String MAX_POOL = "250";

        // init connection object
        private Connection connection;
        // init properties object
        private Properties properties;

        // create properties
        private Properties getProperties() {
            if (properties == null) {
                properties = new Properties();
                properties.setProperty("user", USERNAME);
                properties.setProperty("password", PASSWORD);
                properties.setProperty("MaxPooledStatements", MAX_POOL);
            }
            return properties;
        }

        // connect database
        public Connection connect() {
            if (connection == null) {
                try {
                    Class.forName(DATABASE_DRIVER);
                    connection = DriverManager.getConnection(DATABASE_URL, getProperties());
                } catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
                }
            }
            return connection;
        }

        // disconnect database
        public void disconnect() {
            if (connection != null) {
                try {
                    connection.close();
                    connection = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } }

【问题讨论】:

  • 如果我是你,我会很快更改我的 MySQL 密码,因为你刚刚在互联网上公开发布了它。
  • 为什么你认为你可以使用代码连接到你机器上的常规 MySQL 数据库来连接到 Android 上的 SQLite 数据库而不做任何更改?
  • @Niels Masdorp 当我连接常规 MySql 时,我使用以下代码获取数据:linkString sql = "SELECT * FROM stackoverflow"; try { PreparedStatement statement = mysqlConnect.connect().prepareStatement(sql); ... go on ... ... go on ... ... DONE .... } catch (SQLException e) { e.printStackTrace(); } finally { mysqlConnect.disconnect(); }

标签: java android mysql eclipse


【解决方案1】:

我发现了我的问题。我用

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 

和我的应用程序工作:)

【讨论】:

    猜你喜欢
    • 2013-10-14
    • 2018-11-14
    • 1970-01-01
    • 2018-05-15
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多