1 Public classDatabasedemo extends Activity {2 PrivateTextView TextView;3 4 @Override5 protected voidonCreate (Bundle savedinstancestate) {6 //TODO auto-generated Method Stub7 super.oncreate (savedinstancestate);8 Setcontentview (r.layout.activity_databasedemo1);9TextView = (TextView) This. Findviewbyid (R.id.mytextview);Ten } One A Public voidMybutton_onclick (view view) { - NewThread (runnable). Start (); - the } - -Handler Handler =NewHandler () { - @Override + Public voidhandlemessage (Message msg) { - super.handlemessage (msg); +Bundle bundle =Msg.getdata (); AString result = bundle.getstring ("result"); at Textview.settext (result); - }; - }; - - PrivateRunnable Runnable =NewRunnable () { - in @Override - Public voidrun () { to //TODO auto-generated Method Stub + Try { -String result =executesqlquery (); theMessage message =NewMessage (); *Bundle bundle =NewBundle (); $Bundle.putstring ("result", result);Panax Notoginseng Message.setdata (bundle); - handler.sendmessage (message); the}Catch(ClassNotFoundException e) { + //TODO auto-generated Catch block A e.printstacktrace (); the}Catch(SQLException e) { + //TODO auto-generated Catch block - e.printstacktrace (); $ } $ } - }; - the - /**Wuyi * Use JDBC in Android to connect to MySQL database on the server logcat possible exceptions: the * 1.Caused by:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:Could not create connection to database server; - * If you just throw the exception, the possible causes are as follows: Wu * (1) The user who connects to the MySQL database does not have remote host access rights; - * (2) Increase the number of connections to the database, in the Mysql.ini configuration document can be modified; About * (3) No database created; $ * If an exception is thrown at the same time as 2, refer to the 2 solution; - * 2.Caused by:android.os.NetworkOnMainThreadException; The reason for this exception is that HTTP requests are executed in the main thread of Android; - * The method is to use handler to manage the newly created thread and execute the HTTP request in the new thread; - * @return A * @throws SQLException + * @throws classnotfoundexception the */ - PublicString ExecuteSQLQuery () throws SQLException, ClassNotFoundException { $Stringstring=""; the //load the driver; (The virtual machine loads the class, not the one that is used during programming.) ) theClass.forName ("Com.mysql.jdbc.Driver"); the //establish database connection; theConnection Connection =Drivermanager.getconnection ( - "jdbc:mysql://10.20.48.157:3306/test","Test","[email protected]"); in //create the SQL statement, execute the statement, return the result set; theStatement Statement =connection.createstatement (); theResultSet ResultSet = Statement.executequery ("SELECT * from vote"); About the //processing result sets (including metadata); theResultSetMetaData Rsmetadata =Resultset.getmetadata (); the while(Resultset.next ()) { + for(inti =1; I <= rsmetadata.getcolumncount (); i++) { - string+ = resultset.getstring (i) +" "; the }Bayi string+="\ n"; the } the //close the database connection; (Connecting the database is a big overhead in Java applications.) ) - connection.close (); - return string; the } the}
Android Database Programming: A detailed example of a MySQL database on a connection server