Connecting external database in android without using webservice - android

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.
I have already build the app using webservice but now they have asked us to make it without using webservice.
Can anybody knows or give any reference about same ?
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.

Yes you can connect to remote database directly. I'm unsure about it's security though.
Try this:
Download MySQL JDBC
Add the .jar file in your Android project and add it as a library.
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

Related

Android studio connectivity with postgresql

I'm new to android and not much aware about it. I though have been through tutorial but still didn't get any solution. How to connect Android Studio with postgressql? Step by step!
I wrote this code in my MainActitvity.java. Is this correct? Or should I write it else where?
static final String JDBC_DRIVER = "org.postgresql.Driver";
static final String DB_URL = "jdbc:postgresql://localhost:5432/user1";
// Database credentials
static final String USER = "root";
static final String PASS = "root";
public static void main(String[] args)
{
Connection conn = null;
Statement st = null;
try{
//STEP 2: Register JDBC driver
Class.forName("org.postgresql.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/","root","root");
//STEP 4: Execute a query
System.out.println("Creating statement...");
st = conn.createStatement();
String sql;
sql = "SELECT first, last FROM Employees";
ResultSet rs = st.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
String first = rs.getString("first");
String last = rs.getString("last");
//Display values
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
//STEP 6: Clean-up environment
rs.close();
st.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}
finally
{
//finally block used to close resources
try{
if(st!=null)
st.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}//end finally try
}
}
use 10.0.2.2 instead of localhost, it works for me.
You cannot directly use java.sql.DriverManger, Connection, etc in Android. Android support SQLite DB, if you want to use DB in android you have to go with SQLite database. For Postgres you have to develop server side application and api services which you can the call from Android
Okay, this may be obsolete but still helpful for users (it was helpful for me)
I copied your example and worked with it because I also need to get postgres running on android. And it works!
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/","root","root");
This will result in an error because you need to enter the database name without a slash at the and, like:
conn = DriverManager.getConnection("jdbc:postgresql://domain.com:5432/databaseName", "username", "password");
Network connections (like connection to database) must be done in an AsyncTask using doInBackground(). I did it inside an activity
public class dbactivity extends AppCompatActivity { //sry code formatting just broke
String message = null;
String conmsg = null;
private class pgsqlcon extends AsyncTask<Void, Void, Void>
{
public pgsqlcon()
{
super();
}
#Override
protected Void doInBackground(Void... voids) {
Connection conn = null;
Statement st = null;
try
{
//STEP 2: Register JDBC driver
Class.forName("org.postgresql.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to database...");
message = "Connecting to database...";
conn = DriverManager.getConnection("jdbc:postgresql://serverdomain.com:5432/databasename",
"dbusername", "password");
//and so on
If you need to make UI changes like setText, you must use runOnUiThread like so ():
//using quote because code formatting doesn't work anymore for me xD
private void setAsyncText(final TextView text,final String value){
runOnUiThread(new Runnable() {
#Override
public void run() {
if (value == null)
text.setText("null");
else
text.setText(value);
}
});
}
Oh yeah and last but not least, since I wrote this inside an Activiy, I have to trigger the trouble by calling my asynctask in OnCreate() of my Activity.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dbactivity);
pgsqlcon pgcon = new pgsqlcon();
pgcon.execute();
}
}
I am not that experienced by myself so you can use this only for getting a connection at all to your postgresdb using JDBC only. Although I managed to get successful query results that way.
And again, sorry for the wrong code formatting. I did what they wanted (4 space rule etc.) and it didn't work. I hope you can read it anyway, good luck.
And if nothing of this does work, maybeeee you want to take a look at these little hints: https://jdbc.postgresql.org/documentation/head/prepare.html
(I assume you did that anyway since you have done a lot of almost correct code)
My app uses PostgreSQL as backend. Use the retrofit library for connecting to the backend. In my app backend is written in python which will make queries in the database. This will make the front-end codes more smooth and secure. And the more controls can be shifted to the back-end.
You can not connect the database with android studio directly,
you have to make connection with your application and database through api ,
and you can write your api in java, php etc.
?php
$db_connection = pg_connect("host=localhost dbname=record user=postgres password= ''");
//pg query
?>
This is your connect query api.

connect android apps to sql server [duplicate]

Is there a way that we can connect an Android application to a central database server (e.g. MSSQLServer 2008)?
I have a MySQL database that is accessed by both website and Android. Connecting to the database from the website is fine, but how can it be done from the Android app?
here are some similar questions asked (an answered):
android MySQL connection
How to connect to a MySQL Database from an Android App?
Even though those are for MySQL, it should work for MSSQL by changing the engine or the driver's use to connect. Usually, the approach is to expose some limited level of modification through a web service. Still, nothing is stopping you from directly accessing the database, albeit depending on the case, could pose a security risk.
Main Reasons the web service approach is taking:
Performance
Security
Best Practice
Separation of concerns
An exception is if you want to enable direct access because you're building a sort of database client through mobile.
All you have to do is use the appropriate driver, i'd recommend using JTDS, and version 1.2.5 seems to have worked well with android.Detailed instruction on how to use with eclipse can be found here
A working code is available in github
/**
* This is a demo code to demonstrate db connection and operations and not
* meant for a live run.
*
*/
public class DBTestActivity extends Activity {
private Connection conn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dbtest);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dbtest, menu);
return true;
}
#Override
protected void onResume() {
super.onResume();
(new DBConnection()).execute(null, null, null);
}
#Override
protected void onPause() {
super.onPause();
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
class DBConnection extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... arg0) {
try {
Log.e("MSSQL", "Attempting to connect");
Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:jtds:sqlserver://yourserver.com/DBName",
"username", "password");
Log.e("MSSQL", "Connected");
} catch (Exception e) {
e.printStackTrace();
Log.e("MSSQL", e.toString());
}
return null;
}
}
class UserInfo {
String userID;
String userName;
String PhoneNo;
String age;
public UserInfo(String userID, String userName, String PhoneNo,
String age) {
this.userID = userID;
this.userName = userName;
this.PhoneNo = PhoneNo;
this.age = age;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPhoneNo() {
return PhoneNo;
}
public void setPhoneNo(String phoneNo) {
PhoneNo = phoneNo;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
class DBOperation {
public List<UserInfo> getAllUsers() throws SQLException {
Statement statement = getStatement(conn);
List<UserInfo> userlist = new ArrayList<UserInfo>();
ResultSet rs = statement.executeQuery("SELECT * FROM UserInfoTable");
rs.next();
int count = 0;
while (rs.next()) {
userlist.add(new UserInfo(rs.getString(1), rs.getString(2),
rs.getString(3), rs.getString(4)));
count++;
}
rs.close();
statement.close();
return userlist;
}
public void addUser(UserInfo info) {
Log.e("MSSQL", "in adduser");
Statement statement = getStatement(conn);
try {
ResultSet rs = statement.executeQuery("INSERT INTO UserInfoTable "
+ " VALUES ('1001', 'Bob', '333333', '33')");
rs.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private Statement getStatement(Connection connection) {
try {
return connection.createStatement();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
An other approach, musch simpler than Web Service, is to use a Virtual JDBC Driver that uses a three-tier architecture: your JDBC code is sent through HTTP to a remote Servlet that filters the JDBC code (configuration & security) before passing it to the SQL Server JDBC Driver. The result is sent you back through HTTP. There are some free software that use this technique. Just Google "Android JDBC Driver over HTTP".
Connecting your android application directly to an external database server is a bad idea, instead create a web application and access the database through that.
There are a number of strategies you can employ to accomplish what you want to do. Given that the SOAP support for Android is non-existent, you're going to most likely want to push the data out in either XML or JSON format through WCF, ASP.NET, Ruby On Rails, PHP or any number of web frameworks.
Without knowing what your web application is currently running, it's hard to say how to best make that data connection. You can use WCF Data Services if you want to get up and running as fast as possible, and MSDN has a decent article on getting started with it:
http://msdn.microsoft.com/en-us/library/cc668792.aspx
I suggest that you examine your existing solution and figure out how to best extend that to push data out to your Android app.
i've try to connect android via PHPto ms sql server, you can read here,am using httprequest and json. If you want to connect to Ms SQL Server 2005 or higher, you must download Microsoft Driver for PHP for SQL Server.
I've used php as web service to connect Ms SQL Server database, anyway you can used jdbc to connect from android direct to MS SQL Server database
If you need to do that probably you donìt have already a 3 tier architecture. If this is the case consider writing a mobile web application.
I did that to add mobile interface to a client server 2 tier legacy system.
I had a legacy Delphi client/server app, and i created by using Raudus a web app optimized for mobile devices. Of course this makes sense if you have Delphi skills, but for other languages/tecnhologies there are the coutnerparts.

Connecting and creating an android application to retrieve data from MSSQL

Am kind of new to android.the thing is i programmed a vb code to upload some data to SQL server including an image.so i want to create an android app to view these data by creating a drop down list with the main entry in the database..so whenever i choose something from the drop down list it suppose to show the rest of the columns..sorry for my English but i hope u guys got the idea.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void query2() {
Log.i("Android"," MySQL Connect Example.");
Connection conn = null;
try {
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
//test = com.microsoft.sqlserver.jdbc.SQLServerDriver.class;
String connString = "jdbc:jtds:sqlserver:// AMD/SQLEXPRESS :1433/***********;encrypt=fasle;user=*******;password=*********;instance=SQLEXPRESS;";
String username = "Shawtari";
String password = "";
conn = DriverManager.getConnection(connString,username,password);
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet forest = stmt.executeQuery("select * from aspnet_Users");
//Print the data to the console
while(forest.next()){
Log.w("username",forest.getString(3));
}
conn.close();
} catch (Exception e) {
Log.w("Error connection","" + e.getMessage());
}
}
}
Use a Spinner to show your main entries and a ListView to show the columns
Add a OnItemSelectedListener for this spinner. In this listener, make the JDBC call as a background task preferably using AsyncTask.
After getting the response, populate the listview . You may need to implement a custom adapter which takes the ResultSet and parse it to feed it to your listview.

connecting Android to Oracle

I'm new and am trying to connect an android application to my oracle db. My current code is throwing the error:
android.os.NetworkOnMainThreadException ...
My connection code is as follows:
public class ConnectOra {
public String driver, url, ip, bd, usr, pass;
public Connection conexion;
public ConnectOra(String ip, String bd, String usr, String pass) {
driver = "oracle.jdbc.driver.OracleDriver";
this.bd = bd;
this.usr = usr;
this.pass = pass;
url = new String("jdbc:oracle:thin:#" + ip + ":1521:" + bd);
try {
Class.forName(driver).newInstance();
conexion = DriverManager.getConnection(url, usr, pass);
// revisan el log para ver que tira....
System.out.println("Conexion a Base de Datos " + bd + " Exitosa");
} catch (Exception exc) {
System.out.println("Error al tratar de abrir la base de Datos "
+ bd + " : " + exc + "ricardo");
}
}
public Connection getConexion() {
return conexion;
}
public Connection CerrarConexion() throws SQLException {
conexion.close();
conexion = null;
return conexion;
}
Well as the error suggests, you cannot do a network connection on the main thread... So put it in a separate thread.
You have to use another thread for network tasks.
I recommend you use AsyncTask.
U should implement AsyncTask and add: to your android mainfest.
Anyway, it's a really bad practice in termns of security made a direct connection from any local client to a remote DB, that model extinguished for very good reasons years ago, in this case, in Android, if u have the apk file, u can decompress it and then decompile the java source files for get the db credentials and gain complete access to the db.

Connecting android with MS SQL SERVER 2008

Is there a way that we can connect an Android application to a central database server (e.g. MSSQLServer 2008)?
I have a MySQL database that is accessed by both website and Android. Connecting to the database from the website is fine, but how can it be done from the Android app?
here are some similar questions asked (an answered):
android MySQL connection
How to connect to a MySQL Database from an Android App?
Even though those are for MySQL, it should work for MSSQL by changing the engine or the driver's use to connect. Usually, the approach is to expose some limited level of modification through a web service. Still, nothing is stopping you from directly accessing the database, albeit depending on the case, could pose a security risk.
Main Reasons the web service approach is taking:
Performance
Security
Best Practice
Separation of concerns
An exception is if you want to enable direct access because you're building a sort of database client through mobile.
All you have to do is use the appropriate driver, i'd recommend using JTDS, and version 1.2.5 seems to have worked well with android.Detailed instruction on how to use with eclipse can be found here
A working code is available in github
/**
* This is a demo code to demonstrate db connection and operations and not
* meant for a live run.
*
*/
public class DBTestActivity extends Activity {
private Connection conn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dbtest);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.dbtest, menu);
return true;
}
#Override
protected void onResume() {
super.onResume();
(new DBConnection()).execute(null, null, null);
}
#Override
protected void onPause() {
super.onPause();
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
class DBConnection extends AsyncTask<String, String, String> {
#Override
protected String doInBackground(String... arg0) {
try {
Log.e("MSSQL", "Attempting to connect");
Class.forName("net.sourceforge.jtds.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:jtds:sqlserver://yourserver.com/DBName",
"username", "password");
Log.e("MSSQL", "Connected");
} catch (Exception e) {
e.printStackTrace();
Log.e("MSSQL", e.toString());
}
return null;
}
}
class UserInfo {
String userID;
String userName;
String PhoneNo;
String age;
public UserInfo(String userID, String userName, String PhoneNo,
String age) {
this.userID = userID;
this.userName = userName;
this.PhoneNo = PhoneNo;
this.age = age;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPhoneNo() {
return PhoneNo;
}
public void setPhoneNo(String phoneNo) {
PhoneNo = phoneNo;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
class DBOperation {
public List<UserInfo> getAllUsers() throws SQLException {
Statement statement = getStatement(conn);
List<UserInfo> userlist = new ArrayList<UserInfo>();
ResultSet rs = statement.executeQuery("SELECT * FROM UserInfoTable");
rs.next();
int count = 0;
while (rs.next()) {
userlist.add(new UserInfo(rs.getString(1), rs.getString(2),
rs.getString(3), rs.getString(4)));
count++;
}
rs.close();
statement.close();
return userlist;
}
public void addUser(UserInfo info) {
Log.e("MSSQL", "in adduser");
Statement statement = getStatement(conn);
try {
ResultSet rs = statement.executeQuery("INSERT INTO UserInfoTable "
+ " VALUES ('1001', 'Bob', '333333', '33')");
rs.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private Statement getStatement(Connection connection) {
try {
return connection.createStatement();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
An other approach, musch simpler than Web Service, is to use a Virtual JDBC Driver that uses a three-tier architecture: your JDBC code is sent through HTTP to a remote Servlet that filters the JDBC code (configuration & security) before passing it to the SQL Server JDBC Driver. The result is sent you back through HTTP. There are some free software that use this technique. Just Google "Android JDBC Driver over HTTP".
Connecting your android application directly to an external database server is a bad idea, instead create a web application and access the database through that.
There are a number of strategies you can employ to accomplish what you want to do. Given that the SOAP support for Android is non-existent, you're going to most likely want to push the data out in either XML or JSON format through WCF, ASP.NET, Ruby On Rails, PHP or any number of web frameworks.
Without knowing what your web application is currently running, it's hard to say how to best make that data connection. You can use WCF Data Services if you want to get up and running as fast as possible, and MSDN has a decent article on getting started with it:
http://msdn.microsoft.com/en-us/library/cc668792.aspx
I suggest that you examine your existing solution and figure out how to best extend that to push data out to your Android app.
i've try to connect android via PHPto ms sql server, you can read here,am using httprequest and json. If you want to connect to Ms SQL Server 2005 or higher, you must download Microsoft Driver for PHP for SQL Server.
I've used php as web service to connect Ms SQL Server database, anyway you can used jdbc to connect from android direct to MS SQL Server database
If you need to do that probably you donìt have already a 3 tier architecture. If this is the case consider writing a mobile web application.
I did that to add mobile interface to a client server 2 tier legacy system.
I had a legacy Delphi client/server app, and i created by using Raudus a web app optimized for mobile devices. Of course this makes sense if you have Delphi skills, but for other languages/tecnhologies there are the coutnerparts.

Categories

Resources