android - 使用 Java 将 android studio 连接到 azure sql 数据库
问题描述
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”。谢谢。,
解决方案
在 timeout=30 ;ssl=request 后将其添加到当前连接 URL 的末尾
还要确保在您的天蓝色服务器的防火墙设置中!/数据库您当前的设备 IP 被允许访问,因为默认情况下所有访问都被阻止,
要允许所有设备访问服务器,请转到防火墙设置并在插入 ip 部分添加此 ip 范围
0,0,0,0 和 255,255,255,255
推荐阅读
- javascript - 出现错误“您可能需要适当的加载程序来处理此文件类型。” 使用 Webpack 编译 ES6 资产时
- mongodb - Springboot monodb 查询过滤数据计数
- wordpress - 未捕获的错误:在自定义插件中调用未定义的函数 wp_redirect()
- javascript - 访问数组属性的编译错误
- sql - Select * From Table Where Name in like(通配符)
- angular8 - (ngx-admin/Nebular) 如何将 Grid 系统与 nebular 集成?
- mongodb - Mongoose 从多个文档更新多个子文档
- java - 如何使用 WebView 为 onClickListener 设置线性布局
- intellij-idea - 如果已通过 JetBrains Toolbox 安装 IntelliJ IDEA,如何在 Windows 上下文菜单中提供“在 IntelliJ IDEA 中打开此项目”选项?
- gcloud - 无法下载谷歌凭据 json 文件