android - 使用Java将android studio连接到azure sql数据库

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

 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;DatabaseName=Newfin;user=;password= ;加密=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。我已经在 list 文件中添加了所有权限,还在项目中添加了 jtds 模块 1.3.1 并在 gradle 模块应用程序中实现了它。我的代码退出时出现 0 错误,但数据未显示在模拟器上。预期输出是我的数据库中的第一个值“8”。 谢谢。,

最佳答案

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

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

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

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

关于android - 使用Java将android studio连接到azure sql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61382492/

相关文章:

Android 找出对话框何时显示在屏幕上

json - 如何在 Azure Data Lake Analytics 上下文中分别使用 JSON 文件格式 usql

android - 在 Android Studio/IntelliJ 中方便地打开 SDK 源

android - CardView 在 API 19 上消失

SQLite 安卓 NDK

android - 移动设备不支持应用程序

azure - 链接到 Office 365 帐户

html - 带有 HTML5 视频的 Azure AD

尽管我删除了存储库,但 Android Studio 项目说项目已经在 GitHub 上了。

java - 检测可点击项目上的 throw 手势