0

I have a Linux server which has a Database. I want to query the Database from my Android application.

All the tutorials I find tell me to use PHP for some reason.

Can't I just query like I would normally with a Java application? Which is, I connect to the Database then send my queries as Strings.

It seems I need something like this:

enter image description here

Why?

J_Strauton
  • 2,270
  • 3
  • 28
  • 70
  • 1
    because then your database is open to the whole wide world, and the connection strings are stored in an application anyone can dig into and discover. – pala_ Apr 21 '15 at 04:43
  • You can do it by JAVA, but not recommended. Check http://stackoverflow.com/questions/26470117/can-we-connect-remote-mysql-database-in-android-using-jdbc – Abhishek Apr 21 '15 at 04:47
  • This question may help: http://stackoverflow.com/questions/11105529/how-to-connect-android-with-mysql-using-mysql-jdbc-driver – Michas Apr 21 '15 at 04:51
  • Most kinds of databases run as separate processes, or are just files on the hard disk, and can be accessed by anyone/anything that has the right credentials. MySQL is such a database, and can be accessed by any programming language that has a MySQL database API (most have one). – Sverri M. Olsen Apr 21 '15 at 04:58
  • @pala_ ok that makes sense. So I must create a web service on the server that I can connect to to avoid this security leak, right? – J_Strauton Apr 21 '15 at 06:19
  • thats how i'd do it. simple little api web service that performs the database work for you. – pala_ Apr 21 '15 at 06:21

2 Answers2

2

PHP is just a server-side programming language. You can use any language and any server. The main issue is

Can I Query MySQL Database Directly from Android without a Web Service?

Yes you can. Just open the port where your MySql database is set usually at 3306. You also need JDBC Driver to set up connection to it.

Should I Query MySQL Database Directly from Android without a Web Service?

Unless you want to make a Database Client app (like phpMyAdmin) where each user holds his own credentials then you should not. In such case everyone will be using their own credentials to access their own db. In your case, you'd be hardcoding your database credentials in the app for everyone to access.

inmyth
  • 8,880
  • 4
  • 47
  • 52
0

Well, your database user is only allowed to connect from localhost not from the outer world. If you want to execute queries from outer world then you will have to allow access from all IPs i.e. You will have to create a user create user 'app'@'%' identified by password123 (In my opinion it will be a bad approach).
Moreover you can build REST Web Services either using PHP (Recommended) or JAVA to interact with the database without opening it to world wide.

Raja
  • 851
  • 9
  • 22
  • Why not Java?! Recommendations should be based in fact, not opinion. – Sverri M. Olsen Apr 21 '15 at 05:04
  • It is based on facts not from opinion. I have been building REST for around 3 years in both PHP & JAVA and find PHP more suitable than JAVA in this case though I love Java more than PHP :P – Raja Apr 21 '15 at 05:09
  • 1
    that is the very definition of an opinion, as opposed to a fact. – pala_ Apr 21 '15 at 06:20