Android開發之MySQL操作
阿新 • • 發佈:2019-02-15
如果想遠端連線 mysql,需要記住:mysql 允許被遠端訪問。
1. 啟動 MySql
sudo /etc/init.d/mysql.server start
mysql -u root -p
2. 增加一個使用者
use mysql(mysql 是自帶的資料庫檔案,裡面有張表 user)
執行下面的兩條命令:
解釋:
grant all privileges on *.* to 'mark' @'%' identified by '123456';
授予使用者 mark,密碼 123456,可以使用任意 ip (%)訪問任何資料庫(*.*) .
查看錶中資料:
可以看出 mark 這個使用者建立了。
password 是被加密了的。host 為 % 表示通配任意 ip(ipv4)。
3. 命令列驗證
ctrl + c 停止 mysql 互動模式
ifconfig 檢視本機的 ip:192.168.1.102
mysql -h 192.168.1.102 -umark -p123456
如果進入互動模式,就 ok.
新建一個數據庫檔案 mydb 和表 mytable:
4. jdbc
eclipse --- New Java project
注意:
要將 jdbc(connector-java-5.1.6-bin.jar 版本) 的 jar 檔案 Build path 到這個專案。
MySqlUtil.java
- package net.mark.util;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- publicclass MySqlUtil {
- publicstatic 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;
- }
- publicstaticvoid 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");
- while (!result.isAfterLast()) {
- System.out.println("------------------");
- System.out.print("id " + result.getString(idColumnIndex) + "\t");
- System.out
- .println("name " + 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) {
- }
- }
- }
- publicstaticboolean 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.java
[java] view plain copy print?- package net.mark;
- import java.sql.Connection;
- import net.mark.util.MySqlUtil;
- publicclass Main {
- privatestaticfinal String URL = "jdbc:mysql://192.168.1.102/mydb";
- privatestaticfinal String USER = "mark";
- privatestaticfinal String PASSWORD = "123456";
- publicstaticvoid main(String[] args) throws Exception {
- Connection conn = MySqlUtil.openConnection(URL, USER, PASSWORD);
- System.out.println("All users info:");
- MySqlUtil.query(conn, "select * from mytable");
- }
- }
Run as java application:
還可以插入、刪除或者更新資料:
[java] view plain copy print?- package net.mark;
- import java.sql.Connection;
- import net.mark.util.MySqlUtil;
- publicclass Main {
- privatestaticfinal String URL = "jdbc:mysql://192.168.1.102/mydb";
- privatestaticfinal String USER = "mark";
- privatestaticfinal String PASSWORD = "123456";
- publicstaticvoid main(String[] args) throws Exception {
- Connection conn = MySqlUtil.openConnection(URL, USER, PASSWORD);
- System.out.println("All users info:");
- MySqlUtil.execSQL(conn, "insert into mytable values(56,'小李')");
- MySqlUtil.execSQL(conn, "update mytable set name='mark' where id=1");
- MySqlUtil.execSQL(conn, "delete from mytable where id=6");
- MySqlUtil.query(conn, "select * from mytable");
- }
- }
上面的訪問是基於區域網通過 jdbc 操作 mysql。
遠端訪問 mysql,採用上面的方式也是可以的,但是速度不是很快。