Alex
Alex

Reputation: 131

How to connect Android to a database server

Is there any sample? I have my android application and I need to connect to mysql server on my machine, what is the best way?

I should not use jdbc, explanation here

Never never use a database driver across an Internet connection, for any database, for any platform, for any client, anywhere. That goes double for mobile. Database drivers are designed for LAN operations and are not designed for flaky/intermittent connections or high latency.

And should go for:

DefaultHttpClient httpclient = new DefaultHttpClient(); 

But there is no example in how to open a connection or execute a simple sql statement. anyone could help me?

Upvotes: 11

Views: 79767

Answers (6)

Neil McGuigan
Neil McGuigan

Reputation: 48236

I'm a fan of Offline First, meaning your app should be usable without a connection.

To facilitate this, I would recommend using a SQLite local database, and syncing to a remote database when online.

You can use a tool like SymmetricDS or Daffodil Replicator to sync your local and remote databases over HTTP(S).

Upvotes: 3

Rajesh Kumar
Rajesh Kumar

Reputation: 131

I tried to connect DatabaseServer from an Android Application,initially i faced some issue while i was using jtds, jar package for database Driver Support, instead of using jtds jar file use mysql-Connector jar file for Database Driver Support. mysql-connector jar file "mysql-connector-java-5.1.24-bin.jar". put this jar file in projects libs folder.

a little bit code snippet :

`String url="jdbc:mysql://(IP-of databaseServer):3306/DBNAME";`
`String driver="org.gjt.mm.mysql.Driver";`
`String username="XYZ";//user must have read-write permission to Database
`String password= "*********";`//user password

`try{
    Class.forName(driver).newInstance();//loading driver
    Connection con = DriverManager.getConnection(url,username,password);
    /*once we get connection we can execute ths SQL command to Remote Database Server with the help mysql-connector driver over Internet*/

}`    
`catch(Exception e){
    e.printStackTrace();
}`

I hope it could work.

Cheers Rajesh P Yadav.

Upvotes: 2

Octoberdan
Octoberdan

Reputation: 460

If you need to share data between multiple phones, I would recommend exposing it as a web service. If the data only needs to be accessible on that particular phone (and you want to remain relational) you can use sqlite.

Upvotes: 0

Josh Bolton
Josh Bolton

Reputation: 114

You should either use web services or implement an HTTP handler and transfer in a RESTful manner.

Upvotes: 9

EboMike
EboMike

Reputation: 77722

In order to connect to a MySQL server, you need a MySQL client. Android does not come with any MySQL libraries. You may be able to take a generic Java MySQL library and fudge it to work with Android, but that would be a big undertaking and wasted time.

The link you pointed to already told you that what you're trying to do is wrong in the first place. Don't connect to a database across the internet! You will need something on your server that responds to HTTP requests, looks up data in the database, and sends them back via HTTP. The link already mentioned a few options. You could even write something yourself, although it's most likely easier to use an existing solution than trying to make your own approach safe and hack-proof.

Upvotes: 3

LizB
LizB

Reputation: 2213

You won't be able to connect directly to a MySQL database with the HttpClient, as the MySQL database doesn't operate on that protocol.

The second part of the answer to the question you linked recommends going with web services and consuming those to communicate with the database server. Which is exactly what you would be able to do with the HttpClient.

Upvotes: 0

Related Questions