Connecting to MySQL from Android with Jdbc

Connecting to MySQL from Android with JDBC

You can't access a MySQL DB from Android natively. EDIT: Actually you may be able to use JDBC, but it is not recommended (or may not work?) ... see Android JDBC not working: ClassNotFoundException on driver

See

http://www.helloandroid.com/tutorials/connecting-mysql-database

http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/8339-connect-android-mysql-database-tutorial.html

Android cannot connect directly to the database server. Therefore we
need to create a simple web service that will pass the requests to the
database and will return the response.

http://codeoncloud.blogspot.com/2012/03/android-mysql-client.html

For most [good] users this might be fine. But imagine you get a hacker that gets a hold of your program. I've decompiled my own applications and its scary what I've seen. What if they get your username / password to your database and wreak havoc? Bad.

Using JDBC MySQL to connect database in Android?

Generally MySql is installed with security restrictions so it is possible to access to it only from localhost.

You need to execute a command similar to the following to permit access from other 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. So generally it is a good idea to place a server that intercept requests from your android application and directly call the database.


Note: check also if it is open the route between your application and the mysql server.

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). It is also possible that outside of the local network the MySql Server is not visible. 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. Otherwise you can access to the local network using a phone with wireless connection to your local network.

How to make android connect directly to mysql

Although it is not recommended for Android to connect with mysql directly, you can possibly do so. I actually found another stack overflow link that explains why it is not recommended (in first answer) and also gives answer of your original query (in second answer). Please have a look:

Can we connect remote MySQL database in Android using JDBC?

How to connect Android with MySQL using Mysql JDBC driver

Android by default does not support MySQL. It has an in-built database i.e SQLite.
If you are trying to access MySQL database remotely, you should expose interface to this database with any standard web service.
E.g you could create RESTful Web Service on Server Side which is written using Java/PHP etc. and MySQL Connector. (Which you have already done!)
And your Android App could talk to this service with the URL generated using this web service.

Again, this question has been repeated previously, so you can check those solutions.



Related Topics



Leave a reply



Submit