MySQL: 基于 Android 远程连接

来源:互联网 发布:java webservice教程 编辑:程序博客网 时间:2024/04/28 05:54

MySQL: 基于 Android 远程连接


程序效果图




项目结构





项目文件中新建 libs 目录,将 jdbc 的 jar 文件放到里面。


这样 adt 插件自动将该 jar 添加到 build path.


数据库表里面的原始数据




插入数据




删除数据




更新数据





Main.Java


[java] view plain copy
 print?
  1. package mark.zhang;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.SQLException;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9.   
  10. public class Main extends Activity {  
  11.   
  12.     private static final String REMOTE_IP = "192.168.1.102";  
  13.     private static final String URL = "jdbc:mysql://" + REMOTE_IP + "/mydb";  
  14.     private static final String USER = "mark";  
  15.     private static final String PASSWORD = "123456";  
  16.   
  17.     private Connection conn;  
  18.   
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.     }  
  24.   
  25.     public void onConn(View view) {  
  26.         conn = Util.openConnection(URL, USER, PASSWORD);  
  27.     }  
  28.   
  29.     public void onInsert(View view) {  
  30.         String sql = "insert into mytable values(9, 'hanmeimei')";  
  31.         Util.execSQL(conn, sql);  
  32.     }  
  33.   
  34.     public void onDelete(View view) {  
  35.         String sql = "delete from mytable where name='mark'";  
  36.         Util.execSQL(conn, sql);  
  37.     }  
  38.   
  39.     public void onUpdate(View view) {  
  40.         String sql = "update mytable set name='lilei' where name='hanmeimei'";  
  41.         Util.execSQL(conn, sql);  
  42.     }  
  43.   
  44.     public void onQuery(View view) {  
  45.         System.out.println("All users info:");  
  46.         Util.query(conn, "select * from mytable");  
  47.     }  
  48.   
  49.     @Override  
  50.     protected void onDestroy() {  
  51.         super.onDestroy();  
  52.         if (conn != null) {  
  53.             try {  
  54.                 conn.close();  
  55.             } catch (SQLException e) {  
  56.                 conn = null;  
  57.             } finally {  
  58.                 conn = null;  
  59.             }  
  60.         }  
  61.     }  
  62. }  

Util.java

[java] view plain copy
 print?
  1. package mark.zhang;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.sql.Statement;  
  8.   
  9. public class Util {  
  10.   
  11.     public static Connection openConnection(String url, String user,  
  12.             String password) {  
  13.         Connection conn = null;  
  14.         try {  
  15.             final String DRIVER_NAME = "com.mysql.jdbc.Driver";  
  16.             Class.forName(DRIVER_NAME);  
  17.             conn = DriverManager.getConnection(url, user, password);  
  18.         } catch (ClassNotFoundException e) {  
  19.             conn = null;  
  20.         } catch (SQLException e) {  
  21.             conn = null;  
  22.         }  
  23.   
  24.         return conn;  
  25.     }  
  26.   
  27.     public static void query(Connection conn, String sql) {  
  28.   
  29.         if (conn == null) {  
  30.             return;  
  31.         }  
  32.   
  33.         Statement statement = null;  
  34.         ResultSet result = null;  
  35.   
  36.         try {  
  37.             statement = conn.createStatement();  
  38.             result = statement.executeQuery(sql);  
  39.             if (result != null && result.first()) {  
  40.                 int idColumnIndex = result.findColumn("id");  
  41.                 int nameColumnIndex = result.findColumn("name");  
  42.                 System.out.println("id\t\t" + "name");  
  43.                 while (!result.isAfterLast()) {  
  44.                     System.out.print(result.getString(idColumnIndex) + "\t\t");  
  45.                     System.out.println(result.getString(nameColumnIndex));  
  46.                     result.next();  
  47.                 }  
  48.             }  
  49.         } catch (SQLException e) {  
  50.             e.printStackTrace();  
  51.         } finally {  
  52.             try {  
  53.                 if (result != null) {  
  54.                     result.close();  
  55.                     result = null;  
  56.                 }  
  57.                 if (statement != null) {  
  58.                     statement.close();  
  59.                     statement = null;  
  60.                 }  
  61.   
  62.             } catch (SQLException sqle) {  
  63.   
  64.             }  
  65.         }  
  66.     }  
  67.   
  68.     public static boolean execSQL(Connection conn, String sql) {  
  69.         boolean execResult = false;  
  70.         if (conn == null) {  
  71.             return execResult;  
  72.         }  
  73.   
  74.         Statement statement = null;  
  75.   
  76.         try {  
  77.             statement = conn.createStatement();  
  78.             if (statement != null) {  
  79.                 execResult = statement.execute(sql);  
  80.             }  
  81.         } catch (SQLException e) {  
  82.             execResult = false;  
  83.         }  
  84.   
  85.         return execResult;  
  86.     }  
  87. }  

main.xml

[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_marginBottom="10dip"  
  11.         android:onClick="onConn"  
  12.         android:text="connect mysql" />  
  13.   
  14.     <Button  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_marginBottom="10dip"  
  18.         android:onClick="onInsert"  
  19.         android:text="insert data" />  
  20.   
  21.     <Button  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_marginBottom="10dip"  
  25.         android:onClick="onDelete"  
  26.         android:text="delete data" />  
  27.   
  28.     <Button  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content"  
  31.         android:layout_marginBottom="10dip"  
  32.         android:onClick="onUpdate"  
  33.         android:text="update data" />  
  34.   
  35.     <Button  
  36.         android:layout_width="fill_parent"  
  37.         android:layout_height="wrap_content"  
  38.         android:onClick="onQuery"  
  39.         android:text="query data" />  
  40.   
  41. </LinearLayout>  


manifest.xml


[html] view plain copy
 print?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="mark.zhang"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="7" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:name=".Main"  
  14.             android:label="@string/app_name" >  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.       
  23.     <uses-permission android:name="android.permission.INTERNET"/>  
  24.   
  25. </manifest>  

注意:

 <uses-permission android:name="android.permission.INTERNET"/>


-----------------------------------------------------------------------------

2016.7.28 更新

-----------------------------------------------------------------------------

首先非常感谢@lyz12600对工程的整理.

大家可以去下载 AndroidStudio 的代码, 运行, 有问题多交流.

下载地址:http://download.csdn.net/detail/lyz12600/9581532

再次感谢@lyz12600.

-----------------------------------------------------------------------------

原文链接:http://blog.csdn.net/veryitman/article/details/7764894

原创粉丝点击