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?

6
  • 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. Commented Apr 21, 2015 at 4:43
  • You can do it by JAVA, but not recommended. Check stackoverflow.com/questions/26470117/… Commented Apr 21, 2015 at 4:47
  • This question may help: stackoverflow.com/questions/11105529/… Commented Apr 21, 2015 at 4: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). Commented Apr 21, 2015 at 4: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? Commented Apr 21, 2015 at 6:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

Why not Java?! Recommendations should be based in fact, not opinion.
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
that is the very definition of an opinion, as opposed to a fact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.