如何让MySQL Connector / J在android上运行? [英] How to make MySQL Connector/J working on android?

查看:148
本文介绍了如何让MySQL Connector / J在android上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地Linux机器上运行了MySQL服务器。我没有遇到任何与它通信和使用MySQL Connector / J通过一个简单的Java程序(在eclipse中运行)执行SQL语句的问题。
这是我的简单Java程序:

I've got a MySQL server running on a local Linux machine. I haven't been faced to any problems communicating with it and executing SQL statements through a simple Java program (running inside of eclipse) using the MySQL Connector/J. Here is my simple Java program:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.lang.*;


public class main{
    private static void request(){
        String url = "jdbc:mysql://myadress:3306/mydatabase";
        String user = "user";
        String passwd = "passwd";

        Connection conn = null;
        try{
            /* Initializing the connection */
            conn = DriverManager.getConnection(url, user, passwd);

            Statement statement = conn.createStatement();

            ResultSet resultset = statement.executeQuery(/* MY SQL reqquest */);

            while(resultset.next()){
                System.out.println(resultset.getString(/* THE COLUMN AND ROW I WANTED IN MY REQUEST */));       
            }

        }catch(SQLException e){
            System.out.println("SQL connection error: " + e.getMessage());
        }finally {
            if(conn != null){
                try{
                    /* CLosing connection */
                    conn.close();
                }catch (SQLException e){
                    System.out.println("Error while closing the connection: " + e.getMessage());
                }
            }
        }
    }
    public static void main(String[] args) {
        try{ // Loading the MySQL Connector/J driver
            Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException e){
            System.out.println("Error while loading the Driver: " + e.getMessage());
        }
        request();
    }

}

此代码在eclipse中完美运行并且可以执行与UPDATE SQL请求一样多的SELECT而没有任何问题。
当我尝试将此请求方法调整到我的MainActivity.java类时,我的android应用程序确实正确加载驱动程序但无法与我的MySQL服务器通信并返回错误。

This code works perfectly inside of eclipse and can perform as much SELECT as UPDATE SQL requests without any problem. When I tried to adapt this request method to my MainActivity.java class, my android application does load the driver correctly but can't communicate with my MySQL server and returns me an error.

如果我在前面的代码中输入我的服务器的本地IP地址并将我的Android手机连接到Wi-Fi,那么我的应用程序会返回给我:

If I enter a local IP adress to my server in the previous code and connect my android phone to the Wi-Fi, then my app returns me:


通信链接失败。成功发送到
服务器的最后一个数据包是0毫秒前。驱动程序没有从服务器收到任何数据包

"Communication link failure. The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server."

但如果我尝试使用相同的先前代码输入外部IP地址到我的服务器(不要担心MySQL端口是开放的互联网和我的服务器正在监听任何IP),通过LTE互联网连接使用我的Android手机,我得到一个不同的错误:

But if I try to enter an external IP adress to my server in the same previous code (don't worry the MySQL port is open to the internet and my server is listening to any IP), use my android phone over an LTE internet connection, I get a different error:


无法创建与数据库服务器的连接

"Could not create connection to database server"

到目前为止,没有m研究帮助了我。有谁知道我的问题可能来自哪里?
提前致谢:)

Up until now, none of my research has helped me out. Does anyone has any idea of where my problem could come from ? Thanks in advance :)

推荐答案

移植到Android世界的Java开发人员之间的一般误解是,已经为旧的Java创建,也可以在Android上使用。由于Android的Java使用违反了主要的Java原则,一次编写,到处运行,这根本不是真的。我见过有人试图在Android上使用常见的Apache jar,导致出现奇怪的错误(参见无法在Eclipse中导入Apache HTTP

The general misconception among Java developers who is moving to Android world is that everything that has been created for old good Java can be used on Android as well. Since Android usage of Java has violated the major Java principle, "write once, run everywhere", it's simply not true. I've seen people trying to use common Apache jars with Android, which resulted to weird errors (see Can't import Apache HTTP in Eclipse).

对于Java JDBC驱动程序和API,似乎也存在同样的问题,例如请在此处查看答案:使用JDBC从Android连接到MySQL

The same problems seem to be true for Java JDBC drivers and API's, e.g. see answers here: Connecting to MySQL from Android with JDBC

常见建议 - 每次尝试在Android上使用第三方JAR时,请检查它是否与后者兼容,或者是否有特定于Android的端口。

The common advice - each time when you try to use a 3rd party JAR with Android, check if it's compatible with the latter or if there is a port, which is specific for Android.

这篇关于如何让MySQL Connector / J在android上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆