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
Best way to implement Client <-> Server <-> Database architecture in an Android application?
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?
You should either use web services or implement an HTTP handler and transfer in a RESTful manner.
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).
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.
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.
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.
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.
Related
I've looked around trying to find an answer to this question, but have so far been unsuccessful. I have a current version of an Android mobile application that stores items in the local database, and I'm trying to hook it up to a 'cloud' database (not sure if this is the right term) so that other people that download the app will be able to view and post things to the database.
I have an ec2 service set up, with my LAMP stack installed. I have the database set up on this server ready to go. How do I go about hooking up the Android application so that it can communicate (insert/view/delete items) with the database? I am a new developer so any help/insight/guidance you have is much appreciated!
You haven't mentioned the type of database you have setup on cloud.
I am assuming it to be some kind of relational database (e.g. MySQL).
You can use standard JDBC connection from a regular java code but it is not yet possible (supported) to do it directly from Android code.
I faced a similar issue, what I did was this:
Create a Servlet and deployed it on GAE (Google App Engine).
Make an HTTP request from your android application code to this Servlet (SELECT/UPDATE/DELETE).
Initiate a JDBC connection to your database (on EC2) from that Servlet.
Shoot the SQL statement to the database.
Get the result and send the response back from servlet to your android code.
There may be other better solutions but I could not find any and tried this on my own, it worked like a charm, multiple times.
After writing the previous answer, I kept on thinking of alternate solution.
FYI: This may or may NOT work !
Instead of hosting your servlet on GAE, you can host it on the same EC2 instance as your database, using tomcat or any other package.
This way your android code would communicate directly with the instance that has servlet and database deployed together.
In the servlet, you can shoot SQL queries internally and send the results back to android code.
How to host a servlet in Tomcat 7
Adding new security group and linking it with my instance..worked for me
I am trying to learn to implement android apps to get news, promotion message, and calendar from server. What is the best and easy way to communicate with database server? using JDBC or other methods?
Thanks
Using JDBC?:
In the words of Commonsware:
Never 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.
On the client side (Android app), you can use SQLite to store data locally. It might not be necessary at all actually. For instance, it can be used for offline features, search, etc. For client-side, read up on this simple post
On the server side (whatever server side technology you know or want to learn), you can use whatever language, whatever database on whatever server OS you want. This part is commonly called the back-end, which will store your data while your app communicate with it through HTTP.
You can use json to parse data between server and the android device. In the server you can use jdbc with json if you are using servlets in the server.
To communicate between a server and an android device you can use JASON. See following links for some help.
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
https://www.learn2crack.com/2013/10/android-asynctask-json-parsing-example.html
https://www.learn2crack.com/2013/10/android-json-parsing-url-example.html
Also in the device to deal with data within the device you can use SQLite. And in the server you can use jdbc if you are using servelets.
To communicate with Database server You should use web service API such REST , soap
I want to insert, update, show records from an android app to a database (online) which will be installed on an android mobile.
So,my question is; which database is perfect?
MySQL
SQLite3
Any other
It's impossible to use MySQL in Android. Other SQL types neither. The best way is to use a MySQL database and get the information via PHP. Encode it to JSON in PHP. You can get the Web page in Android and decode it in Android.
Is mysql db is network database
MySQL (like many other database servers including Postgres and Oracle) can be accessed either through a local socket or via TCP/IP. (You also mentioned SQLite, this is a file based database and does not have a network server AFAIK).
There may be libraries for Android that implement a MySQL client, but a few minutes with Google didn't turn any up for me.
Generally speaking, network access to MySQL should be limited to private networks and not end clients anyway.
If you want to access a database from a client application over a network, you are usually better off by building an HTTP based API (preferably one that is RESTful) and letting the client software connect to that. This gives you more control over what clients are allowed to do with the database.
You can build such an API in just about any language you like. If you are working with Android, you might prefer Java. My preference would be Perl. Python is a good option, etc, etc.
which database is perfect?
Nothing is perfect. Giving a recommendation for a specific database would be drifting into "Not Constructive" territory, even if you were more specific about your requirements.
I am trying to develop a real-time Android application where all contents are stored in server. So, they are available whenever a connection to Internet is available. Also, the application provides communication between users and conversations are stored in the server as well. Nothing is locally stored.
However, I am still cannot decide which database type I can use. I intended to use SQLite but I am not sure if I can really use it or not.
Could you please guide me to the proper database type to my application.
Appreciate your time and efforts.
As its upto you which database you use.
you may Install Lamp (For Linux) or WAMP(for window) . This is a nice database tool and very easy to handle and easy linked with PHP for various database function
I recently developed something similar to what you are talking about and here is what I would suggest you to go for.
Use SQL server to manage the data on your desktop and create a web-service in .Net on Visual Studio.
(Note that as others have already mentioned, it really does not matter what is the database you are using in your server end, because eventually the data is going to come to the Android application from the server in form of either xml or json in the web-service., regardless of what database you are using. So it is totally your wish which database you want to use.)
Then connect to the web-service in your application and set/get data from the remote Database, using SOAP.
Link on how to make a web-service in .NET (does not include the implementation in Android).
Links on how to connect your service with Android : this, this and this.
I am new for Android Development. Now I want to connect Mysql with Android Application for uploading data to the server. Please send any sample code for connecting mysql with Android.
There is no MySQL connection library for Android that I'm aware of, unless you can get the MySQL java library operational under Android.
Either way, you don't want to do this - that would mean exposing your MySQL server to the world for TCP connections. There's no way to tell what IP your device will be appearing from. Each mobile carrier has their own internet gateway for devices, and it's invariably a NAT firewall to boot. This would be require you to leave MySQL wide open to TCP connections from all IPs, a major security hole.
Instead, you should build up a web service that acts as a middleman between your app and MySQL.
You can use MySql to connect to your android application through a servlet using Apache Tomcat. You will have to put your SQL syntax into the servlet you created and in your Java insert the URI path for the servlet.
i believe you need apache tomcat to run a servlet for your sql, do you have it?
The way it should work is , you use the sql-lite database to store the data that is generated in your application.Then, when you have a fixed set of data,convert into a protocol buffer, transmit the same via web-services,depacket it and store into a mysql database at the server.
If you need to manage data in a private database, use the android.database.sqlite classes.
Not sure if android supports mysql.
Go through the Notepad Tutorial for information on connecting to sqllite
http://developer.android.com/resources/tutorials/notepad/index.html
Other than sqlite we can't connect with any other database.
Using sqlite is also not a good practice, better use web service calls.
It is because, some viewers will be having less memory phones, has we are doing the application global, we should think of these things.
For small requirements you can use sqlite.