-1

I am new to android & I am trying to learn how to connect my app to a Mysql database.

With some research I found that its not recommended to use JDBC for Database connection in Android.

Also some alternatives I found seem pretty weird, they were using a PHP file to handle the connection and query part inside the App.

I am confused now, Can you please explain how I should co about doing it ?

and also how its generally recommended ?

Guru Prasath
  • 5
  • 1
  • 3
deepeshdm
  • 43
  • 4

1 Answers1

1

JDBC is rarely used in Android apps, for many reasons. MySQL can, in principle, be made to work in an app, and there are other posts on SO that explain the steps needed to make it work.

But...

In the end, JDBC is an interface to database wire protocols, that are rarely suitable for the kind of networking used in mobile computing. The usual way to do database operations in an Android app is to expose them as webservices on some server that has a strong network connection to the database. The webservice would do the low-level data manipulation, perhaps using JDBC, while accepting and presenting data in a form that is suitable for handling in an app. There are many Java libraries for simplifying the work of acting as a webservice client.

In fact, this approach is almost always used for wide-area access to databases in all contexts, not just Android apps. Most organizations will not expose database wire protocols to the public Internet, because these protocols are hard to route, and it's hard to do it securely.

In short, unless your database server is accessible to your mobile device over a WiFi connection, direct JDBC connection is unlikely to work well, even if the Android-specific problems can be overcome. It's almost not worth trying to overcome these problems, because JDBC is unlikely to be robust, secure, or bandwidth-efficient even if it actually works.

Kevin Boone
  • 4,092
  • 1
  • 11
  • 15
  • And also opening a database port on the server to the world. Nice mention of web services. – Joop Eggen Oct 08 '20 at 13:49
  • Recent versions of MySQL Connector/J will not even work on Android as it uses Java features not available on Android. – Mark Rotteveel Oct 09 '20 at 09:31
  • Indeed -- that's always a risk. Android has an impoverished Java (and C) API. It's probably best to assume that you can't use any external connectivity that uses APIs beyond those provided by Google. Of course, that's very limiting. – Kevin Boone Oct 09 '20 at 09:42