首页 > 解决方案 > 为什么 Android 连接到 SQL 数据库不起作用

问题描述

我尝试使用 JDBC 连接器将我的 android 应用程序连接到 SQL DB。如果我在 Netbeans 中尝试,那就没问题并且连接正常。我正在使用连接器:mssql-jdbc-8.2.2.jre13

package sql_server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Sql_server {

    public static void main(String[] args) {
        String url = "jdbc:sqlserver://localhost\\MSSQLSERVER19;databaseName=Helios002";
        String user = "user";
        String password = "password";

        String query = "select top 1 skupzbo, regcis, nazev1 from Tabkmenzbozi order by id desc";

        try ( Connection con = DriverManager.getConnection(url, user, password);  Statement st = con.createStatement();  ResultSet rs = st.executeQuery(query)) {
            if (rs.next()) {
                System.out.println(rs.getString(1) + rs.getString(2) + rs.getString(3));
            }
        } catch (SQLException ex) {
            System.out.println("An error occurred while connecting MySQL databse");
            ex.printStackTrace();
        }
    }
}

但在 android studio 中它不起作用。我使用的是连接器:mssql-jdbc-8.2.2.jre11,因为 android studio 不支持 mssql-jdbc-8.2.2.jre13。

package com.example.hepopr.ui.gallery;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.hepopr.R;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

public class GalleryFragment extends Fragment {

    private TextView test;
    public static String url = "jdbc:sqlserver://localhost\\MSSQLSERVER19;databaseName=Helios002";
    public static final String user = "user";
    public static final String pass = "password";

    private GalleryViewModel galleryViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                ViewModelProviders.of( this ).get( GalleryViewModel.class );
        View root = inflater.inflate( R.layout.fragment_gallery, container, false );
        final TextView textView = root.findViewById( R.id.text_gallery );
        galleryViewModel.getText().observe( getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText( s );
            }
        } );

        test=root.findViewById( R.id.textView2 );


        return root;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );

        Timer timer = new Timer();
        //Set the schedule function
        timer.scheduleAtFixedRate( new TimerTask() {
            @Override
            public void run() {
                new MyTask().execute();

            }
        },0, 30000 );   // 1000 Millisecond  = 1 second

    }


    private class MyTask extends AsyncTask<Void, Void, Void> {
        private String STvody = "", STvzduchu = "", SVlhkost = "", SCerpadlo = "", SHladina = "", SSvetlo = "", SDate = "", STlak = "";

        @Override
        protected Void doInBackground(Void... arg0) {

            try {
                Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
                Connection con = DriverManager.getConnection( url, user,pass);
                if (con == null) {
                    Toast.makeText( getActivity(), "Nepřipojeno", Toast.LENGTH_SHORT ).show();
                } else {
                    Statement st = con.createStatement();
                    String sql = "select top 1 regcis from Tabkmenzbozi order by id desc ";

                    final ResultSet rs = st.executeQuery( sql );
                    if (rs == null) {
                        Toast.makeText( getActivity(), "Bez dat", Toast.LENGTH_SHORT ).show();
                    }
                    Objects.requireNonNull( rs ).next();
                    SDate = rs.getString( 1 );

                    String pattern = "HH:mm:ss";
                    DateFormat df = new SimpleDateFormat(pattern);
                    Date today = Calendar.getInstance().getTime();
                   // todayAsString = df.format( today );
                    con.close();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            test.setText( SDate );
            super.onPostExecute( result );
        }
    }
}

工作室返回错误:


W/System.err: com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, named instance mssqlserver19 failed. Error: "java.net.SocketTimeoutException: Poll timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.  For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:6068)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:2457)
W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2200)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2067)
        at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1204)
W/System.err:     at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:825)
        at java.sql.DriverManager.getConnection(DriverManager.java:569)
        at java.sql.DriverManager.getConnection(DriverManager.java:219)
W/System.err:     at com.example.hepopr.ui.gallery.GalleryFragment$MyTask.doInBackground(GalleryFragment.java:84)
        at com.example.hepopr.ui.gallery.GalleryFragment$MyTask.doInBackground(GalleryFragment.java:76)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)

你能帮我吗,有什么问题吗?谢谢你。

标签: androidsqlsql-server

解决方案


不要这样做。曾经。您的数据库密码在您的代码中,该代码将发送到设备。这意味着只要 30 分钟的努力,任何人都可以获取您的数据库密码并搞砸您的数据。您应该只通过 web 服务与远程数据库交互,因此只有 web 服务(在您的硬件上)拥有数据库的密钥。其他任何事情都令人难以置信的不安全。

JDBC 可以安全地用于服务器端,因为它只在您的服务器上运行。但它永远不应该在您无法物理控制的设备上使用。


推荐阅读