当前位置:   article > 正文

Android连接MySQL数据库_android mysql

android mysql

注意:

1. 要为MySQL添加 非root用户 设置权限一定要设置权限!!!默认是没有权限的!!!

请注意为用户设置主机时,主机设置为%时表示通配符,即任何主机均可使用本用户连接,但不能使用localhost(但可以使用本机ipv4地址连接),想使用localhost连接需将用户主机设置为localhost。

Android连MySQL因为不确定连接地址,所以用户主机要设置为%

2. 在Android中连接MySQL的目标ip不能用 //localhost 或 //127.0.0.1 应使用真实的ip地址

(可用cmd查询本机ip,cmd->ipconfig)

3. Android连接的MySQL版本应为5.X版本(8.X版本无法使用)驱动程序通用

4. 请注意!!!Android中 连接/对数据库操作 只能在子线程进行!!!!单开一个线程

因为是耗时操作!!!

如果url、账号、密码正确还报错大概率是没有在子线程操作数据库。

5. 应为程序添加网络及WIFI权限(通过网络连接数据库

  1. //必须加
  2. <uses-permission android:name="android.permission.INTERNET"/>
  3. //可不加
  4. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

6. 添加MySQL驱动程序

将JAR程序放入Project视图模式下 app/libs中,并右键->Add As Library

7. 在url后加入如下语句能避免许多麻烦

"?useUnicode=true&characterEncoding=utf-8&useSSL=false"

8. 打开电脑的telnet服务,使其他人可以访问

9. 打开电脑防火墙的MySQL端口,使其他人可以访问

10. 建议以如下格式写连接代码

    (1)ConnectionUtils 连接工具类,用于连接数据库

    (2)User  存储内容类,用于存储MySQL回传数据

    (3)UserDao  数据访问对象,执行SQL操作返回存储内容类

11. 连接关闭后ResultSet中内容会消失(将其内容在连接关闭前存在其他地方)

12. 读ResultSet内容前先移动指针 .next() 如果ResultSet中无搜索结果,.next()方法返回false,有搜索结果返回true。

13. 设置连接MySQL超时 DriverManager.setLoginTimeout(2000);

14. 尽量复用通道,避免反复连接造成延迟!!!

15. ip地址问题

个人电脑一般在局域网内,其ip为NAT局域网ip,只能被同局域网设备访问,无法直接被其他网络访问,可使用内网穿透软件(如花生壳)进行处理。

16. MySQL其实就是服务器,可以直接进行远程连接,不用再做服务器

17. MySQL自带的4个数据库不能删!!!

information_schema、mysql、performation_schema、test

18. 可以 多人 同时 使用 同个用户名及密码 登录,但不建议

19. MySQL中建议使用PreparedStatement而不是使用Statement,PreparedStatement的执行速度比Statement快并且可以防止SQL注入攻击。在使用PreparedStatement时可以用代替值,然后使用.setString(int index , String value)方法设置?的值,但请注意,PreparedStatement的index起始是1而不是0要复用PreparedStatement!!

实例:

(1)ConnectionUtils 连接工具类,用于连接数据库

(2)User  存储内容类,用于存储MySQL回传数据

(3)UserDao  数据访问对象,执行SQL操作返回存储内容类

  1. //主线程 以监听为例
  2. Thread thread=null;
  3. public void onClick(View view){
  4. if(thread==null){
  5. //连接数据库不能在主线程,单开一个线程
  6. thread=new Thread(new Runnable(){
  7. User user=UserDao.findUser(1);
  8. });
  9. }
  10. }
  1. //连接工具类
  2. public class ConnectionUtils {
  3. public static Connection getConn(){
  4. String url="jdbc:mysql://255.255.255.1:3306/databaseName"+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";
  5. String username="firstUser";
  6. String password="123456";
  7. try {
  8. Class.forName("com.mysql.jdbc.Driver");
  9. } catch (ClassNotFoundException e) {
  10. throw new RuntimeException(e);
  11. }
  12. Connection connection_jdbc = null;
  13. try {
  14. DriverManager.setLoginTimer(2000);
  15. connection_jdbc= DriverManager.getConnection(url,username,password);
  16. } catch (SQLException e) {
  17. throw new RuntimeException(e);
  18. }
  19. return connection_jdbc;
  20. }
  21. public static boolean close(Connection connection){
  22. if(connection!=null){
  23. try {
  24. connection.close();
  25. } catch (SQLException e) {
  26. throw new RuntimeException(e);
  27. }
  28. }
  29. }
  30. }
  1. //存储内容类
  2. public class User {
  3. private int id;
  4. private String username;
  5. private String password;
  6. private int storyid;
  7. public int getId() {
  8. return id;
  9. }
  10. public String getUsername() {
  11. return username;
  12. }
  13. public String getPassword() {
  14. return password;
  15. }
  16. public int getStoryid() {
  17. return storyid;
  18. }
  19. public User(int id,String username,String password,int storyid){
  20. this.id=id;
  21. this.username=username;
  22. this.password=password;
  23. this.storyid=storyid;
  24. }
  25. public String toString(){
  26. String str="id:"+id+" username:"+username+" password:"+password+" storyid:"+storyid;
  27. return str;
  28. }
  29. }
  1. //数据访问对象类
  2. public class Dao {
  3. private Connection conn;
  4. public static User findUser(int id){
  5. //尽量复用通道,避免反复连接造成延迟
  6. if(conn==null){
  7. conn=ConnectionUtils.getConn();
  8. }
  9. try {
  10. Statement statement=conn.createStatement();
  11. String sql="select * from mysql where id ='"+id+"'";
  12. ResultSet resultSet=statement.executeQuery(sql);
  13. Boolean bool = resultSet.next();//先移动指针再获取值
  14. //如果ResultSet无结果,next()返回false
  15. if(bool){
  16. int resultSetId=resultSet.getInt("id");
  17. String resultSetSex=resultSet.getString("sex");
  18. String resultSetName=resultSet.getString("name");
  19. User user=new User(resultSetId,resultSetSex,resultSetName);
  20. return user;
  21. }
  22. } catch (SQLException e) {
  23. throw new RuntimeException(e);
  24. }
  25. //确定不使用再关闭通道
  26. //ConnectionUtils.close(conn);
  27. }
  28. }

常见报错原因:

1.程序 对/连接 数据库操作 是否在子线程
2.mysql用户主机是否设置%(任意主机)或其他
3.mysql用户权限是否设置
4.程序连接时的用户名与密码是否正确

连接缓慢原因:

1.程序是否复用通道connection,反复连接数据库会导致缓慢
2.prepareStatement代替statement,减少命令的创建(要复用PreparedStatement)

tag: java ,远程连接 ,mysql ,Android ,安卓,MySQL

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/336461?site
推荐阅读
相关标签
  

闽ICP备14008679号