MySQL: 基于 android 远程连接

/************************************************************

声明:如需转载,请注明出处!

************************************************************/

本篇博客基于

在上篇博客里面,介绍了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";Util.execSQL(conn, sql);}public void onUpdate(View view) {String sql = "update mytable set where";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.javapackage 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=""android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dip"android:onClick="onConn"android:text="connect mysql" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dip"android:onClick="onInsert"android:text="insert data" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dip"android:onClick="onDelete"android:text="delete data" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginBottom="10dip"android:onClick="onUpdate"android:text="update data" /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:onClick="onQuery"android:text="query data" /></LinearLayout>

一遍一遍的……你突然明白自己还活着,

MySQL: 基于 android 远程连接

相关文章:

你感兴趣的文章:

标签云: