Link android to MySQL database

Source: Internet
Author: User

Android can be connected to remote data, but your remote database service to Open remote, specific settings http://www.ways2u.com/knowledge? Posting = 142

Java programming steps for accessing a database using JDBC
Http://www.ways2u.com /? Post = 76

Link android to a MySQL database instance:
Package com. HL;

Import java. SQL. drivermanager;
Import java. SQL. resultset;

Import com. MySQL. JDBC. connection;
Import com. MySQL. JDBC. statement;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. textview;

Public class androidmsql extends activity {

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button BTN = (button) findviewbyid (R. Id. BTN );
BTN. setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view v ){
Sqlcon ();
}
});

}

Private void msettext (string Str ){
Textview txt((textview1_findviewbyid(r.id.txt );
TXT. settext (STR );
}

Private void sqlcon (){
Try {
Class. forname ("com. MySQL. JDBC. Driver ");
} Catch (exception e ){
E. printstacktrace ();
}
Try {
String url = "JDBC: mysql: /// 192.168.142.128: 3306/MySQL? User = zzfeihua & Password = 12345 & useunicode = true & characterencoding = UTF-8 "; // link database statement
Connection conn = (connection) drivermanager. getconnection (URL); // link to the database
Statement stmt = (statement) Conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
String SQL = "select * from user"; // query the user TABLE statement
Resultset rs1_stmt.exe cutequery (SQL); // execute the query
Stringbuilder STR = new stringbuilder ();
While (Rs. Next ()){
Str. append (Rs. getstring (1) + "/N ");
}
Msettext (Str. tostring ());

Rs. Close ();
Stmt. Close ();
Conn. Close ();

} Catch (exception e ){
E. printstacktrace ();
}

}

}

However, eclipse always prompts:
Warning: Ignoring innerclasses attribute for an anonymous inner class that doesn't come with an associated enclosingmethod attribute. (This class was probably produced by a broken compiler .)
I don't know who can solve it ??

Attachment download:
Androidmsql.rar 785.5kb

Related Article

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.