Click here to Skip to main content
15,885,875 members
Articles / Mobile Apps / Android
Tip/Trick

Direct Access to SQL Server From Android

Rate me:
Please Sign up or sign in to vote.
4.54/5 (5 votes)
19 Nov 2015CPOL 39.6K   2.2K   9   17
Using JTDS - SQL Server and Sybase JDBC driver to access SQL Server from Android APP

Introduction

Sometimes, we need to deploy mobile app for local network. Open source JDBC 3.0 type 4 driver for Microsoft SQL Server (6.5 up to 2012) and Sybase ASE. jTDS is a complete implementation of the JDBC 3.0 spec and the fastest JDBC driver for Microsoft SQL Server.

Background

  1. Basic Android development
  2. Can using SQL Server

GUI XML Code As

XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    android:background="#336699"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/etFirstName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp"
        android:background="#eeeeee"
        android:hint="Firstname"
        android:textColor="#000000"
        android:layout_marginBottom="4dp"
        android:textSize="24dp" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/etLastName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#eeeeee"
        android:hint="Lastname"
        android:padding="4dp"
        android:textColor="#000000"
        android:textSize="24dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="4dp" >

        <Button
            android:id="@+id/btnConnect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Connect" />

        <Button
            android:id="@+id/btnAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_weight="1"
            android:text="Add new" />
    </LinearLayout>

    <TextView
        android:id="@+id/tvDs"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:padding="8dp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

Register Access Permission for your App

Java
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

Connect to Microsoft SQL Server by JDBC

Java
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(""
                            + "jdbc:jtds:sqlserver://172.16.160.81/northwind;instance=SQL2008;"
                            + "user=sa;password=sa;");

Description

  • 172.16.160.81 is your database's server address
  • northwind: access database
  • SQL2008 is server instance
  • user and password

Reading data:

Java
Statement comm;
try {
// create command to read data
    comm = conn.createStatement();
    ResultSet rs = comm.executeQuery("Select EmployeeID, Firstname From Employees");
    String msg = "";
// read all row
    while (rs.next()) {
        msg += "\nID: " + rs.getInt("EmployeeID") + " Name: "
                + rs.getString("Firstname");
    }
    tv.setText(msg);
} catch (SQLException e) {
    tv.setText(e.toString());
}

AddNew Record

Java
PreparedStatement comm;
try {
    comm = conn.prepareStatement("insert into Employees("
            + "firstname, lastname) values(?,?)");
    comm.setString(1, etFirst.getText().toString());
    comm.setString(2, etLast.getText().toString());   
// run commandto add new record                 
    comm.executeUpdate();
} catch (SQLException e) {
    tv.setText(e.toString());
}

Conclusion

Sometimes, we need to protect our database server, only access in local network. For more details about jTDS, see: http://jtds.sourceforge.net/.

History

  • Initial version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Tae Kwang Can Tho LTD
Vietnam Vietnam
Code for fun -> ^.^ <-

Comments and Discussions

 
Questionwhat is the IDE used? Pin
adam syria21-Apr-17 12:25
adam syria21-Apr-17 12:25 
AnswerRe: what is the IDE used? Pin
Ngo Tuong Dan22-Apr-17 0:23
professionalNgo Tuong Dan22-Apr-17 0:23 
GeneralRe: what is the IDE used? Pin
adam syria22-Apr-17 6:48
adam syria22-Apr-17 6:48 
GeneralRe: what is the IDE used? Pin
adam syria26-Apr-17 14:13
adam syria26-Apr-17 14:13 
GeneralRe: what is the IDE used? Pin
Ngo Tuong Dan28-Apr-17 0:13
professionalNgo Tuong Dan28-Apr-17 0:13 
GeneralRe: what is the IDE used? Pin
adam syria9-May-17 13:31
adam syria9-May-17 13:31 
GeneralMessage Closed Pin
9-May-17 13:31
adam syria9-May-17 13:31 
GeneralRe: what is the IDE used? Pin
Ngo Tuong Dan9-May-17 17:57
professionalNgo Tuong Dan9-May-17 17:57 
GeneralRe: what is the IDE used? Pin
adam syria10-May-17 15:40
adam syria10-May-17 15:40 
GeneralRe: what is the IDE used? Pin
Ngo Tuong Dan10-May-17 23:11
professionalNgo Tuong Dan10-May-17 23:11 
GeneralRe: what is the IDE used? Pin
adam syria12-May-17 22:42
adam syria12-May-17 22:42 
GeneralRe: what is the IDE used? Pin
adam syria16-May-17 11:19
adam syria16-May-17 11:19 
Questionandroid connectivity to remote sql server Pin
techSharp6-Jun-16 21:38
techSharp6-Jun-16 21:38 
AnswerRe: android connectivity to remote sql server Pin
Ngo Tuong Dan8-Jun-16 1:38
professionalNgo Tuong Dan8-Jun-16 1:38 
GeneralRe: android connectivity to remote sql server Pin
techSharp8-Jun-16 7:17
techSharp8-Jun-16 7:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.