java - 连接被拒绝错误显示 Android studio 与 Sql Server 连接
问题描述
我正在使用带有 Sqlserver 的 Android Studio 创建一个简单的 CRUD。当我将数据添加到数据库中时,出现错误 ConnectionRefusedError。我已将代码附在我到目前为止尝试过的代码下方。我已经尝试连接数据库两天了,但它没有让我。
我成功添加了jar文件jtds-1.2.7.jar
我得到了 SQL 服务器的 IP 地址及其端口
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View"
/>
<EditText
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
/>
<EditText
android:id="@+id/course"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine" />
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id="@+id/v1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View" />
</LinearLayout>
爪哇
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
EditText ed1,ed2;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = findViewById(R.id.title);
ed2 = findViewById(R.id.course);
b1 = findViewById(R.id.btn1);
b2 = findViewById(R.id.v1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insert();
}
});
}
public void insert() {
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String connectionUrl = "jdbc:jtds:sqlserver://192.168.84.2; databaseName=skill; port =0; user=sa; password=admin123";
Connection con = DriverManager.getConnection(connectionUrl);
PreparedStatement pst;
String title = ed1.getText().toString();
String description = ed2.getText().toString();
pst = con.prepareStatement("insert into course(coursename,fee)values(?,?)");
pst.setString(1, title);
pst.setString(2, description);
pst.executeUpdate();
Toast.makeText(this, "Success", Toast.LENGTH_LONG).show();
ed1.setText("");
ed2.setText("");
ed1.requestFocus();
} catch (Exception ex) {
Toast.makeText(this, "Record Fail", Toast.LENGTH_LONG).show();
}
}
解决方案
该ConnectionRefused
错误仅表示主机拒绝该端口上的连接。也许该端口在主机的防火墙上被阻止?如果您有权访问主机,则可以简单地取消阻止该端口。请报告您的发现。
推荐阅读
- winforms - 等同于 Windows 窗体和 Access 窗体中的 Microsoft Forms 2.0 ComboBox .TopIndex 属性?
- python - 创建新列时在 Dask 中高效使用 If Then 场景
- c# - 当属性值为 () 时,如何将列表中的属性值更改为一个值?
- php - 如何使用下拉列表中的 ajax codeigniter 根据包含在第二个表中的第一个表 id 获取数据
- arrays - 计算包含数字的数组元素的数量
- c# - 如何将动态 JSON 转换为 C# 对象
- tensorflow - Unet Runtime Execution 停止并显示警告消息
- r - 尝试安装 ICEWS R 包时出现“非零退出状态”错误
- angular - Ionic 4 如何使用 aws cognito 会话登录 facebook?
- c - 使用 SDL_image 保存 PNG 时出现段错误