首页 > 解决方案 > 连接被拒绝错误显示 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();
            }
        }

标签: javaandroid

解决方案


ConnectionRefused错误仅表示主机拒绝该端口上的连接。也许该端口在主机的防火墙上被阻止?如果您有权访问主机,则可以简单地取消阻止该端口。请报告您的发现。


推荐阅读