java.sql.SQLException:网络错误IOException:无法连接ETIMEDOUT

时间:2017-03-28 05:36:35

标签: java android sql-server database-connection connection-timeout

我正在开发一个Android应用程序,我必须将android应用程序与SQL服务器数据库连接,以便用他们的用户名和密码对用户进行身份验证。我使用了jtds库以便为android提供Sql连接.Below是我的代码:

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    EditText username,password;
    CheckBox mCbShowPwd;
    Connection con;
    String un,pass,db,ip,in;
    TextView t;

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

        username = (EditText) findViewById(R.id.username_edtext);
        password = (EditText) findViewById(R.id.passwd_edtext);
        mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);
        t = (TextView) findViewById(R.id.text);
        final Button forget_btn = (Button) findViewById(R.id.forget_btn);
        forget_btn.setOnClickListener(this);
        final Button login_btn = (Button) findViewById(R.id.login_button);
        login_btn.setOnClickListener(this);
        ip = "172.16.0.**";
        in="********";
        db = "*******";
        un = "****";
        pass = "****";
        mCbShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // checkbox status is changed from uncheck to checked.
                if (!isChecked) {
                    password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());}
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.forget_btn:
            {
                Intent intent=new Intent("com.example.pc.uowv1.ForgetPassActivity");
                startActivity(intent);
                break;
            }
            case R.id.login_button:
            {
                CheckLogin checkLogin = new CheckLogin();// this is the Asynctask, which is used to process in background to reduce load on app process
                checkLogin.execute("");
                break;
            }
        }
    }
    public class CheckLogin extends AsyncTask<String,String,String>
    {
        String z = "";
        Boolean isSuccess = false;
        String usernam = username.getText().toString();
        String passwordd = password.getText().toString();

        @Override
        protected void onPreExecute()
        {
            //progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(String r)
        {
            Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
            if(isSuccess)
            {
                Toast.makeText(MainActivity.this , "Login Successfull" , Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        protected String doInBackground(String... params)
        {
            if(usernam.trim().equals("")|| passwordd.trim().equals(""))
                z = "Please enter Username and Password";
            else
            {
                try
                {
                    con = connectionclass(un, pass, db, ip,in);
                    if (con == null)
                    {
                        z = "Check Your Internet Access!";
                    }
                    else
                    {
                        String query = "select * from Login where username= '" + usernam.toString() +"'and password='"+passwordd.toString()+"'";
                        Statement stmt = con.createStatement();
                        ResultSet rs = stmt.executeQuery(query);
                        if(rs.next())
                        {
                            //mApp.setmGuser(usernam);
                            z = "Login successful";
                            isSuccess=true;
                            con.close();
                            Intent intent=new Intent(MainActivity.this,MAin2Activity.class);
                            intent.putExtra("username",usernam.toString());
                            startActivity(intent);
                        }
                        else
                        {
                            z = "Invalid Username and password!";
                            isSuccess = false;
                        }
                    }
                }
                catch (Exception ex)
                {
                    isSuccess = false;
                    z = ex.getMessage();
                }
            }
            return z;
        }
    }
    @SuppressLint("NewApi")
    public Connection connectionclass(String user, String password, String database, String server,String instance)
    {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Connection connection = null;
        String ConnectionURL = null;
        try
        {

            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            ConnectionURL = "jdbc:jtds:sqlserver://" + server + "/"+ database+";instance=" + instance + ";user=" + user+ ";password=" + password + ";";
            connection = DriverManager.getConnection(ConnectionURL);
        }
        catch (SQLException se)
        {
            Log.e("error here 1 : ", se.getMessage());
            t.setText(se.getMessage());
        }
        catch (ClassNotFoundException e)
        {
            Log.e("error here 2 : ", e.getMessage());
            t.setText(e.getMessage());
        }
        catch (Exception e)
        {
            Log.e("error here 3 : ", e.getMessage());
            t.setText(e.getMessage());
        }
        return connection;
    }
}

代码在同一网络上工作,但当我在我的Android设备中连接到其他网络时,它会抛出

Exception E/error here 1 :: Network error IOException: failed to 
connect to /172.16.0.** (port 1433): connect failed: ETIMEDOUT 
(Connection timed out).

1 个答案:

答案 0 :(得分:0)

IMНO你选错了方法。不要使用Android的SQL服务器连接。有必要在SQL服务器端创建Web服务并使用Android中的此Web服务。 JDBC驱动程序不适用于移动网络。