简体   繁体   English

在Android中使用JDBC MySQL连接数据库?

[英]Using JDBC MySQL to connect database in Android?

I create an Android app to connect database using MySQL.我创建了一个 Android 应用程序来使用 MySQL 连接数据库。 When I test connection with localhost 10.0.2.2:3306, I can get database, but when I use the real host to get database from my server, I can not get anything.当我测试与localhost 10.0.2.2:3306的连接时,我可以获取数据库,但是当我使用真实主机从我的服务器获取数据库时,我什么也得不到。 My host is: https://192.168.1.xxx:xxxxx and my database name is test .我的主机是: https://192.168.1.xxx:xxxxx ,我的数据库名称是test What is my problem?我的问题是什么? Please help me.请帮我。 Thank you.谢谢你。 This is my code:这是我的代码:

private class MyTask extends AsyncTask<Void, Void, Void> {

    private String idFromServer;
    private String url="jdbc:mysql://192.168.1.xxx:xxxxx/test";
    String name = "AAA";

    @Override
    protected Void doInBackground(Void... params) {
        try {
            ResultSet result = null;
            String a = "SELECT * FROM testtable";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con = DriverManager.getConnection(url,"aaa","abcdef");
            Statement state = con.createStatement();
            result = state.executeQuery(a);
            while (result.next()) {
                if (name.equals(result.getString("name"))) {
                    idFromServer = result.getString("id");
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(Void result) {
        id.setText(idFromServer);
        super.onPostExecute(result);
    }

}

Generally MySql is installed with security restrictions so it is possible to access to it only from localhost.通常 MySql 安装有安全限制,因此只能从本地主机访问它。

You need to execute a command similar to the following to permit access from other ip需要执行类似下面的命令,允许其他ip访问

GRANT ALL PRIVILEGES
ON database.*
TO 'youruser'@'%'
IDENTIFIED BY 'yourpassword';

Note that it is a bad practice to offer access to MySql from any IP because it opens the possibility to be hacked.请注意,从任何 IP 提供对 MySql 的访问是一种不好的做法,因为它可能会被黑客入侵。 So generally it is a good idea to place a server that intercept requests from your android application and directly call the database.所以一般来说,最好放置一个服务器来拦截来自你的 android 应用程序的请求并直接调用数据库。


Note: check also if it is open the route between your application and the mysql server.注意:还要检查它是否打开了您的应用程序和 mysql 服务器之间的路由。

If you use 3G you are not using a local network and the IP of the MySql server is different from 192.168.1.xxx (that is the ip of a local network).如果你使用3G,你就不是在使用本地网络,并且MySql服务器的IP与192.168.1.xxx不同(即本地网络的IP)。 It is also possible that outside of the local network the MySql Server is not visible.也有可能在本地网络之外 MySql Server 不可见。 It depends from the configuration of your network.这取决于您的网络配置。

You need to "open" your network exposing the port to access mysql and checking which is the ip of your local network as seen from internet.您需要“打开”您的网络,公开端口以访问 mysql 并检查从 Internet 上看到的本地网络的 ip。 Otherwise you can access to the local network using a phone with wireless connection to your local network.否则,您可以使用无线连接到本地网络的电话访问本地网络。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM