/************************************************************
聲明:如需轉載,請註明出處!
************************************************************/
本篇部落格基於 http://blog.csdn.net/androidbluetooth/article/details/7716175
在上篇部落格裡面,介紹了jdbc 的遠端連線操作,並且成功了。
點擊這裡,查看細節。
那麼,我們是否可以將這種操作移植到 android 上面?
答案是肯定的,不信你往下看。
程式
項目結構
專案檔中建立 libs 目錄,將 jdbc 的 jar 檔案放到裡面。
這樣 adt 外掛程式自動將該 jar 添加到 build path.
資料庫表裡面的未經處理資料
插入資料
刪除資料
更新資料
Main.java
package mark.zhang;import java.sql.Connection;import java.sql.SQLException;import android.app.Activity;import android.os.Bundle;import android.view.View;public class Main extends Activity {private static final String REMOTE_IP = "192.168.1.102";private static final String URL = "jdbc:mysql://" + REMOTE_IP + "/mydb";private static final String USER = "mark";private static final String PASSWORD = "123456";private Connection conn;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);}public void onConn(View view) {conn = Util.openConnection(URL, USER, PASSWORD);}public void onInsert(View view) {String sql = "insert into mytable values(9, 'hanmeimei')";Util.execSQL(conn, sql);}public void onDelete(View view) {String sql = "delete from mytable where name='mark'";Util.execSQL(conn, sql);}public void onUpdate(View view) {String sql = "update mytable set name='lilei' where name='hanmeimei'";Util.execSQL(conn, sql);}public void onQuery(View view) {System.out.println("All users info:");Util.query(conn, "select * from mytable");}@Overrideprotected void onDestroy() {super.onDestroy();if (conn != null) {try {conn.close();} catch (SQLException e) {conn = null;} finally {conn = null;}}}}
Util.java
package mark.zhang;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Util {public static Connection openConnection(String url, String user,String password) {Connection conn = null;try {final String DRIVER_NAME = "com.mysql.jdbc.Driver";Class.forName(DRIVER_NAME);conn = DriverManager.getConnection(url, user, password);} catch (ClassNotFoundException e) {conn = null;} catch (SQLException e) {conn = null;}return conn;}public static void query(Connection conn, String sql) {if (conn == null) {return;}Statement statement = null;ResultSet result = null;try {statement = conn.createStatement();result = statement.executeQuery(sql);if (result != null && result.first()) {int idColumnIndex = result.findColumn("id");int nameColumnIndex = result.findColumn("name");System.out.println("id\t\t" + "name");while (!result.isAfterLast()) {System.out.print(result.getString(idColumnIndex) + "\t\t");System.out.println(result.getString(nameColumnIndex));result.next();}}} catch (SQLException e) {e.printStackTrace();} finally {try {if (result != null) {result.close();result = null;}if (statement != null) {statement.close();statement = null;}} catch (SQLException sqle) {}}}public static boolean execSQL(Connection conn, String sql) {boolean execResult = false;if (conn == null) {return execResult;}Statement statement = null;try {statement = conn.createStatement();if (statement != null) {execResult = statement.execute(sql);}} catch (SQLException e) {execResult = false;}return execResult;}}
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:onClick="onConn" android:text="connect mysql" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:onClick="onInsert" android:text="insert data" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:onClick="onDelete" android:text="delete data" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dip" android:onClick="onUpdate" android:text="update data" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onQuery" android:text="query data" /></LinearLayout>
manifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mark.zhang" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".Main" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"/></manifest>
注意:
<uses-permission android:name="android.permission.INTERNET"/>