MySQL连接 - java.sql.SQLException:由于异常,无法连接到任何主机

时间:2015-12-31 14:31:32

标签: java android mysql jdbc

我遇到了问题,但我无法找到答案。

我尝试使用JDBC加载MySQL-Connection。 Lib是导入的。这是我的代码。

import android.widget.Toast;

import java.util.ArrayList;
import java.sql.*;
import java.util.Calendar;

/**
 * Created by Henrik on 27.12.2015.
 */
public class MySQLHandler {

public static final String server = "aintlarry.lima-db.de";
public static final String port = "3306";
public static final String username = "**********";
public static final String password = "********";
public static final String dbName = "db_338853_1";

static Connection connection = null;

MainActivity Ma;

public static void main(String[] args){
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();;
    }
    catch (Exception e) {
    }
}

public String makeTableName(){

    String tableName = "";

    Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    tableName = "" + year + month + day;

    return tableName;

}

public void startNewEvent(ArrayList<Player> players) {

    String tableName = makeTableName();

    try {
        String url = "jdbc:mysql://"+server+":"+port+"/"+dbName;
        connection = DriverManager.getConnection(url, username, password);

        if(doTableExists(tableName) == false){

            String hName = "Henrik";
            int hMoney = 0;
            int hTip = 0;

            Statement statementH = connection.createStatement();
            String sqlCommand = "CREATE TABLE '" + tableName + "'";
            statementH.executeUpdate(sqlCommand);

            for (Player p : players) {
                String pName = p.getName();
                int pMoney = p.getMoney();
                int pTip = p.getTip();
            }

            connection.close();

        }else {
            Toast.makeText(Ma.getApplicationContext(), "Der heutige Wettbewerb wurde bereits erstellt!", Toast.LENGTH_LONG).show();
        }
    }

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

}

public boolean doTableExists(String tbName){
    boolean b = false;

    try {
        String url = "jdbc:mysql://"+server+":"+port+"/"+dbName;
        connection = DriverManager.getConnection(url, username, password);

        Statement statement = connection.createStatement();
        String sqlCommand = "SHOW TABLES LIKE '" + tbName + "'";
        ResultSet rs = statement.executeQuery(sqlCommand);

        if(rs.getRow() == 0){
            b = false;
        }else{
            b = true;
        }

        connection.close();

    } catch (SQLException sqle) {

    }

    return b;
}

}

日志:

12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err: java.sql.SQLException: Unable to connect to any hosts due to exception: android.os.NetworkOnMainThreadException
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err: ** BEGIN NESTED EXCEPTION ** 
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err: android.os.NetworkOnMainThreadException
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err: STACKTRACE:
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err: android.os.NetworkOnMainThreadException
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:125)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at com.mysql.jdbc.Connection.<init>(Connection.java:452)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:179)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:213)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at de.aintlarry.patsch.MySQLHandler.startNewEvent(MySQLHandler.java:55)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at de.aintlarry.patsch.AddEventActivity.startEvent(AddEventActivity.java:86)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at de.aintlarry.patsch.AddEventActivity$2.onClick(AddEventActivity.java:59)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at android.view.View.performClick(View.java:5226)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at android.view.View$PerformClick.run(View.java:21266)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at android.os.Looper.loop(Looper.java:168)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5781)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err: ** END NESTED EXCEPTION **
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at com.mysql.jdbc.Connection.createNewIO(Connection.java:1875)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at com.mysql.jdbc.Connection.<init>(Connection.java:452)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:179)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:213)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at de.aintlarry.patsch.MySQLHandler.startNewEvent(MySQLHandler.java:55)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at de.aintlarry.patsch.AddEventActivity.startEvent(AddEventActivity.java:86)
12-31 15:06:48.524 32314-32314/de.aintlarry.patsch W/System.err:     at de.aintlarry.patsch.AddEventActivity$2.onClick(AddEventActivity.java:59)
12-31 15:06:48.525 32314-32314/de.aintlarry.patsch W/System.err:     at android.view.View.performClick(View.java:5226)
12-31 15:06:48.525 32314-32314/de.aintlarry.patsch W/System.err:     at android.view.View$PerformClick.run(View.java:21266)
12-31 15:06:48.525 32314-32314/de.aintlarry.patsch W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
12-31 15:06:48.525 32314-32314/de.aintlarry.patsch W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
12-31 15:06:48.525 32314-32314/de.aintlarry.patsch W/System.err:     at android.os.Looper.loop(Looper.java:168)
12-31 15:06:48.525 32314-32314/de.aintlarry.patsch W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5781)
12-31 15:06:48.525 32314-32314/de.aintlarry.patsch W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
12-31 15:06:48.525 32314-32314/de.aintlarry.patsch W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
12-31 15:06:48.525 32314-32314/de.aintlarry.patsch W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687

我不知道我的错误在哪里!

登录数据是正确的,我有外部访问权限。

3 个答案:

答案 0 :(得分:1)

这很可能是因为您无法在主线程上执行任何网络任务。

尝试使用以下方式使调用异步: http://developer.android.com/reference/android/os/AsyncTask.html

或者,如果必须,请通过在onViewCreated方法或setContentView(R.layout.xxx)方法之后放置以下代码来避免此异常:

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

答案 1 :(得分:0)

如果您必须在主线程中进行网络操作,例如建立连接,则必须修改AndroidManifest.xml文件,即添加此配置:

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

但是,作为最佳实践,我建议您在主线程之外执行此类操作。尝试使用AsyncTask。

答案 2 :(得分:0)

此错误是由于网络问题造成的。:

  1. 在AndroidManifest文件中声明Internet权限。
  2. 加载您的连接驱动程序并在后台进程中获取连接,即您必须将代码放在AsyncTask类中。
  3. 代码:

    <uses-permission android:name="android.permission.INTERNET" />/*This line in manifest file*/
    
    public class SQLConnect extends AsyncTask<String,Void,String> {
    @Override
      protected String doInBackground(String... params) {
         Your Connection code goes hare ..!!
        return "";
    }
    }