How to fill data in Android Gridview using MS SQL Database

In my previous posts I explained How to connect Android with MS SQL DatabaseSimple Login application in android using MS SQL Database. In this post I will be explaining how to fill Data in a gridview. First of all create a database in MS SQL with name MyDatabase and create following table with name “countries”

USE [mydatabase]
/****** Object: Table [dbo].[Countries] Script Date: 10/04/2015 19:20:56 ******/
CREATE TABLE [dbo].[Countries](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CountryName] [nvarchar](50) NOT NULL,
[Continent] [nvarchar](50) NULL

Download Source code.

The Layout file :

Make a xml layout file in your project named gridviews.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000" android:orientation="vertical" >

<GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:numColumns="auto_fit" android:stretchMode="columnWidth" >


I am going to use a custom layout for the gridview to display data. Make a new layout file named templateforgrid.xml in the layout folder.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#000" android:orientation="vertical" android:padding="5dp" >

<TextView android:id="@+id/txtcountry" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5px" android:text="COUNTRY" android:textStyle="bold" android:textColor="#ff3c3c" android:textSize="16sp" >

<TextView android:id="@+id/txtcontinent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5px" android:text="CONTINENT" android:textStyle="bold|italic" android:textColor="#0080ff" android:textSize="16sp" >



Make a new activity class file in your package with name (I was not getting any good name!!).

package com.hitesh.mssqlapp;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.GridView;
import android.widget.SimpleAdapter;

public class ForGrid extends ActionBarActivity {
GridView gridview;
ArrayList<String> arrayList;

String ip, db, un, passwords;
Connection connect;
PreparedStatement stmt;
ResultSet rs;

protected void onCreate(Bundle savedInstanceState) {

ip = "";
un = "sa";
passwords = "123";
db = "MyDatabase";
gridview = (GridView) findViewById(;

connect = CONN(un, passwords, db, ip);
String query = "select * from countries";

try {
connect = CONN(un, passwords, db, ip);
Statement statement = connect.createStatement();
rs = statement.executeQuery(query);
List<Map<String, String>> data = null;
data = new ArrayList<Map<String, String>>();

while ( {
Map<String, String> datanum = new HashMap<String, String>();
datanum.put("A", rs.getString("CountryName"));
datanum.put("B", rs.getString("Continent"));

String[] from = { "A", "B" };
int[] views = {, };
final SimpleAdapter ADA = new SimpleAdapter(ForGrid.this,
data, R.layout.templateforgrid, from, views);
} catch (SQLException e) {


private Connection CONN(String _user, String _pass, String _DB,
String _server) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
Connection conn = null;
String ConnURL = null;
try {

ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
+ "databaseName=" + _DB + ";user=" + _user + ";password="
+ _pass + ";";
conn = DriverManager.getConnection(ConnURL);
} catch (SQLException se) {
Log.e("ERRO", se.getMessage());
} catch (ClassNotFoundException e) {
Log.e("ERRO", e.getMessage());
} catch (Exception e) {
Log.e("ERRO", e.getMessage());
return conn;



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="" package="com.hitesh.mssqlapp" android:versionCode="1" android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name=".ForGrid" android:label="@string/app_name" >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


Download Source code.

  1. - Realtime Encode your string to Base64 format.
  2. - Realtime Decode Base64 data to plain text.
  3. - Realtime Encoding in Md5Hash.

4 thoughts on “How to fill data in Android Gridview using MS SQL Database”

  1. Pingback: Connect Android to MS SQL Database. • ParallelCodes

  2. Pingback: Connect Android to MS SQL Database. • ParallelCodes

  3. Pingback: Android Gridview Control - A detailed Overview • ParallelCodes;

  4. Pingback: Android - Creating Simple Launcher in Android Studio • ParallelCodes();

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.