简体   繁体   English

将Android App与存储在本地计算机上的My SQL数据库连接

[英]Connect Android App with My SQL database stored on the local machine

What are the ways in which I could accomplish the above? 我可以通过哪些方式完成上述任务? The current one I employ uses a JDBC Connector with the app. 我目前使用的应用程序与该应用程序一起使用JDBC连接器。
My question is? 我的问题是?

In the following steps: 在以下步骤中:

static final String DB_URL = "jdbc:mysql://localhost:3306/mess";
static final String USER = "root";
static final String PASS = "root";

public Connection connection_open(){
    Connection conn=null;
    try{
         Class.forName("com.mysql.jdbc.Driver").newInstance();

         conn = DriverManager.getConnection(DB_URL,USER,PASS);
         System.out.println("\n Connection inside DB = OK");
      }

   catch(SQLException se){
       System.out.println("\n Connection inside DB = failed");
       se.printStackTrace();
      }

    finally {
        if(conn==null)System.out.println("\n Have to return NULL");
             return conn;
        }
}

This always outputs: 这总是输出:

Connection inside DB = failed
Have to return NULL

So, in the localhost section of the DB URL, do I have to write the IP of my android phone, which I use to debug? 因此,在数据库URL的localhost部分中,我是否必须编写用于调试的android手机的IP?
Is it even possible, that my app connects remotely (assuming I have granted privileges to all IP's) 我的应用程序是否有可能远程连接(假设我已授予所有IP特权)

Please help :) 请帮忙 :)

This won't work. 这行不通。 First of all, this is wrong: 首先,这是错误的:

static final String DB_URL = "jdbc:mysql://localhost:3306/mess";

The localhost will be your android phone and it doesn't have any MySQL database in it. 本地主机将是您的​​android手机,并且其中没有任何MySQL数据库。

You should plan a webservice that takes care of all requests to your DB and send the results back to your app. 您应该计划一个Web服务,该服务处理对数据库的所有请求,并将结果发送回您的应用程序。

To access/connect to your webservice, you could use HttpURLConnection. 要访问/连接到您的Web服务,可以使用HttpURLConnection。

What do you wish to achieve? 您想实现什么?

Your android app should save and upload data to a central server location? 您的Android应用程序应保存数据并将其上传到中央服务器位置? - If answer is yes, your options are Parse ( which you already tried i guess ) or build your own rest server api quickly. -如果答案是肯定的,则您可以选择Parse(我已经尝试过了)或快速构建自己的rest server api。 My personal fav is python rest api server on Google App Engine. 我个人最喜欢的是Google App Engine上的python rest api服务器。 https://cloud.google.com/appengine/docs/python/ https://cloud.google.com/appengine/docs/python/

If your android app should save data on the android device and need not communicate to any server then create local instance of sqlite database and store retrieve data from it. 如果您的android应用程序应将数据保存在android设备上并且不需要与任何服务器通信,则创建sqlite数据库的本地实例并从中存储检索数据。 Try this out - http://satyan.github.io/sugar/ 试试看-http: //satyan.github.io/sugar/

There are possibilities that you can run mysql database in android .But for that you have to cross compile the mysql source code for arm architecture becoz mobile phones have arm architecture You can use android ndk or arm gcc compiler for cross compilation. 有可能您可以在android中运行mysql数据库。但是,为此您必须交叉编译arm体系结构的mysql源代码,因为becoz手机具有arm体系结构您可以使用android ndk或arm gcc编译器进行交叉编译。 Once you copiled your libs it will generate the .so files (extension in linux ) .You have to add these libs to your project. 编译完库之后,它将生成.so文件(在Linux中为扩展名)。您必须将这些库添加到项目中。 Then using jni you have to write ac program that will use the mysql functions. 然后使用jni,您必须编写将使用mysql函数的ac程序。 like crud operation then using jni then you have to call that native c program from your java code . 如crud操作,然后使用jni,则必须从Java代码中调用该本地c程序。 this way you can do this. 这样您就可以做到这一点。 I did this once. 我做了一次。 But it is a very tedious and cumbersome task. 但这是一个非常繁琐且繁琐的任务。

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

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