9

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?

Community
  • 1
  • 1
Alex
  • 111
  • 1
  • 2
  • 10

6 Answers6

8

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

Josh Bolton
  • 104
  • 1
3

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).

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
2

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.

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
Rajesh Kumar
  • 133
  • 1
  • 1
2

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.

EboMike
  • 76,846
  • 14
  • 164
  • 167
0

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.

LizB
  • 2,193
  • 16
  • 17
  • So it's no possible to connect to Mysql using HttpClient, so what should use instead if I neither can use jdbc nor HttpClient? – Alex Aug 05 '10 at 22:48
  • 2
    You can use HttpClient you just need to write a web application that can process the information between the mobile device and the database. A RESTful web service would do what you want. http://www.ibm.com/developerworks/webservices/library/ws-restful/ – LizB Aug 05 '10 at 23:03
  • but, it seems that I need one webserver as middle tier, is that correct? – Alex Aug 05 '10 at 23:39
0

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.

Octoberdan
  • 460
  • 5
  • 10