试图在Android Studio上连接到sql server"连接超时"

时间:2014-12-11 22:10:46

标签: android sql-server

我一直在尝试连接到SQL Server。这是代码

package com.servebeer.blooddonations.canadianblooddonations;

import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

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

public class MainActivity extends ActionBarActivity {
    Connection connect;
    public void initialize(){
        connect=CONN(<username is here>,<password is here>,"blooddatabase","70.75.191.26:3306");
    }
   @SuppressLint("NewApi")
    private Connection CONN(String user,String pass,String db,String server){
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection conn=null;
        String connUrl=null;

        try{
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            connUrl="jdbc:jtds:sqlserver://" + server + ";" + "databaseName=" + db + ";user=" + user + ";password=" + pass + ";";
            conn=DriverManager.getConnection(connUrl);
            Log.w("Connection","open");
        }catch(SQLException se){
            Log.e("ERROR", se.getMessage());
        }catch(ClassNotFoundException cl){
            Log.e("ERROR", cl.getMessage());
        }catch(Exception e){
            Log.e("ERROR", e.getMessage());
        }

        return conn;

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



}

但是每当我尝试模拟应用程序时,我都会收到此错误

Network error IOException: failed to connect to /70.75.191.26 (port 3306): connect failed: ETIMEDOUT (Connection timed out)

我已检查过正确的端口是否正在转发(3306)并尝试启用和禁用防火墙。我仍然得到同样的错误。我假设我的计算机上的某些东西停止了连接,但我花了几个小时尝试一切我能想到的没有运气。

这些权限在我的Manifest.xml中

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

这里它是一个异步任务,仍然超时。

 public void initialize(){
        //connect=CONN("root","","blooddatabase","70.75.191.26:3306");
        Connect();

    }
    public void Connect() {
        Connect task = new Connect();
        task.execute();
    }
        private class Connect extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            String connUrl=null;
            String server="70.75.191.26:3306";
            String db = "blooddatabase";
            String user="<username>";
            String pass="<password>";

            try {
                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                connUrl="jdbc:jtds:sqlserver://" + server + ";" + "databaseName=" + db + ";user=" + user + ";password=" + pass + ";";
                Log.w("Connection","Starting");
                Connection connect = DriverManager.getConnection(connUrl);
                Log.w("Connection","open");

            } catch (SQLException se) {
                Log.e("ERROR", se.getMessage());
            } catch (Exception e) {
                Log.e("ERROR", e.getMessage());
            }
            return response;
        }
            protected void onPostExecute(){
            }
        }

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用会话参数,有时数据库需要一个时间范围来操作连接。