引言
在 Android 应用程序中实现用户认证至关重要,因为它可以保护用户数据并提供个性化的体验。本文将指导你如何使用 Android Studio 通过 MySQL 数据库实现登录、注册和注销功能。
先决条件
- 安装 Android Studio
- 具有 MySQL 数据库的服务器
- 了解 Java 和 XML
步骤 1:数据库连接
- 按下 Win+R 打开“运行”对话框。
- 输入
mysql -h localhost -u root -p
,然后按 Enter。 - 系统会提示你输入密码。输入你设置的 MySQL root 用户密码。
- 登录成功后,输入以下命令更新用户权限:
USE mysql;
UPDATE user SET host = '%' WHERE user = 'root';
FLUSH PRIVILEGES;
步骤 2:创建数据库和表及Android Studio 配置
在 Navicat 中创建数据库和表(如果没有连接)
步骤 1:连接到 MySQL 数据库服务器
- 打开 Navicat。
- 单击“连接”菜单,然后选择“MySQL”。
- 在“连接”对话框中,输入以下信息:
- 连接名: 输入一个连接名称,以便以后轻松识别此连接。
- 主机: 输入 MySQL 数据库服务器的地址或主机名。
- 端口: 输入 MySQL 数据库服务器的端口号(通常为 3306)。
- 用户名: 输入具有创建数据库和表权限的 MySQL 用户名。
- 密码: 输入 MySQL 用户的密码。
4.单击“测试连接”按钮以验证连接设置是否正确。
5.单击“确定”保存连接。
步骤 2:创建数据库
- 在 Navicat 中,右键单击“数据库”节点,然后选择“新建数据库”。
- 在“数据库名称”字段中输入“login”。
- 单击“确定”创建数据库。
步骤 3:创建表
- 右键单击“login”数据库,然后选择“新建表”。
- 在“表名称”字段中输入“userinfo”。
- 在“字段”选项卡中,添加以下字段:
字段名 | 数据类型 |
---|---|
uname | VARCHAR(20) |
psw | VARCHAR(20) |
4.单击“确定”创建表。
Android Studio 配置
- 在 Android Studio 中创建一个新项目。
- 在项目根目录下的
build.gradle
文件中添加 MySQL 依赖项:
implementation("mysql:mysql-connector-java:5.1.47")
3.在 AndroidManifest.xml
文件中添加 Internet 权限:
<uses-permission android:name="android.permission.INTERNET" />
4.创建 JdbcHelper.java 类,用于连接 MySQL 数据库。
package com.example.myapplication;
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class jdbcHelper {
// MySql数据库的MySQL 数据库的连接 URL,包括主机名、端口号和数据库名称。
static String url = "jdbc:mysql://192.000.111.222:3306/login";
// MySql数据库的用户名。
static String name = "root";
// MySQL 数据库的密码。
static String psw = "123456";
public static Connection getCon() {
Connection con = null;
try {
// 加载 MySQL JDBC 驱动程序。
Class.forName("com.mysql.jdbc.Driver");
// 使用 DriverManager.getConnection 方法尝试建立与 MySQL 数据库的连接
con = (Connection) DriverManager.getConnection(url,name,psw);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return con;
}
}
要注意的是,192.000.111.222是你自己电脑的IP地址
步骤 3:登录界面
- 创建登录界面布局。
- 实现登录界面功能的代码,包括:
- 从输入框中获取用户名和密码。
- 连接到数据库并验证用户名和密码。
- 显示登录成功或失败信息。
界面代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="登录"
android:textStyle="bold"
android:layout_gravity="center"></TextView>
<EditText
android:id="@+id/uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:paddingLeft="10dp"></EditText>
<EditText
android:id="@+id/upsw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:paddingLeft="10dp"></EditText>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"></Button>
<TextView
android:id="@+id/zc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
android:textSize="17sp"
android:layout_gravity="center"></TextView>
<TextView
android:id="@+id/zx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注销"
android:textSize="17sp"
android:layout_gravity="center"></TextView>
</LinearLayout>
功能代码:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MainActivity extends AppCompatActivity {
EditText uname,upsw;
TextView zc,zx;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname = findViewById(R.id.uname);
upsw = findViewById(R.id.upsw);
zc = findViewById(R.id.zc);
zx = findViewById(R.id.zx);
btn = findViewById(R.id.login);
// 登录按钮点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 开启一个新线程来执行登录操作
new Thread(new Runnable() {
@Override
public void run() {
// 获取到 MySQL 数据库的连接
Connection con = jdbcHelper.getCon();
// 准备一个 SQL 查询语句来检查用户凭据
String sqlStr = "select * from userinfo where uname=? and psw=?";
try {
// 创建一个 PreparedStatement 对象
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sqlStr);
// 设置查询参数
ps.setString(1,uname.getText().toString());
ps.setString(2,upsw.getText().toString());
// 进行查询
ResultSet rs = ps.executeQuery();
// 使用 Looper 来更新 UI
Looper.prepare();
// 判断查询结果是否存在
if(rs.next()) {
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
}
// 结束 Looper
Looper.loop();
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
}
});
// 跳转到注册页面按钮点击事件
zc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
startActivity(intent);
}
});
// 跳转到注销页面按钮点击事件
zx.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MainActivity3.class);
startActivity(intent);
}
});
}
}
步骤 4:注册界面
- 创建注册界面布局。
- 实现注册界面功能的代码,包括:
- 从输入框中获取用户名、密码和电子邮件地址。
- 检查用户名是否存在。
- 显示注册成功或失败信息。
界面代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="注册"
android:textStyle="bold"
android:layout_gravity="center"></TextView>
<EditText
android:id="@+id/uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:paddingLeft="10dp"></EditText>
<EditText
android:id="@+id/upsw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:paddingLeft="10dp"></EditText>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"></Button>
</LinearLayout>
功能代码:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MainActivity2 extends AppCompatActivity {
EditText uname,upsw;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
uname = findViewById(R.id.uname);
upsw = findViewById(R.id.upsw);
btn = findViewById(R.id.login);
// 注册按钮点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 开启一个新线程来执行注册操作
new Thread(new Runnable() {
@Override
public void run() {
// 获取到 MySQL 数据库的连接
Connection con = jdbcHelper.getCon();
// 准备一个 SQL 查询语句来检查用户凭据
String sqlStr = "select * from userinfo where uname=?";
try {
// 创建一个 PreparedStatement 对象
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sqlStr);
// 设置查询参数
ps.setString(1,uname.getText().toString());
// 进行查询
ResultSet rs = ps.executeQuery();
// 使用 Looper 来更新 UI
Looper.prepare();
// 判断查询结果是否存在
if(rs.next()) {
// 用户名已存在
Toast.makeText(MainActivity2.this,"用户名已存在",Toast.LENGTH_SHORT).show();
} else {
// 注册
String sqlStr1 = "insert into userinfo(uname,psw) values(?,?)";
try {
// 创建一个新的 PreparedStatement 对象
PreparedStatement ps1 = (PreparedStatement) con.prepareStatement(sqlStr1);
// 设置插入参数
ps1.setString(1,uname.getText().toString());
ps1.setString(2,upsw.getText().toString());
// 执行插入操作
int rows = ps1.executeUpdate();
// 判断插入是否成功
if(rows > 0) {
// 更新 UI
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity2.this,"注册成功",Toast.LENGTH_SHORT).show();
}
});
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 结束 Looper
Looper.loop();
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
步骤 5:注销界面
- 创建注销界面布局。
- 实现注销界面功能的代码,包括:
- 从输入框中获取用户名和密码。
- 检查用户名和密码是否在数据库当中。
- 显示注销成功或失败信息。
界面代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="注销"
android:textStyle="bold"
android:layout_gravity="center"></TextView>
<EditText
android:id="@+id/uname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:paddingLeft="10dp"></EditText>
<EditText
android:id="@+id/upsw"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:paddingLeft="10dp"></EditText>
<Button
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注销"></Button>
</LinearLayout>
功能代码:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.mysql.jdbc.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MainActivity3 extends AppCompatActivity {
EditText uname,upsw;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
uname = findViewById(R.id.uname);
upsw = findViewById(R.id.upsw);
btn = findViewById(R.id.login);
// 注销按钮点击事件
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 开启一个新线程来执行注销操作
new Thread(new Runnable() {
@Override
public void run() {
// 获取到 MySQL 数据库的连接
Connection con = jdbcHelper.getCon();
// 准备一个 SQL 查询语句来检查用户凭据
String sqlStr = "select * from userinfo where uname=? and psw=?";
try {
// 创建一个 PreparedStatement 对象
PreparedStatement ps = (PreparedStatement) con.prepareStatement(sqlStr);
// 设置查询参数
ps.setString(1,uname.getText().toString());
ps.setString(2,upsw.getText().toString());
// 进行查询
ResultSet rs = ps.executeQuery();
// 使用 Looper 来更新 UI
Looper.prepare();
// 判断查询结果是否存在
if(rs.next()) {
// 用户存在,执行注销操作
String sqlStr1 = "delete from userinfo where uname=?";
try {
// 创建一个 PreparedStatement 对象来执行注销操作
PreparedStatement ps1 = (PreparedStatement) con.prepareStatement(sqlStr1);
// 设置注销参数
ps1.setString(1,uname.getText().toString());
// 执行注销操作
int rows = ps1.executeUpdate();
// 判断注销是否成功
if(rows > 0) {
// 注销成功,更新 UI
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity3.this,"注销成功",Toast.LENGTH_SHORT).show();
}
});
} else {
// 注销失败,更新 UI
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity3.this,"注销失败",Toast.LENGTH_SHORT).show();
}
});
}
} catch (SQLException e) {
e.printStackTrace();
}
} else {
// 用户不存在,更新 UI
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity3.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
}
});
}
// 结束 Looper
Looper.loop();
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
结论
通过遵循本文中的步骤,你可以使用 Android Studio 通过 MySQL 数据库实现登录、注册和注销功能。这将使你的应用程序更加安全、用户友好。