经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android Studio连接MySql实现登录注册(附源代码)
来源:jb51  时间:2021/5/17 9:30:18  对本文有异议

本文主要介绍了Android Studio连接MySql实现登录注册,分享给大家,具体如下:

在这里插入图片描述

在这里插入图片描述

一、创建工程

1、创建一个空白工程

在这里插入图片描述

2、随便起一个名称

在这里插入图片描述

3、设置网络连接权限

在这里插入图片描述

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

二、引入Mysql驱动包

1、切换到普通Java工程

在这里插入图片描述

2、在libs当中引入MySQL的jar包

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

在这里插入图片描述

在这里插入图片描述

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

1、在数据库当中创建表

在这里插入图片描述

SQL语句

  1. /*
  2. Navicat MySQL Data Transfer
  3.  
  4. Source Server : localhost_3306
  5. Source Server Version : 50562
  6. Source Host : localhost:3306
  7. Source Database : test
  8.  
  9. Target Server Type : MYSQL
  10. Target Server Version : 50562
  11. File Encoding : 65001
  12.  
  13. Date: 2021-05-10 17:28:36
  14. */
  15.  
  16. SET FOREIGN_KEY_CHECKS=0;
  17.  
  18. -- ----------------------------
  19. -- Table structure for `student`
  20. -- ----------------------------
  21. DROP TABLE IF EXISTS `student`;
  22. CREATE TABLE `student` (
  23. `sid` int(11) NOT NULL AUTO_INCREMENT,
  24. `sname` varchar(255) NOT NULL,
  25. `sage` int(11) NOT NULL,
  26. `address` varchar(255) NOT NULL,
  27. PRIMARY KEY (`sid`)
  28. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
  29.  
  30. -- ----------------------------
  31. -- Records of student
  32. -- ----------------------------
  33. INSERT INTO `student` VALUES ('1', 'andi', '21', '21212');
  34. INSERT INTO `student` VALUES ('2', 'a', '2121', '2121');
  35.  
  36. -- ----------------------------
  37. -- Table structure for `users`
  38. -- ----------------------------
  39. DROP TABLE IF EXISTS `users`;
  40. CREATE TABLE `users` (
  41. `uid` int(11) NOT NULL AUTO_INCREMENT,
  42. `name` varchar(255) NOT NULL,
  43. `username` varchar(255) NOT NULL,
  44. `password` varchar(255) NOT NULL,
  45. `age` int(255) NOT NULL,
  46. `phone` longblob NOT NULL,
  47. PRIMARY KEY (`uid`)
  48. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
  49.  
  50. -- ----------------------------
  51. -- Records of users
  52. -- ----------------------------
  53. INSERT INTO `users` VALUES ('2', '123', 'HBV环保局', '123', '33', 0x3133333333333333333333);
  54. INSERT INTO `users` VALUES ('3', '1233', '反复的', '1233', '12', 0x3132333333333333333333);
  55. INSERT INTO `users` VALUES ('4', '1244', '第三代', '1244', '12', 0x3133333333333333333333);
  56. INSERT INTO `users` VALUES ('5', '1255', 'SAS', '1255', '33', 0x3133333333333333333333);
  57.  

2、在Android Studio当中创建JDBCUtils类

切换会Android视图

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

  1. package com.example.myapplication.utils;
  2.  
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7.  
  8. public class JDBCUtils {
  9.  
  10.  
  11.  
  12. static {
  13.  
  14. try {
  15. Class.forName("com.mysql.jdbc.Driver");
  16. } catch (ClassNotFoundException e) {
  17. e.printStackTrace();
  18. }
  19.  
  20. }
  21.  
  22. public static Connection getConn() {
  23. Connection conn = null;
  24. try {
  25. conn= DriverManager.getConnection("jdbc:mysql://10.0.2.2:3306/test","root","root");
  26. }catch (Exception exception){
  27. exception.printStackTrace();
  28. }
  29. return conn;
  30. }
  31.  
  32. public static void close(Connection conn){
  33. try {
  34. conn.close();
  35. } catch (SQLException throwables) {
  36. throwables.printStackTrace();
  37. }
  38. }
  39. }

3、创建User实体类

在这里插入图片描述

  1. package com.example.myapplication.entity;
  2.  
  3. public class User {
  4.  
  5. private int id;
  6. private String name;
  7. private String username;
  8. private String password;
  9. private int age;
  10. private String phone;
  11.  
  12.  
  13. public User() {
  14. }
  15.  
  16. public User(int id, String name, String username, String password, int age, String phone) {
  17. this.id = id;
  18. this.name = name;
  19. this.username = username;
  20. this.password = password;
  21. this.age = age;
  22. this.phone = phone;
  23. }
  24.  
  25. public int getId() {
  26. return id;
  27. }
  28.  
  29. public void setId(int id) {
  30. this.id = id;
  31. }
  32.  
  33. public String getName() {
  34. return name;
  35. }
  36.  
  37. public void setName(String name) {
  38. this.name = name;
  39. }
  40.  
  41. public String getUsername() {
  42. return username;
  43. }
  44.  
  45. public void setUsername(String username) {
  46. this.username = username;
  47. }
  48.  
  49. public String getPassword() {
  50. return password;
  51. }
  52.  
  53. public void setPassword(String password) {
  54. this.password = password;
  55. }
  56.  
  57. public int getAge() {
  58. return age;
  59. }
  60.  
  61. public void setAge(int age) {
  62. this.age = age;
  63. }
  64.  
  65. public String getPhone() {
  66. return phone;
  67. }
  68.  
  69. public void setPhone(String phone) {
  70. this.phone = phone;
  71. }
  72. }

4、创建dao层和UserDao

在这里插入图片描述

  1. package com.example.myapplication.dao;
  2.  
  3. import com.example.myapplication.entity.User;
  4. import com.example.myapplication.utils.JDBCUtils;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10.  
  11. public class UserDao {
  12.  
  13.  
  14. public boolean login(String name,String password){
  15.  
  16. String sql = "select * from users where name = ? and password = ?";
  17.  
  18. Connection con = JDBCUtils.getConn();
  19.  
  20. try {
  21. PreparedStatement pst=con.prepareStatement(sql);
  22.  
  23. pst.setString(1,name);
  24. pst.setString(2,password);
  25.  
  26. if(pst.executeQuery().next()){
  27.  
  28. return true;
  29.  
  30. }
  31.  
  32. } catch (SQLException throwables) {
  33. throwables.printStackTrace();
  34. }finally {
  35. JDBCUtils.close(con);
  36. }
  37.  
  38. return false;
  39. }
  40.  
  41. public boolean register(User user){
  42.  
  43. String sql = "insert into users(name,username,password,age,phone) values (?,?,?,?,?)";
  44.  
  45. Connection con = JDBCUtils.getConn();
  46.  
  47. try {
  48. PreparedStatement pst=con.prepareStatement(sql);
  49.  
  50. pst.setString(1,user.getName());
  51. pst.setString(2,user.getUsername());
  52. pst.setString(3,user.getPassword());
  53. pst.setInt(4,user.getAge());
  54. pst.setString(5,user.getPhone());
  55.  
  56. int value = pst.executeUpdate();
  57.  
  58. if(value>0){
  59. return true;
  60. }
  61.  
  62.  
  63. } catch (SQLException throwables) {
  64. throwables.printStackTrace();
  65. }finally {
  66. JDBCUtils.close(con);
  67. }
  68. return false;
  69. }
  70.  
  71. public User findUser(String name){
  72.  
  73. String sql = "select * from users where name = ?";
  74.  
  75. Connection con = JDBCUtils.getConn();
  76. User user = null;
  77. try {
  78. PreparedStatement pst=con.prepareStatement(sql);
  79.  
  80. pst.setString(1,name);
  81.  
  82. ResultSet rs = pst.executeQuery();
  83.  
  84. while (rs.next()){
  85.  
  86. int id = rs.getInt(0);
  87. String namedb = rs.getString(1);
  88. String username = rs.getString(2);
  89. String passworddb = rs.getString(3);
  90. int age = rs.getInt(4);
  91. String phone = rs.getString(5);
  92. user = new User(id,namedb,username,passworddb,age,phone);
  93. }
  94.  
  95. } catch (SQLException throwables) {
  96. throwables.printStackTrace();
  97. }finally {
  98. JDBCUtils.close(con);
  99. }
  100.  
  101. return user;
  102. }
  103.  
  104.  
  105. }
  106.  

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

1、编写登录页面

在这里插入图片描述

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8.  
  9.  
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:orientation="vertical"
  14. tools:layout_editor_absoluteX="219dp"
  15. tools:layout_editor_absoluteY="207dp"
  16. android:padding="50dp"
  17.  
  18. >
  19.  
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:orientation="horizontal">
  24.  
  25.  
  26. <TextView
  27. android:id="@+id/textView"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_weight="1"
  31. android:textSize="15sp"
  32. android:text="账号:" />
  33.  
  34. <EditText
  35. android:id="@+id/name"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_weight="1"
  39. android:ems="10"
  40. android:inputType="textPersonName"
  41. android:text="" />
  42. </LinearLayout>
  43. <LinearLayout
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:orientation="horizontal">
  47.  
  48.  
  49. <TextView
  50. android:id="@+id/textView2"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:layout_weight="1"
  54. android:textSize="15sp"
  55. android:text="密码:"
  56.  
  57. />
  58.  
  59. <EditText
  60. android:id="@+id/password"
  61. android:layout_width="wrap_content"
  62. android:layout_height="wrap_content"
  63. android:layout_weight="1"
  64. android:ems="10"
  65. android:inputType="textPersonName"
  66. />
  67. </LinearLayout>
  68. <LinearLayout
  69. android:layout_width="match_parent"
  70. android:layout_height="wrap_content"
  71. android:orientation="horizontal">
  72.  
  73.  
  74.  
  75. </LinearLayout>
  76.  
  77. <Button
  78. android:layout_marginTop="50dp"
  79. android:id="@+id/button2"
  80. android:layout_width="match_parent"
  81. android:layout_height="wrap_content"
  82. android:text="登录"
  83. android:onClick="login"
  84. />
  85.  
  86. <Button
  87. android:id="@+id/button3"
  88. android:layout_width="match_parent"
  89. android:layout_height="wrap_content"
  90. android:onClick="reg"
  91. android:text="注册" />
  92. </LinearLayout>
  93. </androidx.constraintlayout.widget.ConstraintLayout>

效果

在这里插入图片描述

2、编写注册页面代码

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8.  
  9.  
  10. <LinearLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:orientation="vertical"
  14. tools:layout_editor_absoluteX="219dp"
  15. tools:layout_editor_absoluteY="207dp"
  16. android:padding="50dp"
  17.  
  18. >
  19.  
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:orientation="horizontal">
  24.  
  25.  
  26. <TextView
  27. android:id="@+id/textView"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_weight="1"
  31. android:textSize="15sp"
  32. android:text="账号:" />
  33.  
  34. <EditText
  35. android:id="@+id/name"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_weight="1"
  39. android:ems="10"
  40. android:inputType="textPersonName"
  41. android:text="" />
  42. </LinearLayout>
  43. <LinearLayout
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:orientation="horizontal">
  47.  
  48.  
  49. <TextView
  50. android:id="@+id/textView2"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:layout_weight="1"
  54. android:textSize="15sp"
  55. android:text="密码:"
  56.  
  57. />
  58.  
  59. <EditText
  60. android:id="@+id/password"
  61. android:layout_width="wrap_content"
  62. android:layout_height="wrap_content"
  63. android:layout_weight="1"
  64. android:ems="10"
  65. android:inputType="textPersonName"
  66. />
  67. </LinearLayout>
  68. <LinearLayout
  69. android:layout_width="match_parent"
  70. android:layout_height="wrap_content"
  71. android:orientation="horizontal">
  72.  
  73.  
  74.  
  75. </LinearLayout>
  76.  
  77. <Button
  78. android:layout_marginTop="50dp"
  79. android:id="@+id/button2"
  80. android:layout_width="match_parent"
  81. android:layout_height="wrap_content"
  82. android:text="登录"
  83. android:onClick="login"
  84. />
  85.  
  86. <Button
  87. android:id="@+id/button3"
  88. android:layout_width="match_parent"
  89. android:layout_height="wrap_content"
  90. android:onClick="reg"
  91. android:text="注册" />
  92. </LinearLayout>
  93. </androidx.constraintlayout.widget.ConstraintLayout>

3、完善MainActivity

在这里插入图片描述

  1. package com.example.application01;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.os.Message;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13. import com.example.application01.dao.UserDao;
  14.  
  15. public class MainActivity extends AppCompatActivity {
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. }
  22.  
  23. public void reg(View view){
  24.  
  25. startActivity(new Intent(getApplicationContext(),RegisterActivity.class));
  26.  
  27. }
  28.  
  29.  
  30. public void login(View view){
  31.  
  32. EditText EditTextname = (EditText)findViewById(R.id.name);
  33. EditText EditTextpassword = (EditText)findViewById(R.id.password);
  34.  
  35. new Thread(){
  36. @Override
  37. public void run() {
  38.  
  39. UserDao userDao = new UserDao();
  40.  
  41. boolean aa = userDao.login(EditTextname.getText().toString(),EditTextpassword.getText().toString());
  42. int msg = 0;
  43. if(aa){
  44. msg = 1;
  45. }
  46.  
  47. hand1.sendEmptyMessage(msg);
  48.  
  49.  
  50. }
  51. }.start();
  52.  
  53.  
  54. }
  55. final Handler hand1 = new Handler()
  56. {
  57. @Override
  58. public void handleMessage(Message msg) {
  59.  
  60. if(msg.what == 1)
  61. {
  62. Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_LONG).show();
  63.  
  64. }
  65. else
  66. {
  67. Toast.makeText(getApplicationContext(),"登录失败",Toast.LENGTH_LONG).show();
  68. }
  69. }
  70. };
  71. }

4、完善RegisterActivity

在这里插入图片描述

  1. package com.example.application01;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.os.Handler;
  8. import android.os.Message;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.Toast;
  12.  
  13. import com.example.application01.dao.UserDao;
  14. import com.example.application01.entity.User;
  15.  
  16. public class RegisterActivity extends AppCompatActivity {
  17. EditText name = null;
  18. EditText username = null;
  19. EditText password = null;
  20. EditText phone = null;
  21. EditText age = null;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_register);
  26.  
  27. name = findViewById(R.id.name);
  28. username = findViewById(R.id.username);
  29. password = findViewById(R.id.password);
  30. phone = findViewById(R.id.phone);
  31. age = findViewById(R.id.age);
  32. }
  33.  
  34.  
  35. public void register(View view){
  36.  
  37.  
  38.  
  39. String cname = name.getText().toString();
  40. String cusername = username.getText().toString();
  41. String cpassword = password.getText().toString();
  42.  
  43. System.out.println(phone.getText().toString());
  44.  
  45. String cphone = phone.getText().toString();
  46. int cgae = Integer.parseInt(age.getText().toString());
  47.  
  48. if(cname.length() < 2 || cusername.length() < 2 || cpassword.length() < 2 ){
  49. Toast.makeText(getApplicationContext(),"输入信息不符合要求请重新输入",Toast.LENGTH_LONG).show();
  50. return;
  51.  
  52. }
  53.  
  54.  
  55. User user = new User();
  56.  
  57. user.setName(cname);
  58. user.setUsername(cusername);
  59. user.setPassword(cpassword);
  60. user.setAge(cgae);
  61. user.setPhone(cphone);
  62.  
  63. new Thread(){
  64. @Override
  65. public void run() {
  66.  
  67. int msg = 0;
  68.  
  69. UserDao userDao = new UserDao();
  70.  
  71. User uu = userDao.findUser(user.getName());
  72.  
  73. if(uu != null){
  74. msg = 1;
  75. }
  76.  
  77. boolean flag = userDao.register(user);
  78. if(flag){
  79. msg = 2;
  80. }
  81. hand.sendEmptyMessage(msg);
  82.  
  83. }
  84. }.start();
  85.  
  86.  
  87. }
  88. final Handler hand = new Handler()
  89. {
  90. @Override
  91. public void handleMessage(Message msg) {
  92. if(msg.what == 0)
  93. {
  94. Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show();
  95.  
  96. }
  97. if(msg.what == 1)
  98. {
  99. Toast.makeText(getApplicationContext(),"该账号已经存在,请换一个账号",Toast.LENGTH_LONG).show();
  100.  
  101. }
  102. if(msg.what == 2)
  103. {
  104. //startActivity(new Intent(getApplication(),MainActivity.class));
  105.  
  106. Intent intent = new Intent();
  107. //将想要传递的数据用putExtra封装在intent中
  108. intent.putExtra("a","註冊");
  109. setResult(RESULT_CANCELED,intent);
  110. finish();
  111. }
  112.  
  113. }
  114. };
  115. }

五、运行测试效果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

到此这篇关于Android Studio连接MySql实现登录注册(附源代码) 的文章就介绍到这了,更多相关Android Studio 登录注册内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号