【问题标题】:Connect android studio to azure sql database using Java使用 Java 将 android studio 连接到 azure sql 数据库
【发布时间】:2020-08-06 11:58:37
【问题描述】:
 package com.example.workdb;

 import androidx.appcompat.app.AppCompatActivity;

 import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.StrictMode;
  import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.os.Bundle;
import android.widget.TextView;

import org.w3c.dom.Text;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import net.sourceforge.jtds.jdbc.*;
  public class MainActivity extends AppCompatActivity {
   public Button run;
   public TextView message;
   public TextView txtvw;
   public Connection con;
  @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    run= (Button) findViewById(R.id.button);
    run.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)

        {


            CheckLogin checkLogin= new CheckLogin();
            checkLogin.execute("");
            Log.d("CREATION","ON CREATE WORKS");
            //  Log.d("txtvw", connection);
            //System.out.println("Yes");
            // txtvw.setText("hello");

        }

    });
  } public class CheckLogin extends AsyncTask<String,String,String>
  {
    String z="";
    Boolean IsSuccess= false;
    String name1="";


    // Log.d("txtvw","step 1 done");
    protected void onPostExecute(String r){
        if (IsSuccess){
            message=(TextView)findViewById(R.id.textView);
            message.setText(name1);
            Log.d("TAG", "STEP 1 DONE");

        }
    }
    @Override
    protected String doInBackground(String... strings) {
        try
        {
            Connection con = connectionClass();
            if(con==null){
                z="Check interent";
                //Log.d("txtvw", z);

            }
            else
            {
                String query= "select * from Value";
                Statement stmt= con.createStatement();
                ResultSet rs= stmt.executeQuery(query);
                if (rs.next())
                {
                    name1=rs.getString("KneeAngle");
                    Log.d("MYTAG", "name 1 works");
                    z="Query success";
                    IsSuccess=true;
                    con.close();


                }
                else{
                    z="Invalid query";
                    IsSuccess=false;
                }



            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return z;
    }
}
public Connection connectionClass(){
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection connection = null;
    String ConnectionURL ;

    try{
        Class.forName("net.sourceforge.jtds.jdbs.Driver");
        

  

ConnectionURL="jdbc:jtds:sqlserver://havvasemserv3.database.windows.net:1433;DatbaseName=Newfin;user=;password= ;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30";

        connection= DriverManager.getConnection(ConnectionURL);

    } catch(ClassNotFoundException e){
        Log.e("Error here 2 ",e.getMessage());

    }
    catch (Exception e) {
        Log.e("error here 3:",e.getMessage());

    }
    //Log.d("txtvw", connection);
    return connection;

}

}

我正在尝试将 azure sql 数据库连接到 android studio。我已经在清单文件中添加了所有权限,并且我还在项目中添加了一个 jtds 模块 1.3.1 并在 gradle 模块应用程序中实现了它。我的代码以 0 个错误退出,但数据未显示在模拟器上。预期输出是我数据库中的第一个值,即“8”。 谢谢。,

【问题讨论】:

    标签: android azure android-studio jdbc azure-sql-database


    【解决方案1】:

    在 timeout=30 后将其添加到当前连接 URL 的末尾 ;ssl=请求

    还要确保在您的 azure 服务器的防火墙设置中!/数据库您当前的设备 IP 被允许访问,因为默认情况下所有访问都被阻止,

    要允许所有设备访问服务器,请转到防火墙设置并在插入 ip 部分添加此 ip 范围

    0,0,0,0 和 255,255,255,255

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 2021-09-03
      • 1970-01-01
      相关资源
      最近更新 更多