简体   繁体   中英

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.
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?
Is it even possible, that my app connects remotely (assuming I have granted privileges to all IP's)

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.

You should plan a webservice that takes care of all requests to your DB and send the results back to your app.

To access/connect to your webservice, you could use HttpURLConnection.

What do you wish to achieve?

Your android app should save and upload data to a central server location? - If answer is yes, your options are Parse ( which you already tried i guess ) or build your own rest server api quickly. My personal fav is python rest api server on Google App Engine. 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. Try this out - 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. Once you copiled your libs it will generate the .so files (extension in linux ) .You have to add these libs to your project. Then using jni you have to write ac program that will use the mysql functions. like crud operation then using jni then you have to call that native c program from your java code . this way you can do this. I did this once. But it is a very tedious and cumbersome task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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