简体   繁体   中英

android mysql with java code not responding

I have installed mysql in my computer.and also have jetty.. My requirement:- I have a database student with table name ari; ari table is:-

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

from My Android program I want to fetch it from database..I have done this:- 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();
        }   
    }

But its showing the error:--

 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)

I have the mysql connector.jar but still where is the problem???

I don't believe you can update the view inside of the AsyncTask. Try this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM