简体   繁体   English

Android MySql:JDBC CommunicationsException

[英]Android MySql : JDBC CommunicationsException

I am using Eclipse and Android Emulator. 我正在使用Eclipse和Android Emulator。 I can access to 10.0.2.2 in emulator browser. 我可以在模拟器浏览器中访问10.0.2.2。 But in my app, this code not run : 但在我的应用程序中,此代码无法运行:

Connection con = DriverManager.getConnection("jdbc:mysql://10.0.2.2/stu", "user", "pass");

Anybody has some idea? 有人有点想法吗?

The error is : 错误是:

com.mysql.jdbc.CommunicationsException

I am confused that why i have access to phpMyAdmin in emulator's browser, but not inside the code! 我很困惑,为什么我可以在模拟器的浏览器中访问phpMyAdmin,但不在代码内! :( :(

Note that full code is this : 请注意 ,完整代码是这样的:

public class LearningActivity extends Activity {

private static final String url = "jdbc:mysql://10.0.2.2/stu";
private static final String user = "root";
private static final String pass = "11111";
private Button button;
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button = (Button)this.findViewById(R.id.button1);
    tv = (TextView)this.findViewById(R.id.textView1);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            Connect();
        }
    });
}


private class Connect extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        String response = "";

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(url, user, pass);
            // System.out.println("Database connection success");

            String result = "Database connection success\n";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from users");
            ResultSetMetaData rsmd = rs.getMetaData();

            while(rs.next()) {
                result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
                result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
                result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
            }
            tv.setText(result);
        }
        catch(Exception e) {
            e.printStackTrace();
            tv.setText(e.toString());
        }
        return response;   


    }

    @Override
    protected void onPostExecute(String result) {
        tv.setText(result);
    }
}

public void Connect() {
    Connect task = new Connect();
    task.execute();

}

} }

your mysql database should have access from remote system. 你的mysql数据库应该可以从远程系统访问。

You should grant permission for remote access something like this 你应该授予这样的远程访问权限

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.%'
    IDENTIFIED BY PASSWORD 'password'  
    WITH GRANT OPTION;
FLUSH PRIVILEGES;

check for more info 查看更多信息

Damn it! 该死的! I missed to add Internet permission to manifest! 我错过了添加Internet权限清单! :( I add that and problem solved. :(我补充说,问题解决了。

The browser of phone(android emulator) has this permission and it could connect to network, but my application haven't that permission. 手机浏览器(android模拟器)拥有此权限,可以连接到网络,但我的应用程序没有该权限。 So I should add only this line to manifest.xml: 所以我应该只将这一行添加到manifest.xml:

<manifest ... >
.
.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
.
.
</manifest>

你没有改变ip ...... 1>得到你的机器ip 2>然后连接con = DriverManager.getConnection(“jdbc:mysql://”your_ip“/ stu”,“user”,“pass”);

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

相关问题 JDBC CommunicationsException与MySQL数据库 - JDBC CommunicationsException with MySQL Database com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException MySQL + Apache Tomcat 7 - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException MySQL + Apache Tomcat 7 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败? - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure? 引起原因:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException - Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException com.mysql.cj.jdbc.exceptions.CommunicationsException:通信链接失败 - com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure com.mysql.jdbc.exceptions.jdbc4.CommunicationsException使用JDBC访问远程MySQL数据库时 - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException when using JDBC to access remote MySQL database jdbc4 CommunicationsException - jdbc4 CommunicationsException jdbc单例连接的CommunicationsException - CommunicationsException for jdbc singleton connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM