我正在尝试将我的android项目连接到mysql中的数据库,这是我的代码:
package br.com.savetime;
import java.sql。*;
公共类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)
我没有上网,所以我不明白为什么会发生这个错误,你能帮帮我吗?
答案 0 :(得分:0)
您尝试打开与应用程序不是本地数据库的连接。您正在通过网络建立连接,而Android(Honeycomb和forward)不允许您在UI线程上进行网络调用。使用AsyncTask或其他一些构造在主(UI)线程上执行网络操作。
<强> [编辑] 强>
以下是有关如何使用AsyncTask连接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。代码来自here。
答案 1 :(得分:0)
首先是在android localhost中指的是它自己的循环返回地址,如果你试图连接到系统上的mysql,从模拟器使用10.0.0.2 ip连接到系统。对于设备,您必须使用USB网络共享,并且设备和系统都需要在同一网络上。 其次使用ASYNC任务进行通信,因为这将是网络呼叫。