当前位置: 代码网 > it编程>App开发>Android > Android连接MySQL数据库实现方法详解

Android连接MySQL数据库实现方法详解

2024年05月15日 Android 我要评论
前言要为mysql添加 非root用户 并设置权限。一定要设置权限!!!默认是没有权限的!!!请注意为用户设置主机时,主机设置为%时表示通配符,即任何主机均可使用本用户连接,但不能使用localhos

前言

  • 要为mysql添加 非root用户 并设置权限。一定要设置权限!!!默认是没有权限的!!!

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

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

  • 在android中连接mysql的目标ip不能用//localhost//127.0.0.1,应使用真实的ip地址

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

  • android连接的mysql版本应为5.x版本(8.x版本无法使用)驱动程序通用
  • 请注意!!!android中连接/对数据库操作 只能在子线程进行!!!!(单开一个线程)

因为是耗时操作!!!

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

  • 应为程序添加网络及wifi权限(通过网络连接数据库)
//必须加
<uses-permission android:name="android.permission.internet"/>
//可不加
<uses-permission android:name="android.permission.access_wifi_state"/>
  • 添加mysql驱动程序

将jar程序放入project视图模式下 app/libs中,并右键->add as library

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

"?useunicode=true&characterencoding=utf-8&usessl=false"

  • 打开电脑的telnet服务,使其他人可以访问
  • 打开电脑防火墙的mysql端口,使其他人可以访问
  • 建议以如下格式写连接代码

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

(2)user 存储内容类,用于存储mysql回传数据

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

  • 连接关闭后resultset中内容会消失(将其内容在连接关闭前存在其他地方)
  • 读resultset内容前先移动指针 .next();如果resultset中无搜索结果,.next()方法返回false,有搜索结果返回true。
  • 设置连接mysql超时 drivermanager.setlogintimeout(2000);
  • 尽量复用通道,避免反复连接造成延迟!!!
  • ip地址问题

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

  • mysql其实就是服务器,可以直接进行远程连接,不用再做服务器
  • mysql自带的4个数据库不能删!!!

information_schema、mysql、performation_schema、test

  • 可以多人同时使用同个用户名及密码登录,但不建议
  • mysql中建议使用preparedstatement而不是使用statement,preparedstatement的执行速度比statement快并且可以防止sql注入攻击。在使用preparedstatement时可以用?代替值,然后使用.setstring(int index , string value)方法设置?的值,但请注意,preparedstatement的index起始是1而不是0。要复用preparedstatement!!

实例

  • connectionutils 连接工具类,用于连接数据库
  • user 存储内容类,用于存储mysql回传数据
  • userdao 数据访问对象,执行sql操作返回存储内容类
//主线程  以监听为例
thread thread=null;
public void onclick(view view){
      if(thread==null){
          //连接数据库不能在主线程,单开一个线程
          thread=new thread(new runnable(){
              user user=userdao.finduser(1);
          });
      }
}
//连接工具类
public class connectionutils {
    public static connection getconn(){
        string url="jdbc:mysql://255.255.255.1:3306/databasename"+"?useunicode=true&characterencoding=utf-8&usessl=false";
        string username="firstuser";
        string password="123456";
        try {
            class.forname("com.mysql.jdbc.driver");
        } catch (classnotfoundexception e) {
            throw new runtimeexception(e);
        }
        connection connection_jdbc = null;
        try {
            drivermanager.setlogintimer(2000);
            connection_jdbc= drivermanager.getconnection(url,username,password);
        } catch (sqlexception e) {
            throw new runtimeexception(e);
        }
        return connection_jdbc;
    }
    public static boolean close(connection connection){
        if(connection!=null){
            try {
                connection.close();
            } catch (sqlexception e) {
                throw new runtimeexception(e);
            }
        }
    }
}
//存储内容类
public class user {
    private int id;
    private string username;
    private string password;
    private int storyid;
    public int getid() {
        return id;
    }
    public string getusername() {
        return username;
    }
    public string getpassword() {
        return password;
    }
    public int getstoryid() {
        return storyid;
    }
    public user(int id,string username,string password,int storyid){
        this.id=id;
        this.username=username;
        this.password=password;
        this.storyid=storyid;
    }
    public string tostring(){
        string str="id:"+id+" username:"+username+" password:"+password+" storyid:"+storyid;
        return str;
    }
}
//数据访问对象类
public class dao {
    private connection conn;
    public static user finduser(int id){
        //尽量复用通道,避免反复连接造成延迟
        if(conn==null){
            conn=connectionutils.getconn();
        }
        try {
            statement statement=conn.createstatement();
            string sql="select * from mysql where id ='"+id+"'";
            resultset resultset=statement.executequery(sql);
            boolean bool = resultset.next();//先移动指针再获取值
            //如果resultset无结果,next()返回false
            if(bool){
               int resultsetid=resultset.getint("id");
               string resultsetsex=resultset.getstring("sex");
               string resultsetname=resultset.getstring("name");
               user user=new user(resultsetid,resultsetsex,resultsetname);
               return user;
            }
        } catch (sqlexception e) {
            throw new runtimeexception(e);
        }
        //确定不使用再关闭通道
        //connectionutils.close(conn);
    }
}

常见报错原因

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

连接缓慢原因

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

tag: java ,远程连接 ,mysql ,android ,安卓,mysql

到此这篇关于android连接mysql数据库实现方法详解的文章就介绍到这了,更多相关android连接mysql内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com