连接MySQL Eclipse错误

问题描述:

我正在尝试将我的Android项目连接到mysql中的数据库,这是我的代码:

I'm trying to connect my android project to a database in mysql, it's my code:

package br.com.savetime;

import java.sql。*;

import java.sql.*;

public class CriarConexao {

public class CriarConexao {

private static Connection con = null;

public static Connection abrirBanco(){
    Connection con;
    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();  
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/savetime", "root", "root");
        return con;

    }
    catch(ClassNotFoundException cnfe){
        System.out.println("driver nao encontrado: " + cnfe.getMessage());
        return null;
    }
    catch(SQLException sql){
        System.out.println("SQLException: " + sql.getMessage());
        return null;
    }
    catch(Exception e){
        System.out.println(e.getMessage());
        return null;
    }
}

public static void fecharBDcon(){
    try{
        con.close();
    }
    catch(Exception e){
        System.out.println("erro ao fechar o banco" + e.getMessage());
    }
}   

}

这是错误:

06-18 02:32:57.925: I/System.out(1044): SQLException: Communications link failure due to underlying exception: 
06-18 02:32:57.925: I/System.out(1044): ** BEGIN NESTED EXCEPTION ** 
06-18 02:32:57.925: I/System.out(1044): android.os.NetworkOnMainThreadException
06-18 02:32:57.925: I/System.out(1044): STACKTRACE:
06-18 02:32:57.935: I/System.out(1044): android.os.NetworkOnMainThreadException
06-18 02:32:57.935: I/System.out(1044):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-18 02:32:57.935: I/System.out(1044):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-18 02:32:57.935: I/System.out(1044):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-18 02:32:57.935: I/System.out(1044):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-18 02:32:57.935: I/System.out(1044):     at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:133)
06-18 02:32:57.935: I/System.out(1044):     at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)
06-18 02:32:57.935: I/System.out(1044):     at com.mysql.jdbc.Connection.createNewIO(Connection.java:1765)
06-18 02:32:57.935: I/System.out(1044):     at com.mysql.jdbc.Connection.<init>(Connection.java:430)
06-18 02:32:57.935: I/System.out(1044):     at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)

我不是互联网,所以我不明白为什么会发生这个错误,可以你帮助我吗?

I'm not conneting to internet so I don't understand why it's occurring this error, could you help me please?

您正在尝试打开一个与您的应用程序本地的数据库的连接。您正在进行的连接是通过网络和Android(Honeycomb和转发)不允许您在UI线程上进行网络呼叫。使用 AsyncTask 或其他一些构造来执行网络操作主(UI)线程。

You're trying to open a connection to a database that is not local to your application. The connection you are making is via a network and Android (Honeycomb and forward) does not allow you to make network calls on the UI thread. Use an AsyncTask or some other construct to do your network operations off the main (UI) thread.

以下是使用AsyncTask连接的示例到MySQL:

Here's an example on how to use AsyncTask to connect to MySQL:

private class Connect extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
      String response = "";

      try {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con = DriverManager.getConnection(url, user, pass);
          // System.out.println("Database connection success"); 

          String result = "Database connection success\n";
          Statement st = con.createStatement();
          ResultSet rs = st.executeQuery("select * from users");
          ResultSetMetaData rsmd = rs.getMetaData();

          while(rs.next()) {
            result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
            result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
            result += rsmd.getColumnName(3) + ": " + rs.getString(3) + "\n";
          }
          tv.setText(result);
      }
      catch(Exception e) {
          e.printStackTrace();
          tv.setText(e.toString());
      }
    return response;   


    }

代码来自此处