Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - Connect to mySQL database over LAN without using a webserver

Tags:

android

mysql

I want to have multiple clients that connect to a server over LAN and access/modify the mySQL database in the server.

How would i go about doing this? Can you guys provide some resources/links that i could research on the topic

like image 637
user6535530 Avatar asked Jul 11 '16 12:07

user6535530


2 Answers

To answer your question, you should be able to connect to a mysql database by adding the jdbc driver to your project as a jar file in Android Studio.

Now for a real app that you plan to distributed to thousands of users there are Security issues, Performance issues, and Scalability issues.

Security issues:

  1. You expose your database directly to the internet by opening its port to public access for the apps to connect. A web app adds a layer in the middle, keeping the database access inside the intranet.

  2. You expose your data directy to the public by providing at least one public account known by everybody (I assume this would be the way to access because managing one account per user wouldn't be realistic). A Web app isolates the user account from the database accounts.

  3. By providing access this way, as android mobile devices can be rooted, you are potentially granting anonymous access to your data.

Performance Issues:

  1. With a web app in the middle, it is the webapp who manages the connections to the database. This enables sharing connections amongst different users vs. one dedicated connection per user would have if the different devices estable separate connections.

  2. For the same reason, you can't take advantage of connection pooling, which saves the overhed of establishing a connection to the database for each incoming request.

Scalability issues:

  1. As connections are not shared the number of concurrent users will be bound to the number of connections you can open at the same time to the database.

EDIT 1

I am adding an alternative I thought of which involves using a web application but it is not implemented using a webserver. It is a java NIO framework that runs on its own. The limitations of this solution is you need shell access to the server and java, which is not common in traditional hostings. Checkout Netty.

like image 172
Juan Avatar answered Oct 04 '22 15:10

Juan


There are 2 ways how to do perform your task. You can either add the JDBC driver in android studio, or better implement a REST API that connects to your database, and all the android clients can send HTTP requests to the server and the server will add the information for you. Here you can implement the create, update, delete methods. For HTTP requests you can use Retrofit or Volley libraries.

If you want to use JDBC, check out the answer here How to Mysql JDBC Driver to android studio

But the best and most correct solution for this type of problems would be a REST Service

like image 32
Andrew T Avatar answered Oct 04 '22 16:10

Andrew T