如何修复网络错误IOException:尝试连接到Google Cloud MySQL实例时无法在android studio上连接

时间:2019-05-15 01:46:47

标签: java android jdbc

我对数据库很陌生,并将它们连接到Java。我已经用Google Cloud设置了一个mysql数据库的实例,但继续出现网络错误IOException:每当我尝试使用提供的公共IP连接到该数据库时,都无法连接。

我的主要活动代码


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class MainActivity extends AppCompatActivity {


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

        List<Map<String,String>> MyData = null;
        GetData mydata =new GetData();
        MyData= mydata.doInBackground();
        String[] fromwhere = { "ID","Country"};
        System.out.println("JUSTS TYAFYEYFHYUESYFGESYUGYEGYFGY H HEYF YF YH YU" + fromwhere);
    }

我的连接器代码

package com.example.tester;

import android.annotation.SuppressLint;
import android.os.StrictMode;
import android.util.Log;

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


public class ConnectionHelper {

    String ip,db,DBUserNameStr,DBPasswordStr;


    @SuppressLint("NewApi")
    public Connection connectionclasss()
    {

        // Declaring Server ip, username, database name and password
        ip = "";
        db = "";
        DBUserNameStr = "";
        DBPasswordStr = "";
        // Declaring Server ip, username, database name and password


        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        java.sql.Connection connection = null;
        String ConnectionURL = null;
        try
        {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            ConnectionURL = "jdbc:jtds:sqlserver://" + ip +";databaseName="+ db + ";user=" + DBUserNameStr+ ";password=" + DBPasswordStr + ";";
            connection = DriverManager.getConnection(ConnectionURL);
        }
        catch (SQLException se)
        {
            Log.e("error here 1 : ", se.getMessage());
        }
        catch (ClassNotFoundException e)
        {
            Log.e("error here 2 : ", e.getMessage());
        }
        catch (Exception e)
        {
            Log.e("error here 3 : ", e.getMessage());
        }
        return connection;
    }
}

我的数据提取器代码

package com.example.tester;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.Connection;


public class GetData {

        Connection connect;
        String ConnectionResult = "";
        Boolean isSuccess = false;

        public List<Map<String,String>> doInBackground() {

            List<Map<String, String>> data = null;
            data = new ArrayList<Map<String, String>>();
            try
            {
                ConnectionHelper conStr=new ConnectionHelper();
                connect =conStr.connectionclasss();        // Connect to database
                if (connect == null)
                {
                    ConnectionResult = "Check Your Internet Access!";
                }
                else
                {
                    // Change below query according to your own database.
                    String query = "SELECT * from users LIMIT 1";
                    Statement stmt = connect.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    while (rs.next()){
                        Map<String,String> datanum=new HashMap<String,String>();
                        datanum.put("ID",rs.getString("first_name"));
                        datanum.put("Country",rs.getString("last_name"));
                        data.add(datanum);
                    }


                    ConnectionResult = " successful";
                    isSuccess=true;
                    connect.close();
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
                ConnectionResult = ex.getMessage();
            }

            return data;
        }


    }


我希望会有一个系统输出,显示名字和姓氏的第一行。我收到错误网络错误IOException:连接失败

0 个答案:

没有答案