1. Disable the mysql server firewall.
2. Add the jdbc package of mysql to the libs folder.
3. the ADT and SDK versions are 16 and less than 16, and those above 17 cannot be accessed.
4. The mobile phone and mysq server l can ping each other.
5. Add network permissions:
6. Connect to mysql in Thread to prevent ANR.
7. Set mysql to allow remote connection.
My code:
Package com. mayi. mysql_test; import java. SQL. driverManager; import java. SQL. resultSet; import com. mysql. jdbc. connection; import com. mysql. jdbc. statement; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. view; import android. view. view. onClickListener; public class MainActivity extends Activity {private static final String TAG = "MainActivity"; @ Overrideprotect Ed void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViewById (R. id. select ). setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {select () ;}});} public void select () {try {// call Class. the forName () method loads the driver Class. forName ("org. gjt. mm. mysql. driver "); System. out. println ("MySQL driver loaded successfully! "); String url =" jdbc: mysql: // 192.168.1.113: 3306/test "; // jdbc url Connection conn; conn = (Connection) DriverManager. getConnection (url, "mayi", "123456"); Statement stmt = (Statement) conn. createStatement (); // create the Statement object System. out. println ("successfully connected to the database! "); String SQL =" select * from person "; // The SQL ResultSet rs = stmt.exe cuteQuery (SQL) to be executed; // create the Data Object System. out. println ("Number" + "\ t" + "name" + "\ t" + "Age"); while (rs. next () {System. out. print (rs. getInt (1) + "\ t"); System. out. print (rs. getString (2) + "\ t"); System. out. print (rs. getString (3) + "\ t"); System. out. print (rs. getString (4) + "\ t"); System. out. println ();} rs. close (); stmt. close (); conn. close ();} catch (Exception e) {Log. e (TAG, e. getMessage (); e. printStackTrace ();}}}