How to connect and access SQL Database server from Android app??

Recently I have tried to connect to SQL Database server which is in my local network machine. I can connect and access SQL server from my Android app. I did it in the following way….

1. First of all you need a JDBC driver library for SQL Server. As we know android library has only SQLite database driver. So first download an open source JDBC driver from this http://jtds.sourceforge.net/ site (I downloaded the Linux version).

2. Then import the jar file into your Android app.(jtds-1.2.5.jar).

3. Now just try this code by modifying according to your context

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import net.sourceforge.jtds.jdbc.*;

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://server_ip_address :1433/DBNAME;encrypt=fasle;user=xxxxxxxxx;password=xxxxxxxx;instance=SQLEXPRESS;";
String username = "xxxxxx";
String password = "xxxxxxxxxx";
conn = DriverManager.getConnection(connString,username,password);
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet reset = stmt.executeQuery("select * from TableName");

//Print the data to the console
while(reset.next()){
Log.w("Data:",reset.getString(3));
//              Log.w("Data",reset.getString(2));
}
conn.close();

} catch (Exception e)
{
Log.w("Error connection","" + e.getMessage());
}
}

4. You will find more about parameter passing here in connection string  http://jtds.sourceforge.net/doc.html .

Advertisements

Android begining: Some problems those I faced when I started to develop android !!!

I have been developing android application since last 8 months. During that time I faced a lot of weird problems which wasted a lot of time and created a lot mental pressure on me. No I always try to keep in mind on those things before coding for android application. Let’s discuss some of those things…..

1. First of all I faced problem when I tried to work with functionality which takes a lot of time to finish execution( i.e  parsing a web url response content). It caused ANR (application not responding). Android recommendation was to use asynchronus task for heavy process (http://developer.android.com/reference/android/os/AsyncTask.html) . It is really simple to understand. But as beginner I wasted time to figure out it.

2. Second problem was to update the UI from background function/thread. There is a nice technique for this, use runOnUiThread() method for updating the UI instead from background thread.

3. Most time wasting issue was working with networking on android device specially with WIFI. If you use wifi for net connection or communicate through wifi wireless device/router don’t forget to wait until there is valid DHCP is assigned to your android phone/device. I was tried to do stuff as soon as wifi is enabled checking the network is available. Though there is a method for checking network availablity that made me a real fool for a long period of time. It shows that network is available but still the valid DHCP is not assigned.

4. The most dangerous issue I faced was “Virtual machine memory budget issue”. I worked on multiple IP camera streaming. First stage of development memory budget issue was occurring frequently and the app was crashing unexpectedly. There were so many threads and about 4 input streaming is closed at the same time and I found that if the heap memory size exceeds more than 10 MB (Android 1.6) then memory issue occurs. So when working with stream and graphics care should be taken that the app doesn’t hold up much heap memory.

Some important things while developing android application

To make your application more efficient remember the following things…………..

  • Accessing a local variable than a member field is more efficient in DVM (Dalvik Virtual Machine). So try to use local variable as you can .