/*************************************** *********************
Statement: If You Need To reprint it, please indicate the source!
**************************************** ********************/
This blog is based on http://blog.csdn.net/androidbluetooth/article/details/7716175
In the previous blog, I introduced the JDBC remote connection operation and succeeded.
Click here to view details.
So can we port this operation to Android?
The answer is yes. Do not believe it.
Program
Project Structure
Create the libs directory in the project file and put the jdbc jar file in it.
In this way, the ADT plug-in automatically adds the jar to build path.
Raw data in the database table
Insert data
Delete data
Update Data
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>
Note:
<Uses-Permission Android: Name = "android. Permission. Internet"/>