连接MySQL的Eclipse的错误

问题描述:

我想我的Andr​​oid项目连接到在MySQL数据库中,这是我的code:

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

package br.com.savetime;

导入的java.sql。*;

import java.sql.*;

公共类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)

我不conneting互联网,所以我不明白为什么它的出现这个错误,你能帮助我吗?

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

您正在尝试打开是不是本地的应用程序与数据库的连接。你正在连接是通过网络和Android(蜂窝和远期)不允许你做出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;   


    }

P.S。在code是采取从here.