我在这里有关于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
我觉得有什么不对劲的库,是真的吗? 但我tryied这对bluestack模拟器,与Android姜饼我想和连接工作好...是否有人知道我该怎么办?