Click here to Skip to main content
14,085,361 members
Rate this:
 
Please Sign up or sign in to vote.
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.....!

Thanks.....
Posted
Updated 23-Sep-18 22:03pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You need a JDBC driver for Microsoft SQL Server.

Please try http://jtds.sourceforge.net/[^]

You can do something like this

public void ConnectToDatabase(){
    try {

         // SET CONNECTIONSTRING
         Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
         String username = "XXXXXXXXX";
            String password = "XXXXXX";
         Connection DbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://192.188.4.83:1433/DATABASE;user=" + username + ";password=" + password);

         Log.w("Connection","open");
        Statement stmt = DbConn.createStatement();
        ResultSet reset = stmt.executeQuery(" select * from users ");


         EditText num = (EditText) findViewById(R.id.displaymessage);
        num.setText(reset.getString(1));

        DbConn.close();

        } catch (Exception e)
        {
        Log.w("Error connection","" + e.getMessage());
        }
}
   
Comments
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) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
}

private void findViews()
{

try {
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
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!
net.sourceforge.jtds.jdbc.Driver
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

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.
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

package com.instinctcoder.sqlitedb;

import android.app.Activity;
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;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_detail);
        
        btnSave = (Button) findViewById(R.id.btnSave);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnClose = (Button) findViewById(R.id.btnClose);
 
        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextAge = (EditText) findViewById(R.id.editTextAge);
 
        btnSave.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnClose.setOnClickListener(this);
 
 
        _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);
 
        editTextAge.setText(String.valueOf(student.age));
        editTextName.setText(student.name);
        editTextEmail.setText(student.email);
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
         if (view == findViewById(R.id.btnSave)){
                StudentRepo repo = new StudentRepo(this);
                Student student = new Student();
                student.age= Integer.parseInt(editTextAge.getText().toString());
                student.email=editTextEmail.getText().toString();
                student.name=editTextName.getText().toString();
                student.student_ID=_Student_Id;
     
                if (_Student_Id==0){
                   _Student_Id = repo.insert(student);
     
                    Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show();
                }else{
     
                    repo.update(student);
                    Toast.makeText(this,"Student Record updated",Toast.LENGTH_SHORT).show();
                }
            }else if (view== findViewById(R.id.btnDelete)){
                StudentRepo repo = new StudentRepo(this);
                repo.delete(_Student_Id);
                Toast.makeText(this, "Student Record Deleted", Toast.LENGTH_SHORT);
                finish();
            }else if (view== findViewById(R.id.btnClose)){
                finish();
            }
    }
}
   
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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.
   
Comments
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190518.1 | Last Updated 24 Sep 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100