Android access to remote SQL database

Can I access a remote SQL database (on a webserver) directly from an android program? i.e. simply open up the connection with all the required parameters and then perform an SQL query?

This is a private program (not available to the public) only available on specified handsets, so I am not worried about third parties getting a hold of database access.

If so - what libraries do i need in Java?

Thanks.


This question has popped up several times. You PROBABLY can connect your android device to the SQL server directly if you deployed the MSSQL JDBC drivers to your android device and then exposed your SQL server directly to the internet. If the MSSQL drivers would work properly on Android is a completely different problem.

That is how you might be able to do it. However here is why that is a bad idea.

  1. You are exposing your SQL server directly to the internet. Unless you encrypt the data between your MSSQL server and android device it would be relatively easy for a determined hacker to sniff the TDS data stream between the device and MSSQL and reverse engineer it and steal your data. Encryption will probably make it much harder almost impossible for a attacker to steal your data. However an attacker could still launch a DOS/DDOS attack on your database directly. Not a good idea!

  2. If you are planning to connect other mobile devices (iPhone, Symbian, BlackBerry and so on) you will need to be able to create a SQL connection from those devices as well. iPhone does not support Java natively(from my memory) for example so you would need to find a way to connect iPhone to the SQL server. BlackBerry might be easier but Symbian you are going to be out of luck with. Thus you will need to almost create a custom solution for each device connecting to your database. Bad Idea LOADS of maintenance

Create a webservice or custom TCP/IP server which can manipulate your database. Connect to this webservice/service from your device. Webservices are the way to go. More than 90% of devices these days are natively capable of doing a webservice call.


I think you have to use webservice to communicate with a SQL database. you can define general methods like runSomeScalarQuery or runOneTabularQuery and ... in that webservice and send database responses via self defined protocol like a self defined object.