Android Database Programming: A detailed example of a MySQL database on a connection server

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.