3

I would like to connect to a MySQL Database hosted on the same server with a Tomcat Server from my Android App without using PHP. Is there any way to do this? I found some solutions but all use PHP.

Can I connect directly or do I have to do it through a Web server?

Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
Rafa Romero
  • 31
  • 1
  • 1
  • 2

3 Answers3

2

Because you're running on Tomcat I imagine the best way to access the database would be to write a webservice that handles the communication between the app and the server.

The webservice will be written in Java to run on TomCat using JSP.

I'm guessing you're trying to communicate directly with the MySQL database (i.e. run SQL commands on the database directly) but I don't think you can do this (although never tried or looked into it), I have always been under the impression that you need some code on the server to sit inbetween.

Community
  • 1
  • 1
Matt Gaunt
  • 9,434
  • 3
  • 36
  • 57
  • obviously Tomcat has nothing to do with mysql server. – Ilya Saunkin Mar 09 '11 at 22:54
  • no but if Necronet wants to access a mysql service which is setup on a tomcat server, I imagine he's going to need to set up a script to communciate with it from the app [was the point I was trying to make]. If there is a way of doing it help out Necronet and tell him :P – Matt Gaunt Mar 09 '11 at 23:05
  • tomcat and mysql are used together more often than not, so +1. You could use Servlets or JSP to interact with the DB (MySql or other)... – Dori Mar 10 '11 at 09:23
1

It should be common sense, that directly communicating with databases over the web is a "no go" security wise and with mobile devices a pain regarding the connectivity.

Setup a webservice with JSP or Grails (which I find comes with less workload) and deploy it to your tomcat server.

Hence the thought, you already have a Tomcat running, one assumes you have a java web app running. Try adding a webservice to that app or look in the documentation, if there already is one.

chris polzer
  • 3,219
  • 3
  • 28
  • 44
0

You actually cannot do this with Android the main reason is performance it is really expensive to keep a remote connection alive than rather just call Web Services on demand, and it is more portable.

So i recommend you to play around with your favorite language creating services that access to your database and digest the output (XML or JSON) with android.

BTW i also think this have been asked Android MySQL Connectoin and here is a nice tuto about it (but with PHP) it should be fairly easy to do it in Java.

Community
  • 1
  • 1
Necronet
  • 6,704
  • 9
  • 49
  • 89
  • 2
    This is quite broad statement, maintain a socket can be expensive, but for some applications it is worth it (i.e. a single socket for all push notifications on a device is done and is justified because it is made as efficient as possible). Maintaining a connection over a webservice for MySQL is generally not a good idea and not good practice, it's much safer to build a webservice to communicate between the two. JSP should be quite easy to find a tutorial for to do this kind of thing. I would say I personally find php easier for web services though (JSP is great for enterprise stuff though). – Matt Gaunt Mar 09 '11 at 23:19