Android开发之MySQL操作

来源:互联网 发布:淘宝店铺入口图 编辑:程序博客网 时间:2024/05/01 05:50

如果想远程连接 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

[java] view plain copy print?
  1. package net.mark.util;  
  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 MySqlUtil {  
  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.                 while (!result.isAfterLast()) {  
  43.                     System.out.println("------------------");  
  44.                     System.out.print("id " + result.getString(idColumnIndex) + "\t");  
  45.                     System.out  
  46.                             .println("name " + result.getString(nameColumnIndex));  
  47.                     result.next();  
  48.                 }  
  49.             }  
  50.         } catch (SQLException e) {  
  51.             e.printStackTrace();  
  52.         } finally {  
  53.             try {  
  54.                 if (result != null) {  
  55.                     result.close();  
  56.                     result = null;  
  57.                 }  
  58.                 if (statement != null) {  
  59.                     statement.close();  
  60.                     statement = null;  
  61.                 }  
  62.   
  63.             } catch (SQLException sqle) {  
  64.   
  65.             }  
  66.         }  
  67.     }  
  68.   
  69.     public static boolean execSQL(Connection conn, String sql) {  
  70.         boolean execResult = false;  
  71.         if (conn == null) {  
  72.             return execResult;  
  73.         }  
  74.   
  75.         Statement statement = null;  
  76.   
  77.         try {  
  78.             statement = conn.createStatement();  
  79.             if (statement != null) {  
  80.                 execResult = statement.execute(sql);  
  81.             }  
  82.         } catch (SQLException e) {  
  83.             execResult = false;  
  84.         }  
  85.   
  86.         return execResult;  
  87.     }  
  88. }  

Main.java

[java] view plain copy print?
  1. package net.mark;  
  2.   
  3. import java.sql.Connection;  
  4.   
  5. import net.mark.util.MySqlUtil;  
  6.   
  7.   
  8. public class Main {  
  9.     private static final String URL = "jdbc:mysql://192.168.1.102/mydb";  
  10.     private static final String USER = "mark";  
  11.     private static final String PASSWORD = "123456";  
  12.   
  13.     public static void main(String[] args) throws Exception {  
  14.         Connection conn = MySqlUtil.openConnection(URL, USER, PASSWORD);  
  15.         System.out.println("All users info:");  
  16.         MySqlUtil.query(conn, "select * from mytable");  
  17.     }  
  18.   
  19. }  

Run as java application:

    

还可以插入、删除或者更新数据:

[java] view plain copy print?
  1. package net.mark;  
  2.   
  3. import java.sql.Connection;  
  4.   
  5. import net.mark.util.MySqlUtil;  
  6.   
  7.   
  8. public class Main {  
  9.     private static final String URL = "jdbc:mysql://192.168.1.102/mydb";  
  10.     private static final String USER = "mark";  
  11.     private static final String PASSWORD = "123456";  
  12.   
  13.     public static void main(String[] args) throws Exception {  
  14.         Connection conn = MySqlUtil.openConnection(URL, USER, PASSWORD);  
  15.         System.out.println("All users info:");  
  16.         MySqlUtil.execSQL(conn, "insert into mytable values(56,'小李')");  
  17.         MySqlUtil.execSQL(conn, "update mytable set name='mark' where id=1");  
  18.         MySqlUtil.execSQL(conn, "delete from mytable where id=6");  
  19.         MySqlUtil.query(conn, "select * from mytable");  
  20.     }  
  21.   



上面的访问是基于局域网通过 jdbc 操作 mysql。

远程访问 mysql,采用上面的方式也是可以的,但是速度不是很快。

0 0
原创粉丝点击