如何将android应用程序连接到sql server [英] how to connect android application to sql server

查看:128
本文介绍了如何将android应用程序连接到sql server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个 Sql数据库 ....我已经开发了一个简单的 android应用程序注册和登录 .....现在我希望我的应用程序访问从sql server 的详细信息到登录.....当一个用户尝试注册 数据必须保存在Sql Server .....所以他们的任何方式我可以访问/存储数据库到了sql .....?

我已经知道我想要使用某些Web服务,但我没有一个很好的例子对它......

所以,如果你有人/找到任何好的例子,我可以得到它.......



谢谢...... 。

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

推荐答案

您需要一个用于Microsoft SQL Server的JDBC驱动程序。



请尝试 http://jtds.sourceforge.net/ [ ^ ]



你可以做这样的事情



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());
        }
}


在build.gradle文件中添加你的jtds。在dependecy部分添加编译'net.sourceforge.jtds:jtds:1.3.1'



然后重建。 Gradle会将您的库下载并保存到项目内的正确位置。
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.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();
            }
    }
}


这篇关于如何将android应用程序连接到sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆