我正在尝试使用 jdbc 驱动程序将我的 android 应用程序与 mysql 数据库连接起来。查了很多,发现使用thread或者AsyncTask可以直接连接mysql。但是我发现异步任务很困难,所以我使用线程来做到这一点。这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_customer);
TextView t = (TextView) findViewById(R.id.Search_Customer_textView1);
Button b = (Button) findViewById(R.id.Search_Customer_button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread timer=new Thread(){
public void run(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://192.168.1.102:3306/outsidelaundry", "root", "");
Statement stmt = con.createStatement();
String query = "select Name from nonmember";
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
val = val + rs.getString(1) + "\n";
}
Toast.makeText(getApplicationContext(), val, Toast.LENGTH_LONG).show();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
};
timer.start();
Toast.makeText(getApplicationContext(), "pressed", Toast.LENGTH_LONG).show();
}
});
}
当我运行程序线程没有启动/运行。我没有得到任何异常。请帮我解决一下这个。