Android   数据库      Android通过JDBC访问PC端mysql流程及错误处理   
文章目录  [隐藏]

除非项目周期太短人手奇缺,否则不要直接以JDBC的形式在android内访问数据库。

1.测试环境配置

1)PC端配置



这个172.28.108.1即手机访问本PC上的mysql时的host。

2)Android手机配置

为了验证mysql的host,还需要在手机上确认。
设置,WLAN,高级,查看手机的IP地址。

现在手机和pc都在 172.28.108 这个网段,ok。

2.使用新版mysql驱动引起的jdk版本不兼容问题

这里的mysql驱动文件是:
  mysql-connector-java-5.1.38-bin.jar


当向手机发布apk时报错:

查看当前jdk版本:

查看Eclipse/ADT需要的jdk版本。

一般情况下,即使系统环境变量使用的jdk为1.7、1.8都可以。但遇到这种情况,就须要:Eclipse中的JDK版本高于或等于系统环境变量里的JDK版本。
据说,Android开发最好安装jdk1.6;java开发安装1.5即可。1.7开始的版本为Oracle公司开发,其与Google尚有版权冲突,不得不考虑。

而如果将jdk版本限制为1.6,那么就得考虑Eclipse版本兼容,因为较高版本要求jdk最低为1.7。能兼容1.6的eclipse有3.7 - 4.2。限制JDK、eclipse版本后,又须要匹配ADT插件版本。...

简便方法就是直接更换原Google提供的adt-bundle版本开发环境。本例直接使用adt-bundle-windows-x86-20130219。官方发布AndroidStudio后已经不再更新。

JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html
Eclipse下载地址:http://archive.eclipse.org/eclipse/downloads/
ADT下载地址:https://dl-ssl.google.com/android/eclipse/

3.Mysql拒绝远程连接问题

在Android中连接mysql,即一般的jdbc操作而已。
如果没有对Mysql做针对性配置,会发生这个错误:
java.sql.SQLException: null, message from server: "Host '主机名或IP' is not allowed to connect to this MySQL server"

此时须要开启Mysql的远程访问支持。这个支持是针对用户而言的。以下3中方式未必对任何版本的mysql适用,因为不同版本mysql或多或少有不同架构。

方法1

用root用户登陆,然后:
grant all privileges on *.* to 用户名 @"%" identified by "密码";
flush privileges;


格式:
  grant 权限 on 数据库教程名.表名 to 用户@登录主机 identified by "用户密码";
@ 后面是访问mysql的客户端ip地址(或是 主机名) % 代表任意的客户端,如果填写 localhost 为本地访问,用户就不能远程访问该mysql数据库了。

同时也可以为现有的用户设置是否具有远程访问权限。如下:
use mysql;
update db set host = '%' where user = '用户名';
flush privileges;
grant all privileges on *.* to 'myuser'@'%' identified by 'mypassword' with grant option;

方法2

首先在数据库本机上用root用户登录mysql,然后输入:
grant all privileges on *.* to admin@localhost identified by 'something' with grant option;

添加一个用户admin并授权通过本地机(localhost)访问,密码"something"。
grant all privileges on *.* to admin@"%" identified by 'something' with grant option;

添加一个用户admin并授权可从任何其它主机发起的访问(通配符%)。使用这一条语句即可。

或者,可以使用insert语句:
insert into user
values('%','admin',password('something'), 'y','y','y','y','y','y','y','y','y','y','y','y','y','y');

用户信息可在mysql数据库中的users表中查看,注意“y”的个数要匹配表的字段数量。

方法3

添加远程用户admin密码为password
grant all privileges on *.* to admin@localhost identified by 'password' with grant option;
grant all privileges on *.* to admin@"%" identified by 'password' with grant option;

SSL警告

WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

解决方法1-屏蔽:

解决方法2-开启SSL:
首先用命令查询你的mysql服务器是否支持ssl的连接特性,
SHOW VARIABLES LIKE '%have%ssl%';

要 确保你看到的是disabled,而不是NO的选项

这表示你的mysql服务器支持ssl,只是没有在启动的时候开启ssl功能,当服务器启动ssl功能的时候,你会看到图中的disabled会显示yes


eclipse-adt项目地址:https://git.oschina.net/ivigiles/AndroidCallMysqlUseJDBC

承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设