iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >Android Studio连接MySql实现登录注册的示例代码
  • 778
分享到

Android Studio连接MySql实现登录注册的示例代码

2023-06-15 03:06:17 778人浏览 泡泡鱼
摘要

小编给大家分享一下Android Studio连接MySql实现登录注册的示例代码,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!具体如下:一、创建工程创建一个空白

小编给大家分享一下Android Studio连接MySql实现登录注册的示例代码,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

具体如下:

Android Studio连接MySql实现登录注册的示例代码

Android Studio连接MySql实现登录注册的示例代码

一、创建工程

创建一个空白工程

Android Studio连接MySql实现登录注册的示例代码

随便起一个名称

Android Studio连接MySql实现登录注册的示例代码

设置网络连接权限

Android Studio连接MySql实现登录注册的示例代码

 <uses-permission android:name="android.permission.INTERNET"/>

二、引入Mysql驱动包

切换到普通Java工程

Android Studio连接MySql实现登录注册的示例代码

在libs当中引入mysqljar

将mysql的驱动包复制到libs当中

Android Studio连接MySql实现登录注册的示例代码

Android Studio连接MySql实现登录注册的示例代码

三、编写数据库和dao以及JDBC相关代码

数据库当中创建表

Android Studio连接MySql实现登录注册的示例代码

SQL语句

SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for `student`-- ----------------------------DROP TABLE IF EXISTS `student`;CREATE TABLE `student` (  `sid` int(11) NOT NULL AUTO_INCREMENT,  `sname` varchar(255) NOT NULL,  `sage` int(11) NOT NULL,  `address` varchar(255) NOT NULL,  PRIMARY KEY (`sid`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;-- ------------------------------ Records of student-- ----------------------------INSERT INTO `student` VALUES ('1', 'andi', '21', '21212');INSERT INTO `student` VALUES ('2', 'a', '2121', '2121');-- ------------------------------ Table structure for `users`-- ----------------------------DROP TABLE IF EXISTS `users`;CREATE TABLE `users` (  `uid` int(11) NOT NULL AUTO_INCREMENT,  `name` varchar(255) NOT NULL,  `username` varchar(255) NOT NULL,  `passWord` varchar(255) NOT NULL,  `age` int(255) NOT NULL,  `phone` longblob NOT NULL,  PRIMARY KEY (`uid`)) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;-- ------------------------------ Records of users-- ----------------------------INSERT INTO `users` VALUES ('2', '123', 'HBV环保局', '123', '33', 0x3133333333333333333333);INSERT INTO `users` VALUES ('3', '1233', '反复的', '1233', '12', 0x3132333333333333333333);INSERT INTO `users` VALUES ('4', '1244', '第三代', '1244', '12', 0x3133333333333333333333);INSERT INTO `users` VALUES ('5', '1255', 'SAS', '1255', '33', 0x3133333333333333333333);

在Android Studio当中创建JDBCUtils类

切换会Android视图

Android Studio连接MySql实现登录注册的示例代码

Android Studio连接MySql实现登录注册的示例代码

Android Studio连接MySql实现登录注册的示例代码

Android Studio连接MySql实现登录注册的示例代码

注意链接数据库的地址是:jdbc:mysql://10.0.2.2:3306/test

package com.example.myapplication.utils;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class JDBCUtils {    static {        try {            Class.forName("com.mysql.jdbc.Driver");        } catch (ClassNotFoundException e) {            e.printStackTrace();        }    }    public static Connection getConn() {        Connection  conn = null;        try {            conn= DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/test","root","root");        }catch (Exception exception){            exception.printStackTrace();        }        return conn;    }    public static void close(Connection conn){        try {            conn.close();        } catch (SQLException throwables) {            throwables.printStackTrace();        }    }}

创建User实体类

Android Studio连接MySql实现登录注册的示例代码

package com.example.myapplication.entity;public class User {    private int id;    private String name;    private String username;    private String password;    private int age;    private String phone;    public User() {    }    public User(int id, String name, String username, String password, int age, String phone) {        this.id = id;        this.name = name;        this.username = username;        this.password = password;        this.age = age;        this.phone = phone;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone;    }}

创建dao层和UserDao

Android Studio连接MySql实现登录注册的示例代码

package com.example.myapplication.dao;import com.example.myapplication.entity.User;import com.example.myapplication.utils.JDBCUtils;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class UserDao {    public boolean login(String name,String password){        String sql = "select * from users where name = ? and password = ?";        Connection  con = JDBCUtils.getConn();        try {            PreparedStatement pst=con.prepareStatement(sql);            pst.setString(1,name);            pst.setString(2,password);            if(pst.executeQuery().next()){                return true;            }        } catch (SQLException throwables) {            throwables.printStackTrace();        }finally {            JDBCUtils.close(con);        }        return false;    }    public boolean reGISter(User user){        String sql = "insert into users(name,username,password,age,phone) values (?,?,?,?,?)";        Connection  con = JDBCUtils.getConn();        try {            PreparedStatement pst=con.prepareStatement(sql);            pst.setString(1,user.getName());            pst.setString(2,user.getUsername());            pst.setString(3,user.getPassword());            pst.setInt(4,user.getAge());            pst.setString(5,user.getPhone());            int value = pst.executeUpdate();            if(value>0){                return true;            }        } catch (SQLException throwables) {            throwables.printStackTrace();        }finally {            JDBCUtils.close(con);        }        return false;    }    public User findUser(String name){        String sql = "select * from users where name = ?";        Connection  con = JDBCUtils.getConn();        User user = null;        try {            PreparedStatement pst=con.prepareStatement(sql);            pst.setString(1,name);            ResultSet rs = pst.executeQuery();            while (rs.next()){               int id = rs.getInt(0);               String namedb = rs.getString(1);               String username = rs.getString(2);               String passworddb  = rs.getString(3);               int age = rs.getInt(4);                String phone = rs.getString(5);               user = new User(id,namedb,username,passworddb,age,phone);            }        } catch (SQLException throwables) {            throwables.printStackTrace();        }finally {            JDBCUtils.close(con);        }        return user;    }}

四、编写页面和Activity相关代码

编写登录页面

Android Studio连接MySql实现登录注册的示例代码

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="Http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        tools:layout_editor_absoluteX="219dp"        tools:layout_editor_absoluteY="207dp"        android:padding="50dp"        >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:id="@+id/textView"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:textSize="15sp"                android:text="账号:" />            <EditText                android:id="@+id/name"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:ems="10"                android:inputType="textPersonName"                android:text="" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:id="@+id/textView2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:textSize="15sp"                android:text="密码:"                />            <EditText                android:id="@+id/password"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:ems="10"                android:inputType="textPersonName"               />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">        </LinearLayout>        <Button            android:layout_marginTop="50dp"            android:id="@+id/button2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="登录"            android:onClick="login"            />        <Button            android:id="@+id/button3"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:onClick="reg"            android:text="注册" />    </LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

效果

Android Studio连接MySql实现登录注册的示例代码

编写注册页面代码

Android Studio连接MySql实现登录注册的示例代码

Android Studio连接MySql实现登录注册的示例代码

Android Studio连接MySql实现登录注册的示例代码

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        tools:layout_editor_absoluteX="219dp"        tools:layout_editor_absoluteY="207dp"        android:padding="50dp"        >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:id="@+id/textView"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:textSize="15sp"                android:text="账号:" />            <EditText                android:id="@+id/name"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:ems="10"                android:inputType="textPersonName"                android:text="" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <TextView                android:id="@+id/textView2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:textSize="15sp"                android:text="密码:"                />            <EditText                android:id="@+id/password"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_weight="1"                android:ems="10"                android:inputType="textPersonName"                />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">        </LinearLayout>        <Button            android:layout_marginTop="50dp"            android:id="@+id/button2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="登录"            android:onClick="login"            />        <Button            android:id="@+id/button3"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:onClick="reg"            android:text="注册" />    </LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>

完善MainActivity

Android Studio连接MySql实现登录注册的示例代码

package com.example.application01;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.EditText;import android.widget.Toast;import com.example.application01.dao.UserDao;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void reg(View view){        startActivity(new Intent(getApplicationContext(),RegisterActivity.class));    }    public void login(View view){        EditText EditTextname = (EditText)findViewById(R.id.name);        EditText EditTextpassword = (EditText)findViewById(R.id.password);        new Thread(){            @Override            public void run() {                UserDao userDao = new UserDao();                boolean aa = userDao.login(EditTextname.getText().toString(),EditTextpassword.getText().toString());                int msg = 0;                if(aa){                    msg = 1;                }                hand1.sendEmptyMessage(msg);            }        }.start();    }    final Handler hand1 = new Handler()    {        @Override        public void handleMessage(Message msg) {            if(msg.what == 1)            {                Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_LONG).show();            }            else            {                Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_LONG).show();            }        }    };}

完善RegisterActivity

Android Studio连接MySql实现登录注册的示例代码

package com.example.application01;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.EditText;import android.widget.Toast;import com.example.application01.dao.UserDao;import com.example.application01.entity.User;public class RegisterActivity extends AppCompatActivity {    EditText name = null;    EditText username = null;    EditText password = null;    EditText phone = null;    EditText age = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_register);         name = findViewById(R.id.name);         username = findViewById(R.id.username);         password = findViewById(R.id.password);         phone = findViewById(R.id.phone);         age = findViewById(R.id.age);    }    public void register(View view){        String cname = name.getText().toString();        String cusername = username.getText().toString();        String cpassword = password.getText().toString();        System.out.println(phone.getText().toString());        String cphone = phone.getText().toString();        int cgae = Integer.parseInt(age.getText().toString());        if(cname.length() < 2 || cusername.length() < 2 || cpassword.length() < 2 ){            Toast.makeText(getApplicationContext(),"输入信息不符合要求请重新输入",Toast.LENGTH_LONG).show();            return;        }        User user = new User();        user.setName(cname);        user.setUsername(cusername);        user.setPassword(cpassword);        user.setAge(cgae);        user.setPhone(cphone);        new Thread(){            @Override            public void run() {                int msg = 0;                UserDao userDao = new UserDao();                User uu = userDao.findUser(user.getName());                if(uu != null){                    msg = 1;                }                boolean flag = userDao.register(user);                if(flag){                    msg = 2;                }                hand.sendEmptyMessage(msg);            }        }.start();    }    final Handler hand = new Handler()    {        @Override        public void handleMessage(Message msg) {            if(msg.what == 0)            {                Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show();            }            if(msg.what == 1)            {                Toast.makeText(getApplicationContext(),"该账号已经存在,请换一个账号",Toast.LENGTH_LONG).show();            }            if(msg.what == 2)            {                //startActivity(new Intent(getApplication(),MainActivity.class));                Intent intent = new Intent();                //将想要传递的数据用putExtra封装在intent中                intent.putExtra("a","註冊");                setResult(RESULT_CANCELED,intent);                finish();            }        }    };}

五、运行测试效果

Android Studio连接MySql实现登录注册的示例代码

Android Studio连接MySql实现登录注册的示例代码

Android Studio连接MySql实现登录注册的示例代码

以上是“Android Studio连接MySql实现登录注册的示例代码”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网数据库频道!

您可能感兴趣的文档:

--结束END--

本文标题: Android Studio连接MySql实现登录注册的示例代码

本文链接: https://www.lsjlt.com/news/276929.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android Studio连接MySql实现登录注册的示例代码
    小编给大家分享一下Android Studio连接MySql实现登录注册的示例代码,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!具体如下:一、创建工程创建一个空白...
    99+
    2023-06-15
  • Android Studio连接MySql实现登录注册(附源代码)
    目录一、创建工程二、引入Mysql驱动包三、编写数据库和dao以及JDBC相关代码四、编写页面和Activity相关代码五、运行测试效果本文主要介绍了Android Studio连接...
    99+
    2022-11-12
  • Android Studio+Servlet+MySql实现登录注册
    一、Android 项目当中设置明文传输 1、设置明文传输的xml <?xml version="1.0" encoding="UTF-8"?...
    99+
    2022-11-12
  • Redis实现登录注册的示例代码
    目录1. 引言2. 流程图及代码实现2.1 生成验证码保存到Redis2.2 登录验证2.3 请求拦截器3. 总结1. 引言 在传统的项目中,用户登录成功,将用户信息保存在sessi...
    99+
    2022-11-13
  • django+vue实现注册登录的示例代码
    注册 前台利用vue中的axios进行传值,将获取到的账号密码以form表单的形式发送给后台。 form表单的作用就是采集数据,也就是在前台页面中获取用户输入的值。numberVa...
    99+
    2022-11-12
  • 用node和express连接mysql实现登录注册的实现代码
    为了数据库课设,打算后台用node搭建,前台用vue搞个博客出来(因为前段时间在学啊)。本来node不想用框架,喜欢先打好基础的,奈何3个星期要把他做完和应付各种考试,所以最后还是用了express,大大简...
    99+
    2022-06-04
    代码 node express
  • Android studio连接MySQL并完成简单的登录注册功能
    近期需要完成一个Android项目,那先从与数据库交互最简单的登陆注册开始吧,现记录过程如下: 此篇文章的小demo主要涉及数据库的连接,以及相应信息的查找与插入。 我已将源码上传至GitHub: h...
    99+
    2023-09-08
    android studio mysql android
  • vue实现登录注册模板的示例代码
    模板1:  login.vue <template> <p class="login"> <el-tabs v-model="ac...
    99+
    2022-11-12
  • 基于Java实现QQ登录注册功能的示例代码
    目录前言实现代码登录页面注册页面效果展示前言 本文主要应用的技术有:GUI、JDBC、多线程 实现的功能具体如下: 1、登录功能 2、注册功能 3、是否隐藏密码的选择以及实现功能 4...
    99+
    2022-11-13
  • Android实现微信登录的示例代码
    目录一、布局界面二、MainActivity.java微信登录的实现与qq登录类似。不过微信登录比较麻烦,需要拿到开发者资质认证,花300块钱,然后应用的话还得有官网之类的,就是比较...
    99+
    2022-11-12
  • Android实现简易登陆注册逻辑的实例代码
    大家好,今天给大家带来Android制作登录和注册功能的实现,当我们面临制作登录和注册功能的实现时,我们需要先设计登录界面的布局和注册界面的布局,做到有完整的思路时才开始实现其功能效...
    99+
    2022-11-12
  • 如何使用node和express连接mysql实现登录注册
    这篇文章将为大家详细讲解有关如何使用node和express连接mysql实现登录注册,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。数据库我选了比较“正式”的MySQL,...
    99+
    2022-10-19
  • Android Studio实现QQ的注册登录和好友列表跳转
    一、项目概述 本次项目主要包含了注册、登录和好友列表三个界面以及之间相互跳转。其中好友列表界面设计的很详细,有好友头像和消息内容。用户先点击注册按钮进入注册界面,输入完账号和密码后,...
    99+
    2022-11-12
  • Android Studio|使用SqLite实现一个简单的登录注册功能
    本学期学习了Android Studio这门课程,本次使用Android Studio自带的sqlite数据库实现一个简单的登录注册功能。 目录 一、了解什么是Android Studio? 二、了解什么是sqlite? 三、创建项目文件 ...
    99+
    2023-10-06
    sqlite android studio 数据库
  • SpringBoot实现扫码登录的示例代码
    目录一、首先咱们需要一张表二、角色都有哪些三、接口都需要哪些?四、步骤五、疯狂贴代码Spring Boot中操作WebSocket最近有个项目涉及到websocket实现扫码登录,看...
    99+
    2022-11-13
  • 使用Vue+MySQL实现登录注册的实战案例
    目录1.新建vue项目并连接数据库2.新建登录页面、注册页面和首页3.页面路由配置4.新建/server/API/login.js5.在/server/router.js中配置对应路...
    99+
    2022-11-13
  • JavaWeb 07_创建web项目连接MySQL实现注册登录功能
    一、创建一个web项目,参照JW/01_创建web项目及部署   二、在NAVICat 里建数据库 db_01,建表tb_user ,字段UName 、Pwd     三、在web下创建一个Directory, 设名字为JSPWorks...
    99+
    2015-04-14
    JavaWeb 07_创建web项目连接MySQL实现注册登录功能
  • Python + Tkinter连接本地MySQL数据库简单实现注册登录
    项目结构: 源代码: # -*- coding: utf-8 -*- """ @date:  2022/01/09 17:40 @author: Anker @python:...
    99+
    2022-11-12
  • Python+Tkinter连接本地MySQL数据库怎么实现注册登录
    这篇文章主要介绍“Python+Tkinter连接本地MySQL数据库怎么实现注册登录”,在日常操作中,相信很多人在Python+Tkinter连接本地MySQL数据库怎么实现注册登录问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作...
    99+
    2023-06-26
  • SpringBoot实现token登录的示例代码
    为什么引入token机制 在进行登录验证时,我们需要session或cookie会话进行验证,客户端包括浏览器、app、微信小程序、公众号,只有浏览器有session和cookie机...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作