Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect Android to a database server

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?

like image 458
Alex Avatar asked Aug 05 '10 22:08

Alex


People also ask

Can Android connect to SQL Server?

It uses the new credentials to create new database and table and insert values in it. Then it switches to Android Studio to show in simple steps of how to develop an App to interact with the MS SQL Server and database. It implements the required driver in the App's gradle file of the project.

How do I connect to a database server?

Connect to a SQL Server instanceStart SQL Server Management Studio. The first time you run SSMS, the Connect to Server window opens. If it doesn't open, you can open it manually by selecting Object Explorer > Connect > Database Engine. For Server type, select Database Engine (usually the default option).

How do mobile apps connect to database?

1- Make your own database (Sqlite) in your Android App. This means you have to synchronize via web services, with your server's database, whenever you see fit. Steps: a) Use an ORM to connect to your local database and to relational map your tables with your classes.


3 Answers

You should either use web services or implement an HTTP handler and transfer in a RESTful manner.

like image 66
Josh Bolton Avatar answered Oct 24 '22 08:10

Josh Bolton


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.

like image 21
EboMike Avatar answered Oct 24 '22 08:10

EboMike


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.

like image 33
Rajesh Kumar Avatar answered Oct 24 '22 06:10

Rajesh Kumar