3

When I say "direct database connection", I meant to use a JDBC-alike driver to call and run a remote database query within the context of an activity, just like using an SQLite database to store the local parameters.

Until I realized that I could barely find any tutorials or documents on the Internet regarding this way of database connection, I thought, regardless of the unstable Wi-Fi/3G network bore by most if not all Android devices, to connect to a database should be done like so.

Even in Android SDK, only local database access (a.k.a android.database.sqlite) is supported intrinsically. And it's stated in the description of java.sql API that you must provide your own JDBC driver in order to use this API.

Then I started to realize if my intention of directly connecting a database is incorrectly in the first place in the realm of Android.

I have read many books, wikis, and forums, including numerous answers provided on Stack Overflow. It appears to me that many people are concentrating on getting something done, and discussions used to start with "How can I design a RESTful web service for my Android device?". However, I need a thorough understanding! And I also need authoritative citations from the original Android developers.

2 Answers 2

10

A few reasons (but not nearly all reasons):

  1. Not existing: there are no (supported) JDBC drivers on Android.

  2. Authentication: you want users on a public network to be authenticated:

    A. Each user must have their own credentials (in some form) when talking to your servers. Having one username/pass baked into your app is asking for trouble.

    B. You don't want your database to provide authentication. For this you need a separate authentication layer.

  3. Standard protocol: if you want to make sure your app runs on all networks (especially on locked-down corporate networks) you need to use HTTP/HTTPS. It's the only protocol that works (almost) everywhere.

  4. Business-logic separation: If you support different device platforms (Android, iPhone, etc..) then it's wise to have all business logic on the server. Instead of calling JDBC and then performing business logic on device, you should do this on the server. That way you will conform to DRY (don't repeat yourself), and also it'll be easier to unit test.

7
  • 1
    What about jtds (hub.webring.org/hub/java)? I found this driver in some other posts, it's stated that this driver can be used by android device to enable JDBC connection. If there are no JDBC drivers on Android, why do we need that java.spl API in the first place?! And, do you mean that in order to access the database from an Android device it's always necessary to write a web service on the server??
    – lixiang
    Jan 30, 2011 at 7:30
  • Wow, a lot of questions for one comment: 1. JTDS might work, but there is no mention of Android on their site. You should give it a try. But my points 2.-4. still apply. Jan 30, 2011 at 8:21
  • 2. AFAIK, java.sql is a compatibility interface for accessing SqLite database: developer.android.com/reference/java/sql/package-descr.html Jan 30, 2011 at 8:22
  • 3. Yes this is the usual way of accessing private data over public networks. For reasons stated in points 2-4 above, people write their own access layer on the server. REST is the usual one. Don't fret, writing a REST layer is VERY simple: jboss.org/resteasy/docs Jan 30, 2011 at 8:27
  • Wow, very informative, thanks for your patience!! I think I have understood much better now. I am going to explore the websites you gave to me!! It's very kind of you!
    – lixiang
    Jan 30, 2011 at 9:30
4

I thought, regardless of the unstable Wi-Fi/3G network bore by most if not all android devices, to connect to a database should be done like so.

You are welcome to your opinion. I vehemently disagree with you. JDBC and similar protocols are designed for reliable, low-latency LANs, not unreliable, high-latency mobile networks. Moreover, you would need your database to be visible on the public Internet, meaning that it will be subject to hackers.

1
  • 1
    This is exactly what I meant, I thought I had not expressed my argument clearly. An unstable Wi-Fi/3G network is an unreliable, high-latency mobile network, hence JDBC is certainly not designed for such a network. Would you please recommend me some sources that introduce JDBC in a brief way?
    – lixiang
    Jan 30, 2011 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.