Click here to Skip to main content
15,030,954 members
Please Sign up or sign in to vote.
4.75/5 (3 votes)
Hi Everyone,

I have an Sql database.... and i have developed an simple android app for Register and Login..... Now i want my app to access the details from sql server to login..... And when an user try to register the data has to be saved in Sql Server..... So is their any Way i can Access/Store the database in to the sql.....?
As i have goggled i got to know that i want to use Some Web service but i have not got an Good example For it......
So if u people have/found any good example for it can i get it.....!

Updated 23-Sep-18 22:03pm

You need a JDBC driver for Microsoft SQL Server.

Please try[^]

You can do something like this

public void ConnectToDatabase(){
    try {

         String username = "XXXXXXXXX";
            String password = "XXXXXX";
         Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://;user=" + username + ";password=" + password);

        Statement stmt = DbConn.createStatement();
        ResultSet reset = stmt.executeQuery(" select * from users ");

         EditText num = (EditText) findViewById(;


        } catch (Exception e)
        Log.w("Error connection","" + e.getMessage());
coderTOcode 20-Oct-13 10:35am
Thanks For the Solution....
I have Downloaded an driver from the Link....
But the problem is I am using Android Studio for Developing The application....
So i am not getting how to import this driver in This android Studio....
Member 10454028 10-Jun-14 1:19am
kedamin i se phaku be tomini reko jajaan
akira32 24-Jul-16 21:27pm
I had added the network permissions and add jtds-1.3.1.jar to the libs driectory.

uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"

But when I getConnection, it appears the exception message(android.os.NetworkOnMainThreadException) at

DriveManager.getConnection. Does somebody know what I lose to set?

protected void onCreate(Bundle savedInstanceState) {

private void findViews()

try {
con = DriverManager.getConnection(URL, USER, PASSWORD);
catch (Exception e)
Log.w("Error connection","" + e.getMessage());

Member 12100925 11-Jan-17 2:55am
is it possible to connect?!!
im trying for many days but i still cant connect!
i just get error for above code!
Add your jtds on build.gradle file. At dependecy section add compile 'net.sourceforge.jtds:jtds:1.3.1'

An then rebuild. Gradle will download and save your library to the correct location inside your project.
package com.instinctcoder.sqlitedb;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class StudentDetail extends Activity implements android.view.View.OnClickListener{

    Button btnSave ,  btnDelete;
    Button btnClose;
    EditText editTextName;
    EditText editTextEmail;
    EditText editTextAge;
    private int _Student_Id=0;
    protected void onCreate(Bundle savedInstanceState) {
        btnSave = (Button) findViewById(;
        btnDelete = (Button) findViewById(;
        btnClose = (Button) findViewById(;
        editTextName = (EditText) findViewById(;
        editTextEmail = (EditText) findViewById(;
        editTextAge = (EditText) findViewById(;
        _Student_Id =0;
        Intent intent = getIntent();
        _Student_Id =intent.getIntExtra("student_Id", 0);
        StudentRepo repo = new StudentRepo(this);
        Student student = new Student();
        student = repo.getStudentById(_Student_Id);

    public void onClick(View view) {
        // TODO Auto-generated method stub
         if (view == findViewById({
                StudentRepo repo = new StudentRepo(this);
                Student student = new Student();
                student.age= Integer.parseInt(editTextAge.getText().toString());
                if (_Student_Id==0){
                   _Student_Id = repo.insert(student);
                    Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show();
                    Toast.makeText(this,"Student Record updated",Toast.LENGTH_SHORT).show();
            }else if (view== findViewById({
                StudentRepo repo = new StudentRepo(this);
                Toast.makeText(this, "Student Record Deleted", Toast.LENGTH_SHORT);
            }else if (view== findViewById({
Try something like below, I'm not 100% sure. You have to tell me as I never used Android Studio

Steps to follow in order to import a JAR sucesfully to your project using Android Studio:

Download the library.jar file and copy it to your /libs/ folder inside your application project.
Open the build.gradle file and edit your dependencies to include the new .jar file:
compile files('libs/jtds.jar')

File -> Close Project
Open a command prompt on your project's root location, i.e 'C:\Users\Username\AndroidStudioProjects\MyApplicationProject\'
On the command prompt, type gradlew clean, wait till it's done.
Reopen your application project in Android Studio.
Test run your application and it should work successfully.
Ajinkya Badve 9-Jun-16 5:23am
Its works with android perfectly but giving error on jar file telling something that are some inner class which are used in the lib.But if you ignore them it will work fine.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900