无法从Android Studio连接到远程SQL数据库

时间:2016-05-27 17:09:29

标签: android mysql sql database connect

我有一个远程Windows Server,我在SQL Workbench中有一个数据库。另一方面,我有一个需要连接到远程Windows Server数据库的Android应用程序,但问题是它没有连接,并且它没有给我任何错误。

我检查了远程服务器的端口3306是否已打开。我还应该尝试什么?

这是我的代码:

AsyncTask<String, Void, String> a = new AsyncTask<String, Void, String>() {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(String s) {
        Log.i("postExecute",s);
        super.onPostExecute(s);
    }

    @Override
    protected String doInBackground(String... params) {
        String response= "blank";
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String bdAddress = "jdbc:mysql://XXX.XXX.XXX.XXX:3306/dataBaseName";
            String bdUsername = "XXXXX";
            String bdPassword = "XXXXX";


            conn = DriverManager.getConnection(bdAddress, bdUsername, bdPassword);
            //THE CODE DOESN'T REACH THIS POINT AND IT DOESN'T GIVE ANY ERROR
            Statement stmt = null;
            try {
                conn.setAutoCommit(false);
                stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * FROM tableName");
                while (rs.next()) {
                    response= rs.getString("tableColumn");
                }
                conn.commit();


            } catch (SQLException ex) {
                ex.printStackTrace();
                conn.rollback();
            } finally {
                if (stmt != null) {
                    stmt.close();
                }
                conn.setAutoCommit(true);
            }


        } catch (SQLException sqlException) {
            sqlException.printStackTrace();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return response;
    }


};
a.execute(null, null, null);

非常感谢你!

0 个答案:

没有答案