我在这里遇到了另一个关于 android 的问题...我正在尝试使用该代码连接到我的 mysql 数据库:
public void testDB() {
TextView tv = (TextView)this.findViewById(R.id.textView1);
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());
}
}
在 AVD 中,我收到此错误:
10-23 16:49:08.103: W/System.err(12939): java.sql.SQLException: Unable to connect to any hosts due to exception: android.os.NetworkOnMainThreadException
我认为库中有问题,这是真的吗?但是我在bluestack模拟器上尝试了这个,我认为使用android姜饼并且连接效果很好......有人知道我该怎么做吗?