2

I know this question is asked before also, but I want to know whether we can Connect to external database (e.g mySQL) from android device without using a webservice.

  1. I have already build the app using webservice but now they have asked us to make it without using webservice.

  2. Can anybody knows or give any reference about same ?

  3. I have all the required data about the database location i.e. server name, db name etc.

Actually in my requirement I am downloading and xml using a webservice which will have all the details the connection string, database name, server name, username , password etc. but the connection is to be done on runtime.

7
  • 1
    Yes you can. No, you shouldn't. There's no benefit to this, especially since you have already built the web service. There are significant disadvantages, however, such as the need for additional security and encryption. If "they" don't trust you enough on this, I'd start here: dev.mysql.com/usingmysql/java Commented Feb 28, 2013 at 4:57
  • you can use sqlite for it. download data from web services in background. and use local database. becoz you can't connect directly(without web services) mySql DB with android device. Commented Feb 28, 2013 at 4:57
  • @DhavalSodhaParmar, sure you can. It's just not easy. (And it shouldn't be done) Commented Feb 28, 2013 at 4:58
  • @323go : you want to say it is possible? HOW? give advice with code. Commented Feb 28, 2013 at 5:01
  • yes..I know but the requirement is to build the app and I need to show it to them ..and the methods like downloading it to SQLite db and working or using webservice is already done Commented Feb 28, 2013 at 5:01

1 Answer 1

1

Yes you can connect to remote database directly. I'm unsure about it's security though.

Try this:

  1. Download MySQL JDBC

  2. Add the .jar file in your Android project and add it as a library.

  3. Create a class that extends AsyncTask and execute the method there.

public class connectToServer extends AsyncTask<String, String, String>{ @Override protected String doInBackground(String... args) { try { connect(); } catch (Exception e) { Log.e("Error", e.toString()); } return null; } @Override protected void onPostExecute(String file_url) { } } public void connect(){ Log.e("Android", "SQL Connection start"); Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); String ip = "[your host server]"; String dbName = "[your dbName]"; String user = "[your userName]"; String password = "[your password]"; String connString = "jdbc:mysql://" + ip + "/" + dbName + ""; conn = DriverManager.getConnection(connString, user, password); Log.e("Connection", "Open"); Statement statement = conn.createStatement(); ResultSet set = statement.executeQuery("Select * from [table]"); statement.setQueryTimeout(0); while(set.next()) { Log.e("Data:", set.getString("[column_name]")); } }catch(Exception e) { Log.e("Error connection", e.toString()); } }//end method 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.