android mysql与java代码没有响应

时间:2014-11-22 11:53:40

标签: java android mysql

我已经在我的电脑上安装了mysql。还有jetty ..我的要求: - 我有一个表名为ari的数据库学生; ari表是: -

+----+-------+
| id | name  |
+----+-------+
| 22 | Sandy |
+----+-------+

来自我的Android程序我想从数据库中获取它..我已经这样做了: - MainActivity3.java

public class MainActivity3 extends Activity {
    private static final String url = "jdbc:mysql://localhost:3306/student";
    private static final String user = "root";
    private static final String pass = "root";
    TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.textView1);
        Connect();
    }

 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);
            String result = "Database connection success\n";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from ari");
            ResultSetMetaData rsmd = rs.getMetaData();
            while(rs.next()) {
                result += rsmd.getColumnName(1) + ": " + rs.getInt(1) + "\n";
                result += rsmd.getColumnName(2) + ": " + rs.getString(2) + "\n";
                        }
            tv.setText(result);
        }
        catch(Exception e) {
            e.printStackTrace();
            tv.setText(e.toString());
        }
        return response;   
        }

        @Override
        protected void onPostExecute(String result) {
            tv.setText(result);
        }
      }
      public void Connect() {
        Connect task = new Connect();
          task.execute();
        }   
    }

但它显示错误: -

 FATAL EXCEPTION: AsyncTask #1
 Process: com.TourBus.info, PID: 32102
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:300)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:841)
 Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6661)
    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:969)
at android.view.View.requestLayout(View.java:16767)
    at android.view.View.requestLayout(View.java:16767)
    at android.view.View.requestLayout(View.java:16767)
    at android.view.View.requestLayout(View.java:16767)
    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:369)
    at android.view.View.requestLayout(View.java:16767)
    at android.widget.TextView.checkForRelayout(TextView.java:6820)
    at android.widget.TextView.setText(TextView.java:3889)
    at android.widget.TextView.setText(TextView.java:3728)
    at android.widget.TextView.setText(TextView.java:3703)
    at com.TourBus.info.MainActivity3$Connect.doInBackground(MainActivity3.java:46)
    at com.TourBus.info.MainActivity3$Connect.doInBackground(MainActivity3.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)

我有mysql connector.jar,但问题仍在哪里???

1 个答案:

答案 0 :(得分:0)

我不相信你可以更新AsyncTask里面的视图。试试这个。

runOnUiThread(new Runnable() {  
 @Override
 public void run() {
   // setText() here
 }
});