且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将android应用程序连接到sql server

更新时间:2022-02-07 09:57:22

您需要一个用于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();
            }
    }
}